1 package com.srv4pos.server.api.tax;
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 TaxService {
25 private URL url;
26 private Credentials credentials;
27 private Transport transport;
28
29
30
31
32
33
34
35
36 public TaxService(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
54 public TaxInfo get(Integer version, String identifier) {
55 StringResponse response = requestString(
56 format("%s/taxes/%s", credentials.getSellerId(), identifier),
57 HttpMethod.GET,
58 url,
59 credentials.toAuthentication(),
60 HttpHelper.integerToETag(version),
61 null,
62 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
63 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
64 transport
65 );
66 return response == null ? null : TaxInfo.fromJsonToTaxInfo(response.getContent());
67 }
68
69
70
71
72
73
74
75
76
77 public void put(int version, TaxInfo taxInfo) {
78 requestVoid(
79 format("%s/taxes/%s", credentials.getSellerId(), taxInfo.getIdentifier()),
80 HttpMethod.PUT,
81 url,
82 credentials.toAuthentication(),
83 HttpHelper.integerToETag(version),
84 null,
85 new StringConnectionWriter(taxInfo.toJson()),
86 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
87 transport
88 );
89 }
90
91
92
93
94
95
96
97
98
99
100 public IdentifierInfo create(int version, TaxInfo taxInfo) {
101 return IdentifierInfo.fromJsonToIdentifierInfo(
102 requestString(
103 format("%s/taxes", credentials.getSellerId()),
104 HttpMethod.POST,
105 url,
106 credentials.toAuthentication(),
107 HttpHelper.integerToETag(version),
108 null,
109 new StringConnectionWriter(taxInfo.toJson()),
110 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
111 transport
112 ).getContent()
113 );
114 }
115
116
117
118
119
120
121
122
123
124 public void delete(int version, String identifier) {
125 requestVoid(
126 format("%s/taxes/%s", credentials.getSellerId(), identifier),
127 HttpMethod.DELETE,
128 url,
129 credentials.toAuthentication(),
130 HttpHelper.integerToETag(version),
131 null,
132 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
133 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
134 transport
135 );
136 }
137
138
139
140
141
142
143
144
145 public List<TaxInfo> listDiff(int versionFrom, int versionTo) {
146 StringResponse response = requestString(
147 String.format("%s/taxes-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo),
148 HttpMethod.GET,
149 url,
150 credentials.toAuthentication(),
151 null,
152 null,
153 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
154 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
155 transport
156 );
157 return (List<TaxInfo>) TaxInfo.fromJsonArrayToTaxInfoes(response.getContent());
158 }
159
160
161
162
163
164
165
166 public List<TaxInfo> list(Integer version) {
167 return list(version, null, null, null, null, null);
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181 public List<TaxInfo> list(Integer version,
182 Integer firstResult,
183 Integer maxResults,
184 String like,
185 TaxInfo.Fields orderBy,
186 Boolean orderDesc) {
187 HashMap<String, String> params = new HashMap<String, String>();
188 if (orderBy != null) {
189 params.put("orderBy", orderBy.name());
190 }
191 if (orderDesc != null) {
192 params.put("orderDesc", orderDesc.toString());
193 }
194 if (firstResult != null) {
195 params.put("firstResult", Integer.toString(firstResult));
196 }
197 if (maxResults != null) {
198 params.put("maxResults", Integer.toString(maxResults));
199 }
200 if (like != null) {
201 params.put("like", like);
202 }
203 StringResponse response = requestString(
204 format("%s/taxes", credentials.getSellerId()),
205 HttpMethod.GET,
206 url,
207 credentials.toAuthentication(),
208 HttpHelper.integerToETag(version),
209 params,
210 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
211 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
212 transport
213 );
214 return response == null ? null : (List<TaxInfo>) TaxInfo.fromJsonArrayToTaxInfoes(response.getContent());
215 }
216 }