-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
110 lines (82 loc) · 2.89 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
package sozlukcli
import (
"fmt"
"strconv"
"gopkg.in/resty.v0"
)
const baseURL = "http://sausozluk.net/service/proxy/api/v1"
const (
isTokenExistURL = baseURL + "/sessions/check"
isTopicExistURL = baseURL + "/search?q="
createTopicWithEntryURL = baseURL + "/topics"
createEntryIntoTopicURL = baseURL + "/entries"
deleteEntryURL = baseURL + "/entries"
createSessionURL = baseURL + "/sessions"
deleteSessionURL = baseURL + "/sessions"
)
func isTokenExistReq(token string) (*CheckResponse, error) {
resp, err := resty.R().
SetHeader("token", token).
SetResult(CheckResponse{}).
Post(isTokenExistURL)
response := resp.Result().(*CheckResponse)
return response, err
}
func isTopicExistReq(topic string, token string) (*SearchResponse, error) {
resp, err := resty.R().
SetHeader("token", token).
SetResult(SearchResponse{}).
Get(isTopicExistURL + topic)
response := resp.Result().(*SearchResponse)
return response, err
}
func createTopicWithEntryReq(topic string, entry string, token string) (*TopicCreateResponse, error) {
payload := fmt.Sprintf(`{"topic": {"title": "%s"}, "entry": {"text": "%s"}}`, topic, entry)
resp, err := resty.R().
SetHeader("token", token).
SetHeader("Content-Type", "application/json").
SetBody(payload).
SetResult(TopicCreateResponse{}).
Post(createTopicWithEntryURL)
response := resp.Result().(*TopicCreateResponse)
return response, err
}
func createEntryIntoTopicReq(topicID string, entry string, token string) (*EntryCreateResponse, error) {
payload := fmt.Sprintf(`{"topic_id": "%s", "text": "%s"}`, topicID, entry)
resp, err := resty.R().
SetHeader("token", token).
SetHeader("Content-Type", "application/json").
SetBody(payload).
SetResult(EntryCreateResponse{}).
Post(createEntryIntoTopicURL)
response := resp.Result().(*EntryCreateResponse)
return response, err
}
func deleteEntryReq(ID int, token string) (*EntryDeleteResponse, error) {
resp, err := resty.R().
SetHeader("token", token).
SetHeader("Content-Type", "application/json").
SetResult(EntryDeleteResponse{}).
Delete(deleteEntryURL + "/" + strconv.Itoa(ID))
response := resp.Result().(*EntryDeleteResponse)
return response, err
}
func createSessionReq(email string, password string) (*SessionCreateResponse, error) {
payload := fmt.Sprintf(`{"email":"%s","password":"%s"}`, email, password)
resp, err := resty.R().
SetHeader("Content-Type", "application/json").
SetBody(payload).
SetResult(SessionCreateResponse{}).
Post(createSessionURL)
response := resp.Result().(*SessionCreateResponse)
return response, err
}
func deleteSessionReq(token string) (*SessionDeleteResponse, error) {
resp, err := resty.R().
SetHeader("token", token).
SetHeader("Content-Type", "application/json").
SetResult(SessionDeleteResponse{}).
Delete(deleteSessionURL)
response := resp.Result().(*SessionDeleteResponse)
return response, err
}