1 package com.srv4pos.server.api.precept;
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 import java.util.Map;
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 PreceptService {
25 private URL url;
26 private Credentials credentials;
27 private Transport transport;
28
29
30
31
32
33
34
35
36 public PreceptService(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 String create(String country, String corporateId, PreceptInfo preceptInfo) {
54 return IdentifierInfo.fromJsonToIdentifierInfo(
55 requestString(format("%s%s/precepts", country, corporateId), HttpMethod.POST, url,
56 credentials == null ? null : credentials.toAuthentication(),
57 null, null, new StringConnectionWriter(preceptInfo.toJson()),
58 HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport).getContent()).getIdentifier();
59
60 }
61
62
63
64
65
66
67
68
69
70 public List<PreceptInfo> list(Integer firstResult,
71 Integer maxResults,
72 String like) {
73 Map<String, String> params = new HashMap<String, String>();
74 if (firstResult != null) {
75 params.put("firstResult", Integer.toString(firstResult));
76 }
77 if (maxResults != null) {
78 params.put("maxResults", Integer.toString(maxResults));
79 }
80 if (like != null) {
81 params.put("like", like);
82 }
83
84 return (List<PreceptInfo>) PreceptInfo.fromJsonArrayToPreceptInfoes(requestString(
85 format("%s/precepts", credentials.getSellerId()),
86 HttpMethod.GET,
87 url,
88 credentials.toAuthentication(),
89 null,
90 params,
91 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
92 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
93 transport).getContent());
94 }
95
96
97
98
99
100
101
102 public PreceptInfo get(String identifier) {
103 return PreceptInfo.fromJsonToPreceptInfo(requestString(
104 format("%s/precepts/%s", credentials.getSellerId(), identifier),
105 HttpMethod.GET,
106 url,
107 credentials.toAuthentication(),
108 null,
109 null,
110 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
111 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
112 transport).getContent());
113 }
114
115
116
117
118
119
120
121 public void put(String identifier, PreceptInfo preceptInfo) {
122 requestVoid(
123 format("%s/precepts/%s", credentials.getSellerId(), identifier),
124 HttpMethod.PUT,
125 url,
126 credentials.toAuthentication(),
127 null,
128 null,
129 new StringConnectionWriter(preceptInfo.toJson()),
130 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
131 transport
132 );
133 }
134
135
136
137
138
139
140
141
142
143 public List<PreceptInfo> listDiff(int versionFrom, int versionTo) {
144 StringResponse response = requestString(String.format("%s/precepts-diff/%s/%s", credentials.getSellerId(), versionFrom, versionTo), HttpMethod.GET, url,
145 credentials.toAuthentication(), null, null, EmptyConnectionWriter.EMPTY_CONNECTION_WRITER, HttpHelper.CONTENT_TYPE_APPLICATION_JSON, transport);
146 return (List<PreceptInfo>) PreceptInfo.fromJsonArrayToPreceptInfoes(response.getContent());
147 }
148 }