Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package refactoring #92

Merged
merged 18 commits into from
Oct 2, 2023
22 changes: 12 additions & 10 deletions certificate_connector_certificate_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"

httputils "github.com/fivetran/go-fivetran/http_utils"
)

// CertificateConnectorCertificateApproveService implements the Certificate Management, Approve a connector certificate API.
Expand Down Expand Up @@ -66,18 +68,18 @@ func (s *CertificateConnectorCertificateApproveService) Do(ctx context.Context)
return response, err
}

r := request{
method: "POST",
url: url,
body: reqBody,
queries: nil,
headers: headers,
client: s.c.httpClient,
handleRateLimits: s.c.handleRateLimits,
maxRetryAttempts: s.c.maxRetryAttempts,
r := httputils.Request{
Method: "POST",
Url: url,
Body: reqBody,
Queries: nil,
Headers: headers,
Client: s.c.httpClient,
HandleRateLimits: s.c.handleRateLimits,
MaxRetryAttempts: s.c.maxRetryAttempts,
}

respBody, respStatus, err := r.httpRequest(ctx)
respBody, respStatus, err := r.Do(ctx)
if err != nil {
return response, err
}
Expand Down
14 changes: 8 additions & 6 deletions certificate_connector_certificate_approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package fivetran_test
import (
"context"
"testing"

testutils "github.com/fivetran/go-fivetran/test_utils"
)

func TestNewCertificateConnectorCertificateApproveE2E(t *testing.T) {
connectorId := CreateTempConnector(t)
response, err := Client.NewCertificateConnectorCertificateApprove().
connectorId := testutils.CreateTempConnector(t)
response, err := testutils.Client.NewCertificateConnectorCertificateApprove().
ConnectorID(connectorId).
Hash(CertificateHash).
EncodedCert(EncodedCertificate).
Hash(testutils.CertificateHash).
EncodedCert(testutils.EncodedCertificate).
Do(context.Background())

if err != nil {
t.Logf("%+v\n", response)
t.Error(err)
}

AssertEqual(t, response.Code, "Success")
AssertNotEmpty(t, response.Message)
testutils.AssertEqual(t, response.Code, "Success")
testutils.AssertNotEmpty(t, response.Message)
}
22 changes: 12 additions & 10 deletions certificate_connector_fingerprint_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"

httputils "github.com/fivetran/go-fivetran/http_utils"
)

// CertificateConnectorFingerprintApproveService implements the Certificate Management, Approve a connector fingerprint API.
Expand Down Expand Up @@ -66,18 +68,18 @@ func (s *CertificateConnectorFingerprintApproveService) Do(ctx context.Context)
return response, err
}

r := request{
method: "POST",
url: url,
body: reqBody,
queries: nil,
headers: headers,
client: s.c.httpClient,
handleRateLimits: s.c.handleRateLimits,
maxRetryAttempts: s.c.maxRetryAttempts,
r := httputils.Request{
Method: "POST",
Url: url,
Body: reqBody,
Queries: nil,
Headers: headers,
Client: s.c.httpClient,
HandleRateLimits: s.c.handleRateLimits,
MaxRetryAttempts: s.c.maxRetryAttempts,
}

respBody, respStatus, err := r.httpRequest(ctx)
respBody, respStatus, err := r.Do(ctx)
if err != nil {
return response, err
}
Expand Down
10 changes: 6 additions & 4 deletions certificate_connector_fingerprint_approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package fivetran_test
import (
"context"
"testing"

testutils "github.com/fivetran/go-fivetran/test_utils"
)

func TestNewCertificateConnectorFingerprintApproveE2E(t *testing.T) {
connectorId := CreateTempConnector(t)
response, err := Client.NewCertificateConnectorFingerprintApprove().
connectorId := testutils.CreateTempConnector(t)
response, err := testutils.Client.NewCertificateConnectorFingerprintApprove().
ConnectorID(connectorId).
Hash("test_hash").
PublicKey("test_public_key").
Expand All @@ -18,6 +20,6 @@ func TestNewCertificateConnectorFingerprintApproveE2E(t *testing.T) {
t.Error(err)
}

AssertEqual(t, response.Code, "Success")
AssertNotEmpty(t, response.Message)
testutils.AssertEqual(t, response.Code, "Success")
testutils.AssertNotEmpty(t, response.Message)
}
22 changes: 12 additions & 10 deletions certificate_destination_certificate_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"

httputils "github.com/fivetran/go-fivetran/http_utils"
)

// CertificateDestinationCertificateApproveService implements the Certificate Management, Approve a destination certificate API.
Expand Down Expand Up @@ -66,18 +68,18 @@ func (s *CertificateDestinationCertificateApproveService) Do(ctx context.Context
return response, err
}

