-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermii_main.go
110 lines (89 loc) · 2.72 KB
/
termii_main.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
package termiigo
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
const (
defaultHttpTimeout = 60 * time.Second
baseUrl = "https://api.ng.termii.com/api/"
)
// TermiiConfig holds Termii API configuration, including API key and base URL.
type termiiServices struct {
client *Client
}
// Response represents arbitrary response data
type Response interface{}
// RequestValues aliased to url.Values as a workaround
type RequestValues url.Values
// MarshalJSON to handle custom JSON decoding for RequestValues
func (v RequestValues) MarshalJSON() ([]byte, error) {
m := make(map[string]interface{}, 3)
for k, val := range v {
m[k] = val[0]
}
return json.Marshal(m)
}
type Client struct {
common termiiServices // Reuse a single struct instead of allocating one for each service on the heap.
client *http.Client
// termii api key
apiKey string
// termii base url
baseUrl *url.URL
//Termii Services
SenderIDServiceR *SenderIDService
MessagingServiceR *MessagingService
NumberApiServiceR *NumberApiService
TemplateMessageServiceR *TemplateMessageService
CampaignServiceR *CampaignService
}
func NewClient(apiKey string, httpClient *http.Client) *Client {
//check if httpclient is nil
if httpClient == nil {
httpClient = &http.Client{Timeout: defaultHttpTimeout}
}
baseUrl, _ := url.Parse(baseUrl)
fmt.Println(baseUrl.String())
clientData := &Client{
client: http.DefaultClient,
apiKey: apiKey,
baseUrl: baseUrl,
}
fmt.Println(clientData)
clientData.common.client = clientData
clientData.SenderIDServiceR = (*SenderIDService)(&clientData.common)
clientData.MessagingServiceR = (*MessagingService)(&clientData.common)
clientData.NumberApiServiceR = (*NumberApiService)(&clientData.common)
clientData.TemplateMessageServiceR = (*TemplateMessageService)(&clientData.common)
clientData.CampaignServiceR = (*CampaignService)(&clientData.common)
return clientData
}
func MapToJSON(mapData interface{}) []byte {
jsonBytes, err := json.Marshal(mapData)
if err != nil {
panic(err)
}
return jsonBytes
}
func (clientVal *Client) MakeRequest(requestMethod string, data interface{}, url string) (error error, response map[string]interface{}) {
postData := MapToJSON(data)
requestUrl, _ := clientVal.baseUrl.Parse(url)
request, err := http.NewRequest(requestMethod, requestUrl.String(), bytes.NewBuffer(postData))
if err != nil {
return err, nil
}
// Set the request headers
request.Header.Set("Content-Type", "application/json")
// Perform the request
responseReceived, err := clientVal.client.Do(request)
if err != nil {
return err, nil
}
var result map[string]interface{}
json.NewDecoder(responseReceived.Body).Decode(&result)
return nil, result
}