-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
206 lines (159 loc) · 4.72 KB
/
client.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//********************************************************************************************************************//
//
// Copyright (C) 2018 - 2021 J&J Ideenschmiede GmbH <info@jj-ideenschmiede.de>
//
// This file is part of goerecht24.
// All code may be used. Feel free and maybe code something better.
//
// Author: Jonas Kwiedor (aka gowizzard)
//
//********************************************************************************************************************//
package goerecht24
import (
"encoding/json"
"fmt"
)
// ClientBody is to create or update a client
type ClientBody struct {
ClientId int `json:"client_id"`
PushMethod string `json:"push_method"`
PushUri string `json:"push_uri"`
Cms string `json:"cms"`
CmsVersion string `json:"cms_version"`
PluginName string `json:"plugin_name"`
AuthorMail string `json:"author_mail"`
}
// ListRegisteredClientsReturn is to decode the json data
type ListRegisteredClientsReturn struct {
ClientId int `json:"client_id"`
ProjectId int `json:"project_id"`
PushMethod string `json:"push_method"`
PushUri string `json:"push_uri"`
Cms string `json:"cms"`
CmsVersion string `json:"cms_version"`
PluginName string `json:"plugin_name"`
AuthorMail string `json:"author_mail"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
// RegisterClientReturn is to decode the json data
type RegisterClientReturn struct {
Secret string `json:"secret"`
ClientId int `json:"client_id"`
}
// UpdateRegisteredClientReturn is to decode the json data
type UpdateRegisteredClientReturn struct {
Secret string `json:"secret"`
}
// DeleteClientReturn is to decode the json data
type DeleteClientReturn struct {
Message string `json:"message"`
MessageDe string `json:"message_de"`
Token interface{} `json:"token,omitempty"`
}
// ListRegisteredClients is to get a list of all clients from eRecht24
func ListRegisteredClients(r Request) ([]ListRegisteredClientsReturn, error) {
// Set config for new request
c := Config{"/clients", "GET", nil}
// Send request
response, err := c.Send(r)
if err != nil {
return nil, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode []ListRegisteredClientsReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return nil, err
}
// Return data
return decode, err
}
// RegisterClient is to register client for push notifications
func RegisterClient(body ClientBody, r Request) (RegisterClientReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return RegisterClientReturn{}, err
}
// Set config for new request
c := Config{"/clients", "POST", convert}
// Send request
response, err := c.Send(r)
if err != nil {
return RegisterClientReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode RegisterClientReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return RegisterClientReturn{}, err
}
// Return data
return decode, err
}
// UpdateRegisteredClient is to update registered client
func UpdateRegisteredClient(body ClientBody, r Request) (UpdateRegisteredClientReturn, error) {
// Convert body
convert, err := json.Marshal(body)
if err != nil {
return UpdateRegisteredClientReturn{}, err
}
// Set config for new request
c := Config{fmt.Sprintf("/clients/%d", body.ClientId), "PUT", convert}
// Send request
response, err := c.Send(r)
if err != nil {
return UpdateRegisteredClientReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode UpdateRegisteredClientReturn
err = json.NewDecoder(response.Body).Decode(&decode)
if err != nil {
return UpdateRegisteredClientReturn{}, err
}
// Return data
return decode, err
}
// DeleteClient is to delete registered client
func DeleteClient(id int, r Request) (DeleteClientReturn, error) {
// Set config for new request
c := Config{fmt.Sprintf("/clients/%d", id), "DELETE", nil}
// Send request
response, err := c.Send(r)
if err != nil {
return DeleteClientReturn{}, err
}
// Close request
defer response.Body.Close()
// Decode data
var decode DeleteClientReturn
json.NewDecoder(response.Body).Decode(&decode)
// Return data
return decode, err
}
// TestPush is to send a push notification for development
func TestPush(id int, pushType string, r Request) error {
// Set config for new request
c := Config{fmt.Sprintf("/clients/%d/testPush?type=%s", id, pushType), "POST", nil}
// Send request
response, err := c.Send(r)
if err != nil {
return err
}
// Close request
defer response.Body.Close()
// Check response status
err = statusCodes(response.Status)
if err != nil {
return err
}
// Return nothing
return nil
}