View Javadoc
1   package com.srv4pos.server.api.activation;
2   
3   import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
4   import com.srv4pos.server.api.infrastructure.HttpMethod;
5   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
6   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
7   import com.srv4pos.server.api.infrastructure.http.Transport;
8   
9   import java.net.URL;
10  import java.util.HashMap;
11  import java.util.List;
12  import java.util.Map;
13  
14  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
15  import static java.lang.String.format;
16  
17  /**
18   * Managing devices.
19   */
20  public class DeviceService {
21      private URL url;
22      private Credentials credentials;
23      private Transport transport;
24  
25      /**
26       * Constructor.
27       *
28       * @param url         specify url of the server (usually https://a.srv4pos.com/api/v{versionNumber})
29       * @param credentials specify username and password to connect to or null if you want to connect to public methods
30       * @param transport   specify a mecanism to make api calls, see {@link com.srv4pos.server.api.infrastructure.http.Transport} for more info.
31       */
32      public DeviceService(URL url, Credentials credentials, Transport transport) {
33          this.url = url;
34          this.credentials = credentials;
35          this.transport = transport;
36      }
37  
38      /**
39       * Returns overview of device.
40       * @param id identifies entity
41       * @return overview
42       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException if entity is not found
43       */
44      public DeviceOverviewInfo getOverview(long id) {
45          return DeviceOverviewInfo.fromJsonToDeviceOverviewInfo(requestString(
46                  format("devices-overview/%s", id),
47                  HttpMethod.GET,
48                  url,
49                  credentials.toAuthentication(),
50                  null,
51                  null,
52                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
53                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
54                  transport).getContent());
55      }
56  
57      /**
58       * Returns list of entities.
59       *
60       * @return list of entities
61       */
62      public List<DeviceInfo> list(Integer firstResult,
63                                   Integer maxResults,
64                                   String like) {
65          Map<String, String> params = new HashMap<String, String>();
66          if (firstResult != null) {
67              params.put("firstResult", Integer.toString(firstResult));
68          }
69          if (maxResults != null) {
70              params.put("maxResults", Integer.toString(maxResults));
71          }
72          if (like != null) {
73              params.put("like", like);
74          }
75  
76          return (List<DeviceInfo>) DeviceInfo.fromJsonArrayToDeviceInfoes(requestString(
77                  "devices",
78                  HttpMethod.GET,
79                  url,
80                  credentials.toAuthentication(),
81                  null,
82                  params,
83                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
84                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
85                  transport).getContent());
86      }
87  
88      /**
89       * Returns list of entities.
90       *
91       * @return list of entities
92       */
93      public List<DeviceOverviewInfo> listOverview(Integer firstResult, Integer maxResults, String like) {
94          Map<String, String> params = new HashMap<String, String>();
95          if (firstResult != null) {
96              params.put("firstResult", Integer.toString(firstResult));
97          }
98          if (maxResults != null) {
99              params.put("maxResults", Integer.toString(maxResults));
100         }
101         if (like != null) {
102             params.put("like", like);
103         }
104 
105         return (List<DeviceOverviewInfo>) DeviceOverviewInfo.fromJsonArrayToDeviceOverviewInfoes(requestString(
106                 "devices-overview",
107                 HttpMethod.GET,
108                 url,
109                 credentials.toAuthentication(),
110                 null,
111                 params,
112                 EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
113                 HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
114                 transport).getContent());
115     }
116 }