| Total Lines | 48 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | package client |
||
| 2 | |||
| 3 | import ( |
||
| 4 | "fmt" |
||
| 5 | |||
| 6 | "github.com/evalphobia/httpwrapper/request" |
||
| 7 | ) |
||
| 8 | |||
| 9 | // Client is http client for minFraud API. |
||
| 10 | type Client struct { |
||
| 11 | Option |
||
| 12 | MinFraudAccountID string |
||
| 13 | MinFraudLicenseKey string |
||
| 14 | } |
||
| 15 | |||
| 16 | func New() *Client { |
||
| 17 | return &Client{} |
||
| 18 | } |
||
| 19 | |||
| 20 | func (c *Client) SetAuthData(accountID, licenseKey string) { |
||
| 21 | c.MinFraudAccountID = accountID |
||
| 22 | c.MinFraudLicenseKey = licenseKey |
||
| 23 | } |
||
| 24 | |||
| 25 | func (c *Client) SetOption(opt Option) { |
||
| 26 | c.Option = opt |
||
| 27 | } |
||
| 28 | |||
| 29 | // CallPOST sends POST request to `url` with `params` and set reqponse to `result` |
||
| 30 | func (c *Client) CallPOST(path string, params, result interface{}) (err error) { |
||
| 31 | opt := c.Option |
||
| 32 | url := fmt.Sprintf("%s%s", opt.getBaseURL(), path) |
||
| 33 | |||
| 34 | resp, err := request.POST(url, request.Option{ |
||
| 35 | Payload: params, |
||
| 36 | PayloadType: request.PayloadTypeJSON, |
||
| 37 | User: c.MinFraudAccountID, |
||
| 38 | Pass: c.MinFraudLicenseKey, |
||
| 39 | Retry: opt.Retry, |
||
| 40 | Debug: opt.Debug, |
||
| 41 | UserAgent: opt.getUserAgent(), |
||
| 42 | Timeout: opt.getTimeout(), |
||
| 43 | }) |
||
| 44 | if err != nil { |
||
| 45 | return err |
||
| 46 | } |
||
| 47 | err = resp.JSON(result) |
||
| 48 | return err |
||
| 49 | } |
||
| 50 |