-
Notifications
You must be signed in to change notification settings - Fork 1
/
gomono.go
152 lines (129 loc) · 3.27 KB
/
gomono.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2020 Joseph Cobhams. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE.md file.
//
package gomono
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"reflect"
"time"
)
type (
Gomono interface {
ExchangeToken(code string) (string, error)
Information(id string) (*InformationResponse, error)
Statement(id, period, output string) (*StatementResponse, error)
PdfStatementJobStatus(id, jobId string) (*StatementResponsePdf, error)
Transactions(id, start, end, narration, tnxType string, paginate bool) (*TransactionsResponse, error)
CreditTransactions(id string) (*TransactionByTypeResponse, error)
DebitTransactions(id string) (*TransactionByTypeResponse, error)
Income(id string) (*IncomeResponse, error)
Identity(id string) (*IdentityResponse, error)
Institutions() (*InstitutionsResponse, error)
LookupBVN(bvn string) (*IdentityResponse, error)
}
gomono struct {
secretKey string
client *http.Client
apiUrl string
}
Error struct {
Code int
Body string
Endpoint string
}
Config struct {
SecretKey string
HttpClient *http.Client
ApiUrl string
}
header struct {
Key string
Value string
}
)
func New(cfg Config) (Gomono, error) {
if err := validateConfig(&cfg); err != nil {
return nil, err
}
g := &gomono{
secretKey: cfg.SecretKey,
client: cfg.HttpClient,
apiUrl: cfg.ApiUrl,
}
return g, nil
}
func NewDefaultConfig(secretKey string) Config {
return Config{
SecretKey: secretKey,
HttpClient: &http.Client{
Timeout: 5 * time.Second,
},
ApiUrl: "https://api.withmono.com",
}
}
func validateConfig(cfg *Config) error {
if cfg.SecretKey == "" {
return errors.New("gomono: Missing Secret Key")
}
if cfg.HttpClient == nil {
return errors.New("gomono: HTTP Client Cannot Be Nil")
}
if cfg.ApiUrl == "" {
return errors.New("gomono: Missing API Url")
}
return nil
}
func (g *gomono) preparePayload(body interface{}) (io.Reader, error) {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
func (g *gomono) makeRequest(method, url string, body io.Reader, headers []header, responseTarget interface{}) error {
if reflect.TypeOf(responseTarget).Kind() != reflect.Ptr {
return errors.New("gomono: responseTarget must be a pointer to a struct for JSON unmarshalling")
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return err
}
for _, h := range headers {
req.Header.Set(h.Key, h.Value)
}
req.Header.Set("mono-sec-key", g.secretKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Client-Lib", "GoMono | v1 | github.com/jcobhams/gomono")
resp, err := g.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode == 200 || resp.StatusCode == 201 {
err = json.Unmarshal(b, responseTarget)
if err != nil {
return err
}
return nil
}
err = Error{
Code: resp.StatusCode,
Body: string(b),
Endpoint: req.URL.String(),
}
return err
}
func (e Error) Error() string {
return fmt.Sprintf("Request To %v Endpoint Failed With Status Code %v | Body: %v", e.Endpoint, e.Code, e.Body)
}