-
Notifications
You must be signed in to change notification settings - Fork 0
/
account-service.go
162 lines (140 loc) · 5.86 KB
/
account-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
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
162
package services
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/Alig1493/from3-accounts-modules/models"
"github.com/Alig1493/from3-accounts-modules/utils"
)
type AccountService interface {
// implement the Create, Fetch, and Delete operations on the accounts resource.
Create(accountData *models.Data) (*models.ResponseData, *models.ErrorData)
Fetch(accountId string) (*models.ResponseData, *models.ErrorData)
Delete(accountId string) (*models.ResponseData, *models.ErrorData)
}
type account struct{}
func getNewReuqest(methodtype string, url string, buffer *bytes.Buffer) (*http.Request, error) {
request, error := http.NewRequest(methodtype, url, buffer)
request.Header.Set("Content-Type", "application/vnd.api+json")
return request, error
}
func getClientRequest(request *http.Request) (*http.Response, error) {
client := &http.Client{}
response, repsonse_error := client.Do(request)
return response, repsonse_error
}
func NewAccountService() AccountService {
return &account{}
}
func (*account) Create(accountData *models.Data) (*models.ResponseData, *models.ErrorData) {
var (
buffer bytes.Buffer
errorData models.ErrorData
)
postUrl := utils.GetUrl() + "/v1/organisation/accounts"
encodingError := json.NewEncoder(&buffer).Encode(accountData)
if encodingError != nil {
log.Printf("Failed adding a new account: %v", encodingError)
return nil, &models.ErrorData{ErrorMessage: encodingError.Error(), StatusCode: http.StatusBadRequest}
}
request, requestError := http.NewRequest("POST", postUrl, &buffer)
request.Header.Set("Content-Type", "application/vnd.api+json")
if requestError != nil {
log.Printf("Failed to get a new request: %v", requestError)
return nil, &models.ErrorData{ErrorMessage: requestError.Error(), StatusCode: http.StatusBadRequest}
}
client := &http.Client{}
response, repsonseError := client.Do(request)
if repsonseError != nil {
log.Printf("Failed to get a response: %v", repsonseError)
return nil, &models.ErrorData{ErrorMessage: repsonseError.Error(), StatusCode: http.StatusBadRequest}
}
defer response.Body.Close()
log.Println("response Status:", response.StatusCode)
log.Println("response Headers:", response.Header)
bodyData, bodyReadError := ioutil.ReadAll(response.Body)
if bodyReadError != nil {
log.Printf("Failed to read the response body: %v", bodyReadError)
return nil, &models.ErrorData{ErrorMessage: repsonseError.Error(), StatusCode: http.StatusBadRequest}
}
log.Println("response Body:", string(bodyData))
if response.StatusCode != 201 {
json.Unmarshal(bodyData, &errorData)
return nil, &errorData
}
return &models.ResponseData{Response: accountData, StatusCode: response.StatusCode}, nil
}
func (*account) Fetch(accountId string) (*models.ResponseData, *models.ErrorData) {
fetchUrl := utils.GetUrl() + "/v1/organisation/accounts/" + accountId
var (
errorData models.ErrorData
accountData models.Data
)
request, requestError := http.NewRequest("GET", fetchUrl, nil)
request.Header.Add("Accept", "application/vnd.api+json")
if requestError != nil {
log.Printf("Failed to get a new request: %v", requestError)
return nil, &models.ErrorData{ErrorMessage: requestError.Error(), StatusCode: http.StatusBadRequest}
}
client := &http.Client{}
response, repsonseError := client.Do(request)
if repsonseError != nil {
log.Printf("Failed to get a response: %v", repsonseError)
return nil, &models.ErrorData{ErrorMessage: repsonseError.Error(), StatusCode: http.StatusBadRequest}
}
defer response.Body.Close()
log.Println("response Status:", response.StatusCode)
log.Println("response Headers:", response.Header)
bodyData, bodyError := ioutil.ReadAll(response.Body)
if bodyError != nil {
log.Printf("Failed to read the response body: %v", bodyData)
return nil, &models.ErrorData{ErrorMessage: bodyError.Error(), StatusCode: http.StatusBadRequest}
}
log.Println("response Body:", string(bodyData))
if response.StatusCode != 200 {
json.Unmarshal(bodyData, &errorData)
return nil, &errorData
}
bodyDataUnmarshalError := json.Unmarshal(bodyData, &accountData)
if bodyDataUnmarshalError != nil {
log.Printf("Failed to unmarshal body data: %v", bodyDataUnmarshalError)
return nil, &models.ErrorData{ErrorMessage: bodyDataUnmarshalError.Error(), StatusCode: http.StatusBadRequest}
}
return &models.ResponseData{Response: &accountData, StatusCode: response.StatusCode}, nil
}
func (*account) Delete(accountId string) (*models.ResponseData, *models.ErrorData) {
deleteUrl := utils.GetUrl() + "/v1/organisation/accounts/" + accountId
var errorData models.ErrorData
request, requestError := http.NewRequest("DELETE", deleteUrl, nil)
request.Header.Add("Accept", "application/vnd.api+json")
query := request.URL.Query()
query.Add("version", utils.GetVersion())
request.URL.RawQuery = query.Encode()
log.Println(request.URL.String())
if requestError != nil {
log.Printf("Failed to get a new request: %v", requestError)
return nil, &models.ErrorData{ErrorMessage: requestError.Error(), StatusCode: http.StatusBadRequest}
}
client := &http.Client{}
response, repsonseError := client.Do(request)
if repsonseError != nil {
log.Printf("Failed to get a response: %v", repsonseError)
return nil, &models.ErrorData{ErrorMessage: repsonseError.Error(), StatusCode: http.StatusBadRequest}
}
defer response.Body.Close()
log.Println("response Status:", response.StatusCode)
log.Println("response Headers:", response.Header)
bodyData, bodyError := ioutil.ReadAll(response.Body)
if bodyError != nil {
log.Printf("Failed to read the response body: %v", bodyError)
return nil, &models.ErrorData{ErrorMessage: bodyError.Error(), StatusCode: http.StatusBadRequest}
}
log.Println("response Body:", string(bodyData))
if response.StatusCode != 204 {
json.Unmarshal(bodyData, &errorData)
return nil, &errorData
}
return &models.ResponseData{Response: nil, StatusCode: response.StatusCode}, nil
}