1 package com.srv4pos.server.api.keyboard;
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
19
20
21
22 public class KeyboardService {
23 private URL url;
24 private Credentials credentials;
25 private Transport transport;
26
27
28
29
30
31
32
33
34 public KeyboardService(URL url, Credentials credentials, Transport transport) {
35 this.url = url;
36 this.credentials = credentials;
37 this.transport = transport;
38 }
39
40
41
42
43
44
45
46
47
48
49
50 public KeyboardInfo get(Integer version, String identifier) {
51 StringResponse response = requestString(credentials.getSellerId() + "/keyboards/" + identifier, HttpMethod.GET, url, credentials.toAuthentication(),
52 HttpHelper.integerToETag(version), null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
53 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
54 return response == null ? null : KeyboardInfo.fromJsonToKeyboardInfo(response.getContent());
55 }
56
57
58
59
60
61
62
63
64 public void put(int version, KeyboardInfo keyboardInfo) {
65 requestVoid(credentials.getSellerId() + "/keyboards/" + keyboardInfo.getIdentifier(), HttpMethod.PUT, url, credentials.toAuthentication(),
66 HttpHelper.integerToETag(version), null, new StringConnectionWriter(keyboardInfo.toJson()),
67 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
68 }
69
70
71
72
73
74
75
76
77
78 public IdentifierInfo create(int version, KeyboardInfo keyboardInfo) {
79 return IdentifierInfo
80 .fromJsonToIdentifierInfo(requestString(credentials.getSellerId() + "/keyboards", HttpMethod.POST, url, credentials.toAuthentication(),
81 HttpHelper.integerToETag(version), null, new StringConnectionWriter(keyboardInfo.toJson()),
82 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport).getContent());
83 }
84
85
86
87
88
89
90
91
92 public void delete(int version, String identifier) {
93 requestVoid(credentials.getSellerId() + "/keyboards/" + identifier, HttpMethod.DELETE, url, credentials.toAuthentication(),
94 HttpHelper.integerToETag(version), null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
95 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
96 }
97
98
99
100
101
102
103
104
105 public List<KeyboardInfo> listDiff(int versionFrom, int versionTo) {
106 StringResponse response = requestString(String.format("%s/keyboards-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo),
107 HttpMethod.GET, url, credentials.toAuthentication(), null, null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
108 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
109 return (List<KeyboardInfo>) KeyboardInfo.fromJsonArrayToKeyboardInfoes(response.getContent());
110 }
111
112
113
114
115
116
117
118 public List<KeyboardInfo> list(Integer version) {
119 return list(version, null, null, null, null, null);
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 public List<KeyboardInfo> list(Integer version,
134 Integer firstResult,
135 Integer maxResults,
136 String like,
137 KeyboardInfo.Fields orderBy,
138 Boolean orderDesc) {
139 HashMap<String, String> params = new HashMap<String, String>();
140 if (orderBy != null) {
141 params.put("orderBy", orderBy.name());
142 }
143 if (orderDesc != null) {
144 params.put("orderDesc", orderDesc.toString());
145 }
146 if (firstResult != null) {
147 params.put("firstResult", Integer.toString(firstResult));
148 }
149 if (maxResults != null) {
150 params.put("maxResults", Integer.toString(maxResults));
151 }
152 if (like != null) {
153 params.put("like", like);
154 }
155 StringResponse response = requestString(credentials.getSellerId() + "/keyboards", HttpMethod.GET, url, credentials.toAuthentication(),
156 HttpHelper.integerToETag(version), params, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
157 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
158 return response == null ? null : (List<KeyboardInfo>) KeyboardInfo.fromJsonArrayToKeyboardInfoes(response.getContent());
159 }
160 }