Skip to content

Commit 57c3b21

Browse files
committed
Scafolding
1 parent 9569d55 commit 57c3b21

15 files changed

+493
-2
lines changed

.github/workflows/main.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Build
2+
3+
'on':
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
Validate:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
15+
- name: Setup Go
16+
uses: actions/setup-go@v2
17+
18+
- name: Setup Dependencies
19+
run: |
20+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sudo sh -s -- -b $GOPATH/bin v1.24.0
21+
golangci-lint --version
22+
go get golang.org/x/tools/cmd/cover
23+
go get -t -v ./...
24+
25+
- name: Golang CI Lint
26+
run: golangci-lint run
27+
28+
Test:
29+
runs-on: ubuntu-latest
30+
steps:
31+
- name: Checkout
32+
uses: actions/checkout@v2
33+
34+
- name: Setup Go
35+
uses: actions/setup-go@v2
36+
37+
- name: Setup Dependencies
38+
run: |
39+
go get golang.org/x/tools/cmd/cover
40+
go get -t -v ./...
41+
42+
- name: Run Tests
43+
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic
44+
45+
- name: Upload coverage to Codecov
46+
run: bash <(curl -s https://codecov.io/bash)

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
*.out
1313

1414
# Dependency directories (remove the comment below to include it)
15-
# vendor/
15+
vendor/
16+
$path

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/flutterwave-go.iml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.pre-commit-config.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
repos:
2+
- repo: https://github.com/tekwizely/pre-commit-golang
3+
rev: master
4+
hooks:
5+
- id: go-fumpt
6+
- id: go-mod-tidy
7+
- id: go-lint
8+
- id: go-imports
9+
- repo: https://github.com/pre-commit/pre-commit-hooks
10+
rev: v2.3.0
11+
hooks:
12+
- id: check-yaml
13+
- id: end-of-file-fixer
14+
- id: trailing-whitespace

README.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,95 @@
11
# flutterwave-go
2-
Unofficial Go client for the flutterwave API
2+
3+
[![Build](https://github.com/NdoleStudio/flutterwave-go/actions/workflows/main.yml/badge.svg)](https://github.com/NdoleStudio/flutterwave-go/actions/workflows/main.yml)
4+
[![codecov](https://codecov.io/gh/NdoleStudio/flutterwave-go/branch/main/graph/badge.svg)](https://codecov.io/gh/NdoleStudio/flutterwave-go)
5+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/NdoleStudio/flutterwave-go/badges/quality-score.png?b=main)](https://scrutinizer-ci.com/g/NdoleStudio/flutterwave-go/?branch=main)
6+
[![Go Report Card](https://goreportcard.com/badge/github.com/NdoleStudio/flutterwave-go)](https://goreportcard.com/report/github.com/NdoleStudio/flutterwave-go)
7+
[![GitHub contributors](https://img.shields.io/github/contributors/NdoleStudio/flutterwave-go)](https://github.com/NdoleStudio/flutterwave-go/graphs/contributors)
8+
[![GitHub license](https://img.shields.io/github/license/NdoleStudio/flutterwave-go?color=brightgreen)](https://github.com/NdoleStudio/flutterwave-go/blob/master/LICENSE)
9+
[![PkgGoDev](https://pkg.go.dev/badge/github.com/NdoleStudio/flutterwave-go)](https://pkg.go.dev/github.com/NdoleStudio/flutterwave-go)
10+
11+
12+
This package provides a `go` client for interacting with the [Flutterwave API](https://developer.flutterwave.com/docs)
13+
14+
## Installation
15+
16+
`flutterwave-go` is compatible with modern Go releases in module mode, with Go installed:
17+
18+
```bash
19+
go get github.com/NdoleStudio/flutterwave-go
20+
```
21+
22+
Alternatively the same can be achieved if you use `import` in a package:
23+
24+
```go
25+
import "github.com/NdoleStudio/flutterwave-go"
26+
```
27+
28+
## Implemented
29+
30+
- [Token](#token)
31+
- `POST /token` - Get access token
32+
- [Collect](#collect)
33+
- `POST /collect` - Request Payment
34+
- [Transaction](#transaction)
35+
- `POST /transaction/(reference)/` - Transaction Status
36+
37+
## Usage
38+
39+
### Initializing the Client
40+
41+
An instance of the `campay` client can be created using `New()`. The `http.Client` supplied will be used to make requests to the API.
42+
43+
```go
44+
package main
45+
46+
import (
47+
"github.com/NdoleStudio/flutterwave-go"
48+
)
49+
50+
func main() {
51+
flutterwaveClient := flutterwave.New(
52+
flutterwave.WithSecretKey("" /* flutterwave Secret Key */),
53+
)
54+
}
55+
```
56+
57+
### Error handling
58+
59+
All API calls return an `error` as the last return object. All successful calls will return a `nil` error.
60+
61+
```go
62+
data, httpResponse, err := campayClient.Bills.Create(context.Background(), request)
63+
if err != nil {
64+
//handle error
65+
}
66+
```
67+
68+
### BILLS
69+
70+
#### Create a bill payment
71+
72+
`POST /bills/`: Create a bill payment
73+
74+
```go
75+
response, _, err := flutterwaveClient.Bills.Create(context.Background(), request)
76+
77+
if err != nil {
78+
log.Fatal(err)
79+
}
80+
81+
log.Println(response.Status) // success
82+
```
83+
84+
85+
## Testing
86+
87+
You can run the unit tests for this client from the root directory using the command below:
88+
89+
```bash
90+
go test -v
91+
```
92+
93+
## License
94+
95+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

client.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package flutterwave
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"io/ioutil"
10+
"net/http"
11+
)
12+
13+
type service struct {
14+
client *Client
15+
}
16+
17+
// Client is the campay API client.
18+
// Do not instantiate this client with Client{}. Use the New method instead.
19+
type Client struct {
20+
httpClient *http.Client
21+
common service
22+
secretKey string
23+
baseURL string
24+
}
25+
26+
// New creates and returns a new campay.Client from a slice of campay.ClientOption.
27+
func New(options ...ClientOption) *Client {
28+
config := defaultClientConfig()
29+
30+
for _, option := range options {
31+
option.apply(config)
32+
}
33+
34+
client := &Client{
35+
httpClient: config.httpClient,
36+
secretKey: config.secretKey,
37+
baseURL: config.baseURL,
38+
}
39+
40+
client.common.client = client
41+
return client
42+
}
43+
44+
// newRequest creates an API request. A relative URL can be provided in uri,
45+
// in which case it is resolved relative to the BaseURL of the Client.
46+
// URI's should always be specified without a preceding slash.
47+
func (client *Client) newRequest(ctx context.Context, method, uri string, body interface{}) (*http.Request, error) {
48+
var buf io.ReadWriter
49+
if body != nil {
50+
buf = &bytes.Buffer{}
51+
enc := json.NewEncoder(buf)
52+
enc.SetEscapeHTML(false)
53+
err := enc.Encode(body)
54+
if err != nil {
55+
return nil, err
56+
}
57+
}
58+
59+
req, err := http.NewRequestWithContext(ctx, method, client.baseURL+uri, buf)
60+
if err != nil {
61+
return nil, err
62+
}
63+
64+
req.Header.Set("Content-Type", "application/json")
65+
req.Header.Set("Authorization", "Bearer "+client.secretKey)
66+
67+
return req, nil
68+
}
69+
70+
// do carries out an HTTP request and returns a Response
71+
func (client *Client) do(req *http.Request) (*Response, error) {
72+
if req == nil {
73+
return nil, fmt.Errorf("%T cannot be nil", req)
74+
}
75+
76+
httpResponse, err := client.httpClient.Do(req)
77+
if err != nil {
78+
return nil, err
79+
}
80+
81+
defer func() { _ = httpResponse.Body.Close() }()
82+
83+
resp, err := client.newResponse(httpResponse)
84+
if err != nil {
85+
return resp, err
86+
}
87+
88+
_, err = io.Copy(ioutil.Discard, httpResponse.Body)
89+
if err != nil {
90+
return resp, err
91+
}
92+
93+
return resp, nil
94+
}
95+
96+
// newResponse converts an *http.Response to *Response
97+
func (client *Client) newResponse(httpResponse *http.Response) (*Response, error) {
98+
if httpResponse == nil {
99+
return nil, fmt.Errorf("%T cannot be nil", httpResponse)
100+
}
101+
102+
resp := new(Response)
103+
resp.HTTPResponse = httpResponse
104+
105+
buf, err := ioutil.ReadAll(resp.HTTPResponse.Body)
106+
if err != nil {
107+
return nil, err
108+
}
109+
resp.Body = &buf
110+
111+
return resp, resp.Error()
112+
}

client_config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package flutterwave
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
type clientConfig struct {
8+
httpClient *http.Client
9+
secretKey string
10+
baseURL string
11+
}
12+
13+
func defaultClientConfig() *clientConfig {
14+
return &clientConfig{
15+
httpClient: http.DefaultClient,
16+
secretKey: "",
17+
baseURL: "https://api.flutterwave.com",
18+
}
19+
}

0 commit comments

Comments
 (0)