1
|
|
|
package flutterwave |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"errors" |
7
|
|
|
"net/http" |
8
|
|
|
"strconv" |
9
|
|
|
) |
10
|
|
|
|
11
|
|
|
// transfersService is the API client for the `/v3/transfers` endpoint |
12
|
|
|
type transfersService service |
13
|
|
|
|
14
|
|
|
// Estimate the Transfer Rate of a transaction |
15
|
|
|
// |
16
|
|
|
// API Docs: https://developer.flutterwave.com/reference/check-transfer-rates |
17
|
|
|
func (service *transfersService) Rate(ctx context.Context, amount int, destination_currency, source_currency string) (*TransferRateResponse, *Response, error) { |
18
|
|
|
uri := "/v3/transfers/rates" |
19
|
|
|
|
20
|
|
|
request, err := service.client.newRequest(ctx, http.MethodGet, uri, nil) |
21
|
|
|
if err != nil { |
22
|
|
|
return nil, nil, errors.Join(ErrCouldNotConstructNewRequest, err) |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// Adding the parameters |
26
|
|
|
requestWithParams := service.client.addURLParams(request, map[string]string{ |
27
|
|
|
"amount": strconv.Itoa(amount), |
28
|
|
|
"destination_currency": destination_currency, |
29
|
|
|
"source_currency": source_currency, |
30
|
|
|
}) |
31
|
|
|
|
32
|
|
|
response, err := service.client.do(requestWithParams) |
33
|
|
|
if err != nil { |
34
|
|
|
return nil, response, errors.Join(ErrRequestFailure, err) |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
var data TransferRateResponse |
38
|
|
|
if err = json.Unmarshal(*response.Body, &data); err != nil { |
39
|
|
|
return nil, response, errors.Join(ErrUnmarshalFailure, err) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return &data, response, nil |
43
|
|
|
|
44
|
|
|
} |