-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.go
123 lines (113 loc) · 3.27 KB
/
service.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
package api
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"reflect"
"text/template"
"github.com/exotel/goapi/assets"
"github.com/exotel/goapi/assets/types"
"github.com/exotel/goapi/helpers"
)
//IsValidAction Checks if there are enough details for the execution
//It checks for if an action is set and if set is it a valid action on that resources
//For example if a resource does not accept POST then action being POST would return error
func (c Client) IsValidAction() (ok bool, err error) {
ok = c.action&c.validActions != 0x00
if !ok {
if c.action == types.Action(0) {
err = fmt.Errorf(assets.String.MethodNotSpecified)
}
err = fmt.Errorf(assets.String.UnsupportedMethod, c.action)
}
return
}
//IsValidCredentials Checks if the available credentials are valid or not
//checks if credentials are provided ,it does not check if the provided credentials are valid or not
func (c Client) IsValidCredentials() (ok bool, err error) {
ok = len(c.Credentials.UserName) != 0 && len(c.Credentials.AccessToken) != 0
if !ok {
err = errors.New("ACCESS_TOKEN and USERNAME are mandatory")
}
return
}
//IsValidData checks if the data is valid or not
//It uses the tag `mandatory` to see if a field is mandatory or not
func (c Client) IsValidData() (ok bool, err error) {
v := reflect.ValueOf(c.data)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
//If not a struct its a problem now every data has to be a Struct
if v.Kind() != reflect.Struct {
err = fmt.Errorf(assets.String.PackageInconsistancy+" [only accepts structs; got (%+[1]v)%[1]T]", v)
return
}
//Now pass through all the values and make sure all the mandatory fields are set
tp := v.Type()
for i := 0; i < tp.NumField(); i++ {
field := tp.Field(i)
emptyValue := helpers.IsEmptyValue(v.Field(i))
if emptyValue {
if tagv := field.Tag.Get("mandatory"); tagv == "true" {
err = fmt.Errorf(assets.String.MandatoryFieldUnavailable, field.Name)
return
}
}
}
ok = true
return
}
//setURL sets the url for the request according to the data given
func (c *Client) setURL() (err error) {
var parsedTemplate *template.Template
parsedTemplate, err = template.New("url").Parse(c.url)
if err != nil {
return
}
//create the hash map for filling template from client and data
var urlParamsMap map[string]interface{}
urlParamsMap, err = helpers.StructToMap("url", c, c.data)
if err != nil {
return
}
//fill the template with proper values
var b bytes.Buffer
err = parsedTemplate.Execute(&b, urlParamsMap)
if err != nil {
return
}
c.url = string(b.Bytes())
return
}
//Do does the actual job
func (c *Client) Do() (status int, result map[string]interface{}, err error) {
var ok bool
//Check if the credentials are provided
if ok, err = c.IsValidCredentials(); !ok {
return
}
//Check if the action requested is valid or not
if ok, err = c.IsValidAction(); !ok {
return
}
//Checking if the data is valid
if ok, err = c.IsValidData(); !ok {
return
}
//forming the url to make request
err = c.setURL()
if err != nil {
return
}
url := c.baseURL + c.url
var bresult []byte
status, bresult, err = helpers.MakeHTTPRequest(url, c.Credentials, c.action, c.data, c.mode == types.DEBUG)
if err != nil {
return
}
result = make(map[string]interface{})
err = json.Unmarshal(bresult, &result)
return
}