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