Skip to content

Commit

Permalink
refactor: Disambiguate the clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
nzdjb committed Jul 4, 2023
1 parent 76e0930 commit 6bcb89b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 55 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ A Go library implementing the [Metaname](https://metaname.net.nz) API.

## Usage

Create a client with NewClient, passing it your account reference and API key, then use it to call the needed functions.
Create a client with NewMetanameClient, passing it your account reference and API key, then use it to call the needed functions.

```go
client := NewClient(os.Getenv("ACCOUNT_REF"), os.Getenv("API_KEY"))
client := NewMetanameClient(os.Getenv("ACCOUNT_REF"), os.Getenv("API_KEY"))
client.DeleteDnsRecord(ctx, "example.org", "1234")
```

Expand Down
33 changes: 17 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package metaname

import (
"context"

"github.com/AdamSLevy/jsonrpc2/v14"
)

type IClient interface {
type IJsonRpc2Client interface {
Request(context context.Context, host string, method string, params interface{}, result interface{}) error
}

type Client struct {
Client IClient
type MetanameClient struct {
RpcClient IJsonRpc2Client
Host string
AccountReference string
APIKey string
Expand All @@ -25,43 +26,43 @@ type ResourceRecord struct {
Data string `json:"data"`
}

func NewClient(accountReference string, apiKey string) *Client {
return &Client{
Client: &jsonrpc2.Client{},
func NewMetanameClient(accountReference string, apiKey string) *MetanameClient {
return &MetanameClient{
RpcClient: &jsonrpc2.Client{},
Host: "https://metaname.net/api/1.1",
AccountReference: accountReference,
APIKey: apiKey,
}
}

func (c *Client) CreateDnsRecord(ctx context.Context, domainName string, record ResourceRecord) (string, error) {
func (c *MetanameClient) CreateDnsRecord(ctx context.Context, domainName string, record ResourceRecord) (string, error) {
params := []interface{}{c.AccountReference, c.APIKey, domainName, record}
var result string
err := c.Client.Request(ctx, c.Host, "create_dns_record", params, result)
err := c.RpcClient.Request(ctx, c.Host, "create_dns_record", params, result)
return result, err
}

func (c *Client) UpdateDnsRecord(ctx context.Context, domainName string, reference string, record ResourceRecord) error {
func (c *MetanameClient) UpdateDnsRecord(ctx context.Context, domainName string, reference string, record ResourceRecord) error {
params := []interface{}{c.AccountReference, c.APIKey, domainName, reference, record}
err := c.Client.Request(ctx, c.Host, "update_dns_record", params, nil)
err := c.RpcClient.Request(ctx, c.Host, "update_dns_record", params, nil)
return err
}

func (c *Client) DeleteDnsRecord(ctx context.Context, domainName string, reference string) error {
func (c *MetanameClient) DeleteDnsRecord(ctx context.Context, domainName string, reference string) error {
params := []interface{}{c.AccountReference, c.APIKey, domainName, reference}
err := c.Client.Request(ctx, c.Host, "delete_dns_record", params, nil)
err := c.RpcClient.Request(ctx, c.Host, "delete_dns_record", params, nil)
return err
}

func (c *Client) DnsZone(ctx context.Context, domainName string) ([]ResourceRecord, error) {
func (c *MetanameClient) DnsZone(ctx context.Context, domainName string) ([]ResourceRecord, error) {
params := []interface{}{c.AccountReference, c.APIKey, domainName}
var result []ResourceRecord
err := c.Client.Request(ctx, c.Host, "dns_zone", params, result)
err := c.RpcClient.Request(ctx, c.Host, "dns_zone", params, result)
return result, err
}

func (c *Client) ConfigureZone(ctx context.Context, zoneName string) error {
func (c *MetanameClient) ConfigureZone(ctx context.Context, zoneName string) error {
params := []interface{}{c.AccountReference, c.APIKey, zoneName, []ResourceRecord{}, nil}
err := c.Client.Request(ctx, c.Host, "configure_zone", params, nil)
err := c.RpcClient.Request(ctx, c.Host, "configure_zone", params, nil)
return err
}
74 changes: 37 additions & 37 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func (fc *FauxClient) Request(context context.Context, host string, method strin
}

func TestNewClient(t *testing.T) {
c := NewClient("username", "apikey")
c := NewMetanameClient("username", "apikey")
assert.Equal(t, "https://metaname.net/api/1.1", c.Host)
assert.Equal(t, "username", c.AccountReference)
assert.Equal(t, "apikey", c.APIKey)
}

func TestDnsZone(t *testing.T) {
c := &Client{
Client: &FauxClient{},
c := &MetanameClient{
RpcClient: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
Expand All @@ -40,14 +40,14 @@ func TestDnsZone(t *testing.T) {
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "dns_zone", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz"}, c.Client.(*FauxClient).paramsUsed)
assert.True(t, c.RpcClient.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.RpcClient.(*FauxClient).hostUsed)
assert.Equal(t, "dns_zone", c.RpcClient.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz"}, c.RpcClient.(*FauxClient).paramsUsed)
}
func TestConfigureZone(t *testing.T) {
c := &Client{
Client: &FauxClient{},
c := &MetanameClient{
RpcClient: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
Expand All @@ -56,15 +56,15 @@ func TestConfigureZone(t *testing.T) {
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "configure_zone", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", []ResourceRecord{}, nil}, c.Client.(*FauxClient).paramsUsed)
assert.True(t, c.RpcClient.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.RpcClient.(*FauxClient).hostUsed)
assert.Equal(t, "configure_zone", c.RpcClient.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", []ResourceRecord{}, nil}, c.RpcClient.(*FauxClient).paramsUsed)
}

func TestCreateDnsRecord(t *testing.T) {
c := &Client{
Client: &FauxClient{},
c := &MetanameClient{
RpcClient: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
Expand All @@ -79,15 +79,15 @@ func TestCreateDnsRecord(t *testing.T) {
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "create_dns_record", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", record}, c.Client.(*FauxClient).paramsUsed)
assert.True(t, c.RpcClient.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.RpcClient.(*FauxClient).hostUsed)
assert.Equal(t, "create_dns_record", c.RpcClient.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", record}, c.RpcClient.(*FauxClient).paramsUsed)
}

func TestCreateMXDnsRecord(t *testing.T) {
c := &Client{
Client: &FauxClient{},
c := &MetanameClient{
RpcClient: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
Expand All @@ -103,15 +103,15 @@ func TestCreateMXDnsRecord(t *testing.T) {
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "create_dns_record", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", record}, c.Client.(*FauxClient).paramsUsed)
assert.True(t, c.RpcClient.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.RpcClient.(*FauxClient).hostUsed)
assert.Equal(t, "create_dns_record", c.RpcClient.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", record}, c.RpcClient.(*FauxClient).paramsUsed)
}

func TestUpdateDnsRecord(t *testing.T) {
c := &Client{
Client: &FauxClient{},
c := &MetanameClient{
RpcClient: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
Expand All @@ -126,14 +126,14 @@ func TestUpdateDnsRecord(t *testing.T) {
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "update_dns_record", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", "1234", record}, c.Client.(*FauxClient).paramsUsed)
assert.True(t, c.RpcClient.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.RpcClient.(*FauxClient).hostUsed)
assert.Equal(t, "update_dns_record", c.RpcClient.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", "1234", record}, c.RpcClient.(*FauxClient).paramsUsed)
}
func TestDeleteDnsRecord(t *testing.T) {
c := &Client{
Client: &FauxClient{},
c := &MetanameClient{
RpcClient: &FauxClient{},
Host: "abc",
AccountReference: "def",
APIKey: "ghi",
Expand All @@ -142,8 +142,8 @@ func TestDeleteDnsRecord(t *testing.T) {
if err != nil {
panic(err)
}
assert.True(t, c.Client.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.Client.(*FauxClient).hostUsed)
assert.Equal(t, "delete_dns_record", c.Client.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", "1234"}, c.Client.(*FauxClient).paramsUsed)
assert.True(t, c.RpcClient.(*FauxClient).requestCalled)
assert.Equal(t, "abc", c.RpcClient.(*FauxClient).hostUsed)
assert.Equal(t, "delete_dns_record", c.RpcClient.(*FauxClient).methodUsed)
assert.Equal(t, []interface{}{"def", "ghi", "testzone.nz", "1234"}, c.RpcClient.(*FauxClient).paramsUsed)
}

0 comments on commit 6bcb89b

Please sign in to comment.