-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmondo.go
161 lines (130 loc) · 3.67 KB
/
mondo.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
153
154
155
156
157
158
159
160
161
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
const (
Authorization = "Authorization"
Bearer = "Bearer "
ContentType = "Content-Type"
ApplicationJson = "application/json"
)
type MondoApiClient struct {
clientId string
clientSecret string
url string
}
type RegisterWebhookRequest struct {
AccountId string `json:"account_id"`
Url string `json:"url"`
}
type Webhook struct {
AccountId string `json:"account_id"`
Id string `json:"id"`
Url string `json:"url"`
}
type RegisterWebhookResponse struct {
Webhook Webhook `json:"webhook"`
}
type WebhookRequest struct {
Type string `json:"type"`
Data WebhookData `json:"data"`
}
type WebhookData struct {
Amount int32 `json:"amount"`
Created string `json:"created"`
Currency string `json:"currency"`
Description string `json:"description"`
Id string `json:"id"`
}
var httpClient = &http.Client{}
func (c *MondoApiClient) RegisterWebHook(accessToken, accountId, webhookUrl string) (*RegisterWebhookResponse, error) {
log.Printf("Registering webhook for accountId=%s accessToken=%s url=%s\n", accountId, accessToken, webhookUrl)
webhooksUrl := fmt.Sprintf("%s/webhooks", c.url)
formValues := url.Values{
"account_id": {accountId},
"url": {webhookUrl},
}
request, err := http.NewRequest("POST", webhooksUrl, strings.NewReader(formValues.Encode()))
if err != nil {
return nil, err
}
request.Header.Add(Authorization, Bearer+accessToken)
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, err := httpClient.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return nil, errors.New(string(body))
}
webhookResponse := &RegisterWebhookResponse{}
err = json.NewDecoder(response.Body).Decode(webhookResponse)
if err != nil {
return nil, err
}
return webhookResponse, nil
}
func (c *MondoApiClient) UnregisterWebHook(accessToken, webhookId string) error {
log.Printf("Unregistering webhook accessToken=%s webhookId=%s\n", accessToken, webhookId)
webhooksUrl := fmt.Sprintf("%s/webhooks/%s", c.url, webhookId)
request, err := http.NewRequest("DELETE", webhooksUrl, nil)
if err != nil {
return err
}
request.Header.Add(Authorization, Bearer+accessToken)
response, err := httpClient.Do(request)
if err != nil {
return err
}
if response.StatusCode != 200 {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
return errors.New(string(body))
}
return nil
}
func (c *MondoApiClient) CreateFeedItem(accessToken, accountId, itemType, title, imageUrl, body string) error {
log.Printf("Creating feed item for accountId=%s accessToken=%s type=%s title=%s imageUrl=%s body=%s\n", accountId, accessToken, itemType, title, imageUrl, body)
feedUrl := fmt.Sprintf("%s/feed", c.url)
formValues := url.Values{
"account_id": {accountId},
"type": {itemType},
"title": {title},
"image_url": {imageUrl},
"body": {body},
}
request, err := http.NewRequest("POST", feedUrl, strings.NewReader(formValues.Encode()))
if err != nil {
return err
}
request.Header.Add(Authorization, Bearer+accessToken)
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, err := httpClient.Do(request)
if err != nil {
return err
}
if response.StatusCode != 200 {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
return errors.New(string(body))
}
return err
}