1 package com.srv4pos.server.api.treasurer;
2
3 import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
4 import com.srv4pos.server.api.infrastructure.HttpMethod;
5 import com.srv4pos.server.api.infrastructure.IdentifierInfo;
6 import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
7 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
8 import com.srv4pos.server.api.infrastructure.http.HttpHelper;
9 import com.srv4pos.server.api.infrastructure.http.Transport;
10 import com.srv4pos.server.api.infrastructure.request.StringResponse;
11
12 import java.net.URL;
13 import java.util.HashMap;
14 import java.util.List;
15
16 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
17 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
18 import static java.lang.String.format;
19
20
21
22
23
24 public class TreasurerService {
25 private URL url;
26 private Credentials credentials;
27 private Transport transport;
28
29
30
31
32
33
34
35
36 public TreasurerService(URL url, Credentials credentials, Transport transport) {
37 this.url = url;
38 this.credentials = credentials;
39 this.transport = transport;
40 }
41
42
43
44
45
46
47
48
49
50
51
52
53 public TreasurerInfo get(Integer version, String identifier) {
54 StringResponse response = requestString(
55 format("%s/treasurers/%s", credentials.getSellerId(), identifier),
56 HttpMethod.GET,
57 url,
58 credentials.toAuthentication(),
59 HttpHelper.integerToETag(version),
60 null,
61 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
62 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
63 transport
64 );
65 return response == null ? null : TreasurerInfo.fromJsonToTreasurerInfo(response.getContent());
66 }
67
68
69
70
71
72
73
74
75
76 public void put(int version, TreasurerInfo treasurerInfo) {
77 requestVoid(
78 format("%s/treasurers/%s", credentials.getSellerId(), treasurerInfo.getIdentifier()),
79 HttpMethod.PUT,
80 url,
81 credentials.toAuthentication(),
82 HttpHelper.integerToETag(version),
83 null,
84 new StringConnectionWriter(treasurerInfo.toJson()),
85 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
86 transport
87 );
88 }
89
90
91
92
93
94
95
96
97
98
99 public IdentifierInfo create(int version, TreasurerInfo treasurerInfo) {
100 return IdentifierInfo.fromJsonToIdentifierInfo(
101 requestString(
102 format("%s/treasurers", credentials.getSellerId()),
103 HttpMethod.POST,
104 url,
105 credentials.toAuthentication(),
106 HttpHelper.integerToETag(version),
107 null,
108 new StringConnectionWriter(treasurerInfo.toJson()),
109 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
110 transport
111 ).getContent()
112 );
113 }
114
115
116
117
118
119
120
121
122
123
124
125 public void delete(int version, String identifier) {
126 requestVoid(
127 format("%s/treasurers/%s", credentials.getSellerId(), identifier),
128 HttpMethod.DELETE,
129 url,
130 credentials.toAuthentication(),
131 HttpHelper.integerToETag(version),
132 null,
133 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
134 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
135 transport
136 );
137 }
138
139
140
141
142
143
144
145
146 public List<TreasurerInfo> listDiff(int versionFrom, int versionTo) {
147 StringResponse response = requestString(
148 format("%s/treasurers-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo),
149 HttpMethod.GET,
150 url,
151 credentials.toAuthentication(),
152 null,
153 null,
154 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
155 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
156 transport
157 );
158 return (List<TreasurerInfo>) TreasurerInfo.fromJsonArrayToTreasurerInfoes(response.getContent());
159 }
160
161
162
163
164
165
166
167 public List<TreasurerInfo> list(Integer version) {
168 return list(version, null, null, null, null, null);
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182 public List<TreasurerInfo> list(Integer version,
183 Integer firstResult,
184 Integer maxResults,
185 String like,
186 TreasurerInfo.Fields orderBy,
187 Boolean orderDesc) {
188 HashMap<String, String> params = new HashMap<String, String>();
189 if (orderBy != null) {
190 params.put("orderBy", orderBy.name());
191 }
192 if (orderDesc != null) {
193 params.put("orderDesc", orderDesc.toString());
194 }
195 if (firstResult != null) {
196 params.put("firstResult", Integer.toString(firstResult));
197 }
198 if (maxResults != null) {
199 params.put("maxResults", Integer.toString(maxResults));
200 }
201 if (like != null) {
202 params.put("like", like);
203 }
204
205 StringResponse response = requestString(format("%s/treasurers",
206 credentials.getSellerId()),
207 HttpMethod.GET,
208 url,
209 credentials.toAuthentication(),
210 HttpHelper.integerToETag(version),
211 params,
212 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
213 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
214 transport);
215
216 return response == null ? null : (List<TreasurerInfo>) TreasurerInfo.fromJsonArrayToTreasurerInfoes(response.getContent());
217 }
218 }