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