/
fancyhands.go
142 lines (114 loc) · 4.54 KB
/
fancyhands.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
package fancyhands
import (
"io/ioutil"
"net/http"
"strconv"
"time"
"github.com/mrjones/oauth"
)
const (
api_host = "https://www.fancyhands.com/api/v1/"
STATUS_NEW = 1
STATUS_OPEN = 5
STATUS_CLOSED = 20
STATUS_EXPIRED = 21
)
type Client struct {
test bool
oauth *oauth.Consumer
}
// Create a new Client object
func createClient(key string, secret string, test bool) *Client {
return &Client{
test: test,
oauth: oauth.NewConsumer(key, secret, oauth.ServiceProvider{}),
}
}
// Create an API Client
func NewClient(key string, secret string) *Client {
return createClient(key, secret, false)
}
// Create a test API Client. This will send API calls without actually
// sending tasks to Fancy Hands assistants.
func NewTestClient(key string, secret string) *Client {
return createClient(key, secret, true)
}
// Send a string to the API and it will echo it back.
func (c *Client) Echo(value string) (code int, body string, err error) {
return c.get("echo", map[string]string{"echo": value})
}
// Get all tasks
func (c *Client) GetAllTasks() (code int, body string, err error) {
return c.get("request/custom", map[string]string{})
}
// Get a specific task based on its key
func (c *Client) GetTask(key string) (code int, body string, err error) {
return c.get("request/custom", map[string]string{"key": key})
}
// Get tasks filtered by status. Use the predefined status constants (fancyhands.STATUS_OPEN, etc).
func (c *Client) GetTasksByStatus(status int) (code int, body string, err error) {
return c.get("request/custom", map[string]string{"status": strconv.Itoa(status)})
}
// Get tasks filtered by cursor. The API may return a cursor for pagination.
func (c *Client) GetTasksByCursor(cursor string) (code int, body string, err error) {
return c.get("request/custom", map[string]string{"cursor": cursor})
}
// Create a task. All fields are required.
func (c *Client) CreateTask(title string, desc string, bid float64, expiration time.Time) (code int, body string, err error) {
return c.CreateCustomTask(title, desc, bid, expiration, "")
}
// Create a custom task. The custom field must be a string formatted as JSON.
func (c *Client) CreateCustomTask(title string, desc string, bid float64, expiration time.Time, custom string) (code int, body string, err error) {
// TODO: It would be nice to create a struct for 'custom'. Something like: map[int]map[string]string
data := make(map[string]string)
data["title"] = title
data["description"] = desc
data["bid"] = strconv.FormatFloat(bid, 'f', 2, 64)
data["expiration_date"] = expiration.Format(time.RFC3339)
if custom != "" {
data["custom_fields"] = custom
}
return c.post("request/custom", data)
}
// Cancel a task based on its key
func (c *Client) CancelTask(key string) (code int, body string, err error) {
return c.post("request/custom/cancel", map[string]string{"key": key})
}
// Add a message to a specific task based on the task's key.`
func (c *Client) CreateMessage(key string, message string) (code int, body string, err error) {
return c.post("request/standard/messages", map[string]string{"key": key, "message": message})
}
// Create a task for a phone call. The conversation field must be a string formatted as JSON.
func (c *Client) Call(phone string, conversation string) (code int, body string, err error) {
return c.post("call", map[string]string{"phone": phone, "conversation": conversation})
}
// Send a POST request to the API
func (c *Client) post(url string, data map[string]string) (code int, body string, err error) {
return c.request(url, data, "post")
}
// Send a GET request to the API
func (c *Client) get(url string, data map[string]string) (code int, body string, err error) {
return c.request(url, data, "get")
}
// Send a request to the API
func (c *Client) request(url string, data map[string]string, method string) (code int, body string, err error) {
var response *http.Response
var newErr error
var response_body []byte
if c.test == true {
data["test"] = "true"
}
if method == "get" {
response, newErr = c.oauth.Get(api_host + url, data, &oauth.AccessToken{})
} else if method == "post" {
response, newErr = c.oauth.Post(api_host + url, data, &oauth.AccessToken{})
}
if newErr != nil {
return 0, "", newErr
}
response_body, newErr = ioutil.ReadAll(response.Body)
if newErr != nil {
return 0, "", newErr
}
return response.StatusCode, string(response_body), nil
}