-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
299bcaf
commit 5cb14bc
Showing
22 changed files
with
616 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the | ||
// README at: https://github.com/devcontainers/templates/tree/main/src/go | ||
{ | ||
"name": "veriff-api-client-go", | ||
"image": "mcr.microsoft.com/devcontainers/go:1-1.21-bookworm", | ||
"features": { | ||
"ghcr.io/brokeyourbike/devcontainer-features/staticcheck:0": { | ||
"version": "2023.1.6" | ||
}, | ||
"ghcr.io/brokeyourbike/devcontainer-features/mockery-go:0": { | ||
"version": "2.38.0" | ||
} | ||
}, | ||
"customizations": { | ||
"vscode": { | ||
"extensions": [ | ||
"golang.Go" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github: brokeyourbike | ||
open_collective: brokeyourbike |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: lint | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
|
||
lint: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: 'go.mod' | ||
- uses: brokeyourbike/go-mockery-action@v0.1 | ||
with: | ||
mockery-version: '2.38.0' | ||
- run: mockery --quiet --inpackage --all --testonly | ||
- uses: dominikh/staticcheck-action@v1 | ||
with: | ||
version: '2023.1.6' | ||
install-go: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: release-please | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
update_release_draft: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
|
||
steps: | ||
- uses: google-github-actions/release-please-action@v2 | ||
with: | ||
release-type: go | ||
bump-minor-pre-major: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: tests | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ main ] | ||
|
||
jobs: | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 5 | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: actions/setup-go@v4 | ||
with: | ||
go-version-file: 'go.mod' | ||
- uses: brokeyourbike/go-mockery-action@v0.1 | ||
with: | ||
mockery-version: '2.38.0' | ||
- run: mockery --quiet --inpackage --all --testonly | ||
- run: go build -v ./... | ||
- run: go test -race -covermode=atomic -coverprofile=coverage.out -v ./... | ||
|
||
- uses: paambaati/codeclimate-action@v5 | ||
continue-on-error: true | ||
env: | ||
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE_TOKEN }} | ||
with: | ||
prefix: 'github.com/brokeyourbike/veriff-api-client-go' | ||
coverageLocations: ./coverage.out:gocov | ||
|
||
- uses: codecov/codecov-action@v3 | ||
continue-on-error: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,12 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
.DS_Store | ||
/*.pem | ||
/*.json | ||
/mocks | ||
mock_*.go | ||
/config.toml | ||
/main.go | ||
/main_test.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,27 @@ | ||
# veriff-api-client-go | ||
|
||
[![Go Reference](https://pkg.go.dev/badge/github.com/brokeyourbike/veriff-api-client-go.svg)](https://pkg.go.dev/github.com/brokeyourbike/veriff-api-client-go) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/brokeyourbike/veriff-api-client-go)](https://goreportcard.com/report/github.com/brokeyourbike/veriff-api-client-go) | ||
[![Maintainability](https://api.codeclimate.com/v1/badges/698070207d9d5a7bb44e/maintainability)](https://codeclimate.com/github/brokeyourbike/veriff-api-client-go/maintainability) | ||
[![Test Coverage](https://api.codeclimate.com/v1/badges/698070207d9d5a7bb44e/test_coverage)](https://codeclimate.com/github/brokeyourbike/veriff-api-client-go/test_coverage) | ||
|
||
Veriff API Client for Go | ||
|
||
## Installation | ||
|
||
```bash | ||
go get github.com/brokeyourbike/veriff-api-client-go | ||
``` | ||
|
||
## Usage | ||
|
||
```go | ||
client := veriff.NewClient("veriff.com", "token") | ||
client.CreateSession(context.TODO(), veriff.CreateSessionPayload{}) | ||
``` | ||
|
||
## Authors | ||
- [Ivan Stasiuk](https://github.com/brokeyourbike) | [Twitter](https://twitter.com/brokeyourbike) | [LinkedIn](https://www.linkedin.com/in/brokeyourbike) | [stasi.uk](https://stasi.uk) | ||
|
||
## License | ||
[BSD-3-Clause License](https://github.com/brokeyourbike/veriff-api-client-go/blob/main/LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package veriff | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type HttpClient interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
type Client interface { | ||
CreateSession(ctx context.Context, payload CreateSessionPayload) (data CreateSessionResponse, err error) | ||
} | ||
|
||
var _ Client = (*client)(nil) | ||
|
||
type client struct { | ||
httpClient HttpClient | ||
logger *logrus.Logger | ||
baseURL string | ||
token string | ||
} | ||
|
||
// ClientOption is a function that configures a Client. | ||
type ClientOption func(*client) | ||
|
||
// WithHTTPClient sets the HTTP client for the paystack API client. | ||
func WithHTTPClient(c HttpClient) ClientOption { | ||
return func(target *client) { | ||
target.httpClient = c | ||
} | ||
} | ||
|
||
// WithLogger sets the *logrus.Logger for the paystack API client. | ||
func WithLogger(l *logrus.Logger) ClientOption { | ||
return func(target *client) { | ||
target.logger = l | ||
} | ||
} | ||
|
||
func NewClient(baseURL, token string, options ...ClientOption) *client { | ||
c := &client{ | ||
baseURL: strings.TrimSuffix(baseURL, "/"), | ||
token: token, | ||
} | ||
|
||
c.httpClient = http.DefaultClient | ||
|
||
for _, option := range options { | ||
option(c) | ||
} | ||
|
||
return c | ||
} | ||
|
||
func (c *client) newRequest(ctx context.Context, method, url string, body interface{}) (*request, error) { | ||
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+url, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create request: %w", err) | ||
} | ||
|
||
var b []byte | ||
|
||
if body != nil { | ||
b, err = json.Marshal(body) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal payload: %w", err) | ||
} | ||
|
||
req.Body = io.NopCloser(bytes.NewReader(b)) | ||
req.ContentLength = int64(len(b)) | ||
req.Header.Set("Content-Type", "application/json") | ||
} | ||
|
||
if c.logger != nil { | ||
c.logger.WithContext(ctx).WithFields(logrus.Fields{ | ||
"http.request.method": req.Method, | ||
"http.request.url": req.URL.String(), | ||
"http.request.body.content": string(b), | ||
}).Debug("veriff.client -> request") | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("X-AUTH-CLIENT", c.token) | ||
return NewRequest(req), nil | ||
} | ||
|
||
func (c *client) do(ctx context.Context, req *request) error { | ||
resp, err := c.httpClient.Do(req.req) | ||
if err != nil { | ||
return fmt.Errorf("failed to send request: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
b, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
|
||
if c.logger != nil { | ||
c.logger.WithContext(ctx).WithFields(logrus.Fields{ | ||
"http.response.status_code": resp.StatusCode, | ||
"http.response.body.content": string(b), | ||
"http.response.headers": resp.Header, | ||
}).Debug("veriff.client -> response") | ||
} | ||
|
||
if req.decodeTo != nil { | ||
if err := json.Unmarshal(b, req.decodeTo); err != nil { | ||
return fmt.Errorf("failed to decode response: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module github.com/brokeyourbike/veriff-api-client-go | ||
|
||
go 1.21.5 | ||
|
||
require ( | ||
github.com/sirupsen/logrus v1.9.3 | ||
github.com/stretchr/testify v1.8.4 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/objx v0.5.1 // indirect | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= | ||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= | ||
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0= | ||
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= | ||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package veriff | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
// request is a wrapper around http.Request. | ||
type request struct { | ||
req *http.Request | ||
decodeTo interface{} | ||
} | ||
|
||
func NewRequest(r *http.Request) *request { | ||
return &request{req: r} | ||
} | ||
|
||
func (r *request) DecodeTo(to interface{}) { | ||
r.decodeTo = to | ||
} | ||
|
||
// AddQueryParam adds a query parameter to the request. | ||
func (r *request) AddQueryParam(key, value string) { | ||
r.AddQueryParams(map[string]string{key: value}) | ||
} | ||
|
||
// AddQueryParams adds multiple query parameters to the request. | ||
func (r *request) AddQueryParams(params map[string]string) { | ||
q := r.req.URL.Query() | ||
for _, k := range params { | ||
q.Add(k, params[k]) | ||
} | ||
r.req.URL.RawQuery = q.Encode() | ||
} |
Oops, something went wrong.