r := request{
method: "POST",
url: url,
body: reqBody,
queries: nil,
headers: headers,
client: s.c.httpClient,
handleRateLimits: s.c.handleRateLimits,
maxRetryAttempts: s.c.maxRetryAttempts,
r := httputils.Request{
Method: "POST",
Url: url,
Body: reqBody,
Queries: nil,
Headers: headers,
Client: s.c.httpClient,
HandleRateLimits: s.c.handleRateLimits,
MaxRetryAttempts: s.c.maxRetryAttempts,
}

respBody, respStatus, err := r.httpRequest(ctx)
respBody, respStatus, err := r.Do(ctx)
if err != nil {
return response, err
}
Expand Down
14 changes: 8 additions & 6 deletions certificate_destination_certificate_approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ package fivetran_test
import (
"context"
"testing"

testutils "github.com/fivetran/go-fivetran/test_utils"
)

func TestNewCertificateDestinationCertificateApproveE2E(t *testing.T) {
destinationId := CreateTempDestination(t)
response, err := Client.NewCertificateDestinationCertificateApprove().
destinationId := testutils.CreateTempDestination(t)
response, err := testutils.Client.NewCertificateDestinationCertificateApprove().
DestinationID(destinationId).
Hash(CertificateHash).
EncodedCert(EncodedCertificate).
Hash(testutils.CertificateHash).
EncodedCert(testutils.EncodedCertificate).
Do(context.Background())

if err != nil {
t.Logf("%+v\n", response)
t.Error(err)
}

AssertEqual(t, response.Code, "Success")
AssertNotEmpty(t, response.Message)
testutils.AssertEqual(t, response.Code, "Success")
testutils.AssertNotEmpty(t, response.Message)
}
22 changes: 12 additions & 10 deletions certificate_destination_fingerprint_approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"fmt"

httputils "github.com/fivetran/go-fivetran/http_utils"
)

// CertificateDestinationFingerprintApproveService implements the Certificate Management, Approve a destination fingerprint API.
Expand Down Expand Up @@ -66,18 +68,18 @@ func (s *CertificateDestinationFingerprintApproveService) Do(ctx context.Context
return response, err
}

r := request{
method: "POST",
url: url,
body: reqBody,
queries: nil,
headers: headers,
client: s.c.httpClient,
handleRateLimits: s.c.handleRateLimits,
maxRetryAttempts: s.c.maxRetryAttempts,
r := httputils.Request{
Method: "POST",
Url: url,
Body: reqBody,
Queries: nil,
Headers: headers,
Client: s.c.httpClient,
HandleRateLimits: s.c.handleRateLimits,
MaxRetryAttempts: s.c.maxRetryAttempts,
}

respBody, respStatus, err := r.httpRequest(ctx)
respBody, respStatus, err := r.Do(ctx)
if err != nil {
return response, err
}
Expand Down
10 changes: 6 additions & 4 deletions certificate_destination_fingerprint_approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package fivetran_test
import (
"context"
"testing"

testutils "github.com/fivetran/go-fivetran/test_utils"
)

func TestNewCertificateDestinationFingerprintApproveE2E(t *testing.T) {
destinationId := CreateTempDestination(t)
response, err := Client.NewCertificateDestinationFingerprintApprove().
destinationId := testutils.CreateTempDestination(t)
response, err := testutils.Client.NewCertificateDestinationFingerprintApprove().
DestinationID(destinationId).
Hash("test_hash").
PublicKey("test_public_key").
Expand All @@ -18,6 +20,6 @@ func TestNewCertificateDestinationFingerprintApproveE2E(t *testing.T) {
t.Error(err)
}

AssertEqual(t, response.Code, "Success")
AssertNotEmpty(t, response.Message)
testutils.AssertEqual(t, response.Code, "Success")
testutils.AssertNotEmpty(t, response.Message)
}
31 changes: 23 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@ import (
"encoding/base64"
"fmt"
"net/http"
)

// HttpClient performs an HTTP request. Can be implemented by mocks
// to perform convenient unit tests
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
httputils "github.com/fivetran/go-fivetran/http_utils"
)

// Client holds client configuration
type Client struct {
baseURL string
authorization string
customUserAgent string
httpClient HttpClient
httpClient httputils.HttpClient
handleRateLimits bool
maxRetryAttempts int
}
Expand Down Expand Up @@ -52,7 +48,7 @@ func (c *Client) CustomUserAgent(customUserAgent string) {
}

// SetHttpClient sets custom HTTP client to perform requests with
func (c *Client) SetHttpClient(httpClient HttpClient) {
func (c *Client) SetHttpClient(httpClient httputils.HttpClient) {
c.httpClient = httpClient
}

Expand All @@ -78,3 +74,22 @@ func (c *Client) commonHeaders() map[string]string {
"User-Agent": userAgent,
}
}

func (c *Client) commonHeadersByMethod(method string) map[string]string {
userAgent := defaultUserAgent

if c.customUserAgent != "" {
userAgent += " " + c.customUserAgent
}

result := map[string]string{
"Authorization": c.authorization,
"User-Agent": userAgent,
}

if method == "POST" || method == "PATCH" {
result["Content-Type"] = "application/json"
}

return result
}
32 changes: 32 additions & 0 deletions common/common_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package common

import "encoding/json"

type CommonResponse struct {
Code string `json:"code"`
Message string `json:"message"`
}

type SetupTestResponse struct {
Title string `json:"title"`
Status string `json:"status"`
Message string `json:"message"`
}

type NullableString struct {
value *string
}

func (n *NullableString) MarshalJSON() ([]byte, error) {
return json.Marshal(n.value)
}

func NewNullableString(s *string, clear bool) *NullableString {
if s == nil && !clear {
return nil
}

return &NullableString{
value: s,
}
}
Loading