View Javadoc
1   package com.srv4pos.server.api.receiptSettings;
2   
3   import com.srv4pos.server.api.infrastructure.EmptyConnectionWriter;
4   import com.srv4pos.server.api.infrastructure.HttpMethod;
5   import com.srv4pos.server.api.infrastructure.StringConnectionWriter;
6   import com.srv4pos.server.api.infrastructure.credentials.Credentials;
7   import com.srv4pos.server.api.infrastructure.http.HttpHelper;
8   import com.srv4pos.server.api.infrastructure.http.Transport;
9   import com.srv4pos.server.api.infrastructure.request.StringResponse;
10  
11  import java.net.URL;
12  
13  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestString;
14  import static com.srv4pos.server.api.infrastructure.request.RequestHelper.requestVoid;
15  import static java.lang.String.format;
16  
17  /**
18   * To reach ReceiptSettings entity.
19   * User: Pavel Abizyaev, Date: 10.03.2015 15:49.
20   */
21  public class ReceiptSettingsService {
22      private URL url;
23      private Credentials credentials;
24      private Transport transport;
25  
26      /**
27       * Constructs the service.
28       *
29       * @param url of the server
30       * @param credentials to work with the server
31       * @param transport to know the method to reach the server
32       */
33      public ReceiptSettingsService(URL url, Credentials credentials, Transport transport) {
34          this.url = url;
35          this.credentials = credentials;
36          this.transport = transport;
37      }
38  
39      /**
40       * Returns an entity.
41       *
42       * @param version    latest version of seller, may be null (if latest data needed)
43       * @return null if not modified since version or entity. if version is null then return value can not be null
44       * @see ReceiptSettingsServiceSample
45       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
46       *          if version mismatch
47       * @throws com.srv4pos.server.api.exceptions.NotFoundJsonException
48       *          if entity is not found
49       */
50      public ReceiptSettingsInfo get(Integer version) {
51          StringResponse response = requestString(
52                  format("%s/receipt-settings", credentials.getSellerId()),
53                  HttpMethod.GET,
54                  url,
55                  credentials.toAuthentication(),
56                  HttpHelper.integerToETag(version),
57                  null,
58                  EmptyConnectionWriter.EMPTY_CONNECTION_WRITER,
59                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
60                  transport
61          );
62          return response == null ? null : ReceiptSettingsInfo.fromJsonToReceiptSettingsInfo(response.getContent());
63      }
64  
65      /**
66       * Update receipt settings on the server.
67       *
68       * @param version   expected latest version of seller
69       * @param receiptSettingsInfo data to put
70       * @throws com.srv4pos.server.api.exceptions.VersionConflictJsonException
71       *          if version mismatch
72       */
73      public void update(int version, ReceiptSettingsInfo receiptSettingsInfo) {
74          requestVoid(
75                  format("%s/receipt-settings", credentials.getSellerId()),
76                  HttpMethod.POST,
77                  url,
78                  credentials.toAuthentication(),
79                  HttpHelper.integerToETag(version),
80                  null,
81                  new StringConnectionWriter(receiptSettingsInfo.toJson()),
82                  HttpHelper.CONTENT_TYPE_APPLICATION_JSON,
83                  transport);
84      }
85  }