Skip to content

Commit

Permalink
Package refactoring (#92)
Browse files Browse the repository at this point in the history
* move helpers to utils package

* testutils

* group common types

* connector config moved

* connector common responses

* common response in connector setup tests

* test fix

* connector schema common types

* dbt refactoring

* fix examples

* destinations common parts

* external logging common parts

* users common types

* webhooks common types

* teams common types

* fix json marshalling

* http utils package

* http service base
  • Loading branch information
beevital authored Oct 2, 2023
1 parent d40ce58 commit 5015e04
Show file tree
Hide file tree
Showing 240 changed files with 6,635 additions and 6,543 deletions.
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

0 comments on commit 5015e04

Please sign in to comment.