Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
go-version: '1.24'

- name: Check out code
uses: actions/checkout@v4
uses: actions/checkout@v5
with:
ref: ${{github.event.pull_request.head.sha}}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-on-tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/checkout@v5
- uses: softprops/action-gh-release@v2
with:
generate_release_notes: true
5 changes: 3 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
version: "2"

linters:
default: none
enable:
- staticcheck
disable-all: true
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ repos:
- id: check-yaml
- id: check-merge-conflict
- repo: https://github.com/golangci/golangci-lint
rev: v1.63.4
rev: v2.3.1
hooks:
- id: golangci-lint
- repo: https://github.com/dnephin/pre-commit-golang
Expand Down
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
# Active24.cz client in Go

This is client library to interact with [Active24 API](https://faq.active24.com/eng/739445-REST-API-for-developers?l=en-US).
This is client library to interact with [Active24 APIv2](https://www.active24.cz/centrum-napovedy/api-rozhrani).
Currently, only subset of API is implemented, but contributions are always welcome.

## Usage

```go
package main

import "github.com/rkosegi/active24-go/active24"
import "github.com/hostalp/active24-go/active24"

func main() {
client := active24.New("my-secret-api-token")
client := active24.New("my-secret-api-key", "my-secret-api-secret")

alias := "host1"
_, err := client.Dns().With("example.com").Create(active24.DnsRecordTypeA, &active24.DnsRecord{
Alias: &alias,
recordType := string(active24.DnsRecordTypeA)
hostName := "host1" // Short hostname (excl. the domain)
ipAddress := "1.2.3.4"
ttl := 600

_, err := client.Dns().With("example.com", 12345678).Create(&active24.DnsRecord{
Type: &recordType,
Name: hostName;
Content: &ipAddress,
Ttl: ttl,
})
if err != nil {
panic(err)
Expand Down
76 changes: 54 additions & 22 deletions active24/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ limitations under the License.
package active24

import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"fmt"
"io"
"k8s.io/klog/v2"
"net/http"
"net/url"
"path"
"time"
)

const (
SandboxToken = "123456qwerty-ok"
"k8s.io/klog/v2"
)

type Option func(c *client)
Expand All @@ -50,19 +52,19 @@ type ApiError interface {
type Client interface {
// Dns provides interface to interact with DNS records
Dns() Dns
// Domains provides interface to interact with domains
Domains() Domains
}

func New(apiKey string, opts ...Option) Client {
func New(apiKey string, apiSecret string, opts ...Option) Client {
c := &client{
h: helper{
apiEndpoint: "https://api.active24.com",
auth: apiKey,
apiEndpoint: "https://rest.active24.cz",
authKey: apiKey,
authSecret: apiSecret,
c: http.Client{
Timeout: time.Second * 10,
},
l: klog.NewKlogr(),
l: klog.NewKlogr(),
maxPages: 100, // default max pages to prevent infinite loops
},
}
for _, opt := range opts {
Expand All @@ -81,12 +83,6 @@ func (c *client) Dns() Dns {
}
}

func (c *client) Domains() Domains {
return &domains{
h: c.h,
}
}

type apiError struct {
err error
resp *http.Response
Expand All @@ -102,21 +98,57 @@ func (a *apiError) Response() *http.Response {

type helper struct {
apiEndpoint string
auth string
authKey string
authSecret string
c http.Client
l klog.Logger
maxPages int
}

func (ch *helper) getSignature(message, key string) string {
h := hmac.New(sha1.New, []byte(key))
h.Write([]byte(message))
return hex.EncodeToString(h.Sum(nil))
}

func (ch *helper) do(method string, suffix string, body io.Reader) (*http.Response, error) {
r, err := http.NewRequest(method, fmt.Sprintf("%s/%s", ch.apiEndpoint, suffix), body)
func (ch *helper) do(reqMethod string, reqPath string, reqBody io.Reader) (*http.Response, error) {
return ch.doWithParams(reqMethod, reqPath, nil, reqBody)
}

func (ch *helper) doWithParams(reqMethod string, reqPath string, reqParams url.Values, reqBody io.Reader) (*http.Response, error) {
reqPath = path.Join("/", reqPath)
reqTimestamp := time.Now()
canonicalRequest := fmt.Sprintf("%s %s %d", reqMethod, reqPath, reqTimestamp.Unix())
authSignature := ch.getSignature(canonicalRequest, ch.authSecret)

r, err := http.NewRequest(reqMethod, fmt.Sprintf("%s%s", ch.apiEndpoint, reqPath), reqBody)
if err != nil {
return nil, err
}
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", ch.auth))
r.URL.RawQuery = reqParams.Encode()
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
ch.l.V(4).Info("Calling API", "method", method, "url", r.URL.String())
return ch.c.Do(r)
r.Header.Set("Date", reqTimestamp.UTC().Format(time.RFC3339))
r.SetBasicAuth(ch.authKey, authSignature)
ch.l.V(4).Info("Calling API", "method", reqMethod, "URL", r.URL.String())

// Log the request
/* dumpReq, dumpErr := httputil.DumpRequestOut(r, true)
if dumpErr != nil {
return nil, dumpErr
}
ch.l.V(4).Info("doWithParams", "REQUEST", string(dumpReq)) */

resp, err := ch.c.Do(r)

// Log the response
/* dumpResp, dumpErr := httputil.DumpResponse(resp, true)
if dumpErr != nil {
return nil, dumpErr
}
ch.l.V(4).Info("doWithParams", "RESPONSE", string(dumpResp)) */

return resp, err
}

func apiErr(resp *http.Response, err error) ApiError {
Expand Down
28 changes: 0 additions & 28 deletions active24/client_test.go

This file was deleted.

Loading
Loading