1 package com.srv4pos.server.api.seller;
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.exceptions.enums.UnprocessableEntityErrorType;
7 import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
8 import com.srv4pos.server.api.infrastructure.credentials.Credentials;
9 import com.srv4pos.server.api.infrastructure.http.HttpHelper;
10 import com.srv4pos.server.api.infrastructure.http.Transport;
11 import com.srv4pos.server.api.user.UserInfo;
12 import com.srv4pos.server.api.user.UserRegistrationInfo;
13 import com.srv4pos.server.api.user.UserShortInfo;
14 import com.srv4pos.server.api.infrastructure.exceptions.enums.ForbiddenErrorType;
15
16 import java.net.URL;
17 import java.util.Collection;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
22 import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
23 import static java.lang.String.format;
24
25 public class UserService {
26 private URL url;
27 private Credentials credentials;
28 private Transport transport;
29
30
31
32
33
34
35
36
37 public UserService(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(String id, UserInfo userInfo) {
55 requestVoid(format("users/%s", id),
56 HttpMethod.PUT,
57 url,
58 credentials.toAuthentication(),
59 null,
60 null,
61 new StringConnectionWriter(userInfo.toJson()),
62 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
63 transport);
64 }
65
66
67
68
69
70
71
72
73
74
75 public UserInfo get(String id) {
76 return UserInfo.fromJsonToUserInfo(requestString(
77 format("users/%s", id),
78 HttpMethod.GET,
79 url,
80 credentials.toAuthentication(),
81 null,
82 null,
83 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
84 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
85 transport).getContent());
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99 public String create(UserRegistrationInfo userRegistrationInfo) {
100 return IdentifierInfo.fromJsonToIdentifierInfo(requestString(
101 "users",
102 HttpMethod.POST,
103 url,
104 credentials.toAuthentication(),
105 null,
106 null,
107 new StringConnectionWriter(userRegistrationInfo.toJson()),
108 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
109 transport).getContent()).getIdentifier();
110 }
111
112
113
114
115
116
117
118
119
120 public Collection<UserShortInfo> listMinified(Integer firstResult,
121 Integer maxResults,
122 String like) {
123 Map<String, String> params = new HashMap<String, String>();
124 if (firstResult != null) {
125 params.put("firstResult", Integer.toString(firstResult));
126 }
127 if (maxResults != null) {
128 params.put("maxResults", Integer.toString(maxResults));
129 }
130 if (like != null) {
131 params.put("like", like);
132 }
133
134 return UserShortInfo.fromJsonArrayToUserShortInfoes(requestString(
135 "users-minified",
136 HttpMethod.GET,
137 url,
138 credentials.toAuthentication(),
139 null,
140 params,
141 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
142 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
143 transport).getContent());
144 }
145
146
147
148
149
150
151
152
153
154 public Collection<UserShortInfo> list(Integer firstResult,
155 Integer maxResults,
156 String like) {
157 Map<String, String> params = new HashMap<String, String>();
158 if (firstResult != null) {
159 params.put("firstResult", Integer.toString(firstResult));
160 }
161 if (maxResults != null) {
162 params.put("maxResults", Integer.toString(maxResults));
163 }
164 if (like != null) {
165 params.put("like", like);
166 }
167
168 return UserShortInfo.fromJsonArrayToUserShortInfoes(requestString(
169 "users",
170 HttpMethod.GET,
171 url,
172 credentials.toAuthentication(),
173 null,
174 params,
175 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
176 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
177 transport).getContent());
178 }
179 }