This repository was archived by the owner on Oct 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.go
More file actions
114 lines (92 loc) · 2.56 KB
/
command.go
File metadata and controls
114 lines (92 loc) · 2.56 KB
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
package payprogo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httputil"
)
// Command is used to execute commands against the PayPro API
type Command struct {
url string
command string
key string
verbose bool
parameters map[string]interface{}
}
// Set sets a parameter like 'consumer_email'
func (c *Command) Set(key string, value interface{}) *Command {
c.parameters[key] = value
return c
}
func (c *Command) paramsAsJSON() []byte {
b, _ := json.Marshal(c.parameters)
return b
}
// RawExecute can be used to get the raw unparsed http.Response.
// Remember to close the response body when using this function.
func (c *Command) RawExecute() (*http.Response, error) {
var postBytes bytes.Buffer
w := multipart.NewWriter(&postBytes)
fw, _ := w.CreateFormField("apikey")
fw.Write([]byte(c.key))
fw, _ = w.CreateFormField("command")
fw.Write([]byte(c.command))
fw, _ = w.CreateFormField("params")
fw.Write(c.paramsAsJSON())
w.Close()
req, err := http.NewRequest("POST", c.url, &postBytes)
if err != nil {
return nil, err
}
// set the content-type
req.Header.Set("Content-Type", w.FormDataContentType())
if c.verbose {
b, _ := httputil.DumpRequest(req, true)
fmt.Print(string(b) + "\n")
}
// Submit the request
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, err
}
if c.verbose {
b, _ := httputil.DumpResponse(res, true)
fmt.Print(string(b) + "\n")
}
req.Body.Close()
// Check the response
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("expected status 200 got: %s", res.Status)
}
return res, nil
}
// Execute will transform the input the same way json.Unmarshal does.
// It takes a pointer to a variable and will read the response json.
// If the api returns an error this is returned as a normal go error
func (c *Command) Execute(x interface{}) error {
resp, err := c.RawExecute()
if err != nil {
return err
}
// Copy the response body in buffer to re-read the contents
buff := &bytes.Buffer{}
io.Copy(buff, resp.Body)
resp.Body.Close()
// check for error first, this will remove the need for error handling
// in custom response types(for now only Payment but this could be used
// for Sales etc..)
var apiErr apiError
if err = json.Unmarshal(buff.Bytes(), &apiErr); err == nil && apiErr.Errors == true {
return errors.New("Error from API:" + apiErr.Message)
}
err = json.Unmarshal(buff.Bytes(), x)
if err != nil {
return fmt.Errorf("Execute error: %s for response:%s", err, string(buff.Bytes()))
}
return nil
}