Skip to content

Commit 6291a4f

Browse files
Scale functionality now implemented. Tests changed to reflect.
1 parent 193b7f4 commit 6291a4f

4 files changed

Lines changed: 295 additions & 6 deletions

File tree

‎src/main/java/com/printnode/api/APIClient.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,31 @@ public final Printer[] getPrintersByComputers(final String computerSet,
763763

764764
}
765765

766+
public final Scale[] getScales(final int computerId) throws IOException {
767+
CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credentials).build();
768+
Scale[] scales;
769+
try {
770+
HttpGet httpget = new HttpGet(apiUrl + "/computer/" + computerId + "/scales/");
771+
httpget.addHeader(childHeaders[0], childHeaders[1]);
772+
CloseableHttpResponse response = client.execute(httpget);
773+
try {
774+
HttpEntity entity = response.getEntity();
775+
String responseString = EntityUtils.toString(entity);
776+
checkResponseForExceptions(response.getStatusLine().getStatusCode(), responseString);
777+
JsonArray responseParse = new JsonParser().parse(responseString).getAsJsonArray();
778+
scales = new Scale[responseParse.size()];
779+
for (int i = 0; i < responseParse.size(); i++) {
780+
scales[i] = new Scale(responseParse.get(i).getAsJsonObject());
781+
}
782+
} finally {
783+
response.close();
784+
}
785+
} finally {
786+
client.close();
787+
}
788+
return scales;
789+
790+
}
766791
/**
767792
* Given a set of printers, return an array of printers.
768793
*
@@ -865,6 +890,7 @@ public final String getApiKeys(final String apikey) throws IOException {
865890
return apikeyvalue;
866891
}
867892

893+
868894
/**
869895
* Given a tagname, return the value of that tag.
870896
*

‎src/main/java/com/printnode/api/Capabilities.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class Capabilities {
5858
/**
5959
* A list of paper names and their sizes.
6060
* */
61-
private HashMap<String, int[]> papers;
61+
private HashMap<String, int[]> papers = new HashMap<String, int[]>();
6262
/**
6363
* Whether this printer supports custom paper size.
6464
* */
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
package com.printnode.api;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
import java.util.Set;
9+
10+
/**
11+
* Object for a Whoami.
12+
* */
13+
public class Scale {
14+
15+
/**
16+
* response body that made this Whoami.
17+
* */
18+
private String json;
19+
20+
/**
21+
* The id of this account.
22+
* */
23+
private int[] mass = new int[2];
24+
/**
25+
* The firstname of this account.
26+
* */
27+
private String deviceName;
28+
/**
29+
* The lastname of the authenticated account.
30+
* */
31+
private int deviceNum;
32+
/**
33+
* Whether this account can create sub accounts or not.
34+
* */
35+
private String port;
36+
/**
37+
* The email registered to this account.
38+
* */
39+
private int count;
40+
/**
41+
* The email of the creator of this account.
42+
* */
43+
private HashMap<String, Integer> measurement;
44+
/**
45+
* The reference the creator set to this account.
46+
* */
47+
private String clientReportedCreateTimestamp;
48+
/**
49+
* An array of child account ids.
50+
* */
51+
private int ntpOffset;
52+
/**
53+
* The amount of credits this account has.
54+
* */
55+
private int ageOfData;
56+
/**
57+
* The amount of computers this account has.
58+
* */
59+
private int computerId;
60+
/**
61+
* The total prints this account has done.
62+
* */
63+
private String vendor;
64+
/**
65+
* The versions this account uses.
66+
* */
67+
private String product;
68+
/**
69+
* The accounts connected via this account.
70+
* */
71+
private int vendorId;
72+
/**
73+
* The tags associated with this account.
74+
* */
75+
private int productId;
76+
77+
78+
/**
79+
* Parses a JsonObject into Whoami.
80+
* Firstly, it begins iterating over the object.
81+
* If the object we are converting is a solo JsonPrimitive,
82+
* we map it directly to the variable.
83+
* If the object we are converting is an array of JsonPrimitives,
84+
* we firstly create an array of the same size as it.
85+
* Then, iterate over it.
86+
* If the object is a JsonObject with the same mappings each time, it is mapped to a HashMap.
87+
* If the object is a JsonObject with different mappings, it is mapped to a Java Object.
88+
* @param response JsonObject of the response.
89+
* @see JsonObject
90+
* @see JsonArray
91+
* @see JsonPrimitive
92+
* @see JsonElement
93+
* */
94+
public Scale(final JsonObject response) {
95+
if (!response.get("mass").isJsonNull()) {
96+
JsonArray jsonMass = response.get("mass").getAsJsonArray();
97+
for (int i = 0; i < jsonMass.size(); i++) {
98+
mass[i] = jsonMass.get(i).isJsonNull() ? -1 : jsonMass.get(i).getAsInt();
99+
}
100+
}
101+
if (!response.get("deviceName").isJsonNull()) {
102+
deviceName = response.get("deviceName").getAsString();
103+
}
104+
if (!(response.get("deviceNum").isJsonNull())) {
105+
deviceNum = response.get("deviceNum").getAsInt();
106+
}
107+
if (!response.get("port").isJsonNull()) {
108+
port = response.get("port").getAsString();
109+
}
110+
if (!response.get("count").isJsonNull()) {
111+
count = response.get("count").getAsInt();
112+
}
113+
if (!response.get("measurement").isJsonNull()) {
114+
JsonObject jsonMeasurement = response.get("measurement").getAsJsonObject();
115+
Set<Map.Entry<String, JsonElement>> measurementEntries = jsonMeasurement.entrySet();
116+
measurement = new HashMap<String, Integer>();
117+
for (Map.Entry<String, JsonElement> measurementEntry : measurementEntries) {
118+
int jsonMeasurementValue = measurementEntry.getValue().getAsInt();
119+
measurement.put(measurementEntry.getKey(), jsonMeasurementValue);
120+
}
121+
}
122+
if (!response.get("clientReportedCreateTimestamp").isJsonNull()) {
123+
clientReportedCreateTimestamp = response.get("clientReportedCreateTimestamp").getAsString();
124+
}
125+
if (!response.get("ntpOffset").isJsonNull()) {
126+
ntpOffset = response.get("ntpOffset").getAsInt();
127+
}
128+
if (!response.get("ageOfData").isJsonNull()) {
129+
ageOfData = response.get("ageOfData").getAsInt();
130+
}
131+
if (!response.get("computerId").isJsonNull()) {
132+
computerId = response.get("computerId").getAsInt();
133+
}
134+
if (!response.get("vendor").isJsonNull()) {
135+
vendor = response.get("vendor").getAsString();
136+
}
137+
if (!response.get("product").isJsonNull()) {
138+
product = response.get("product").getAsString();
139+
}
140+
if (!response.get("vendorId").isJsonNull()) {
141+
vendorId = response.get("vendorId").getAsInt();
142+
}
143+
if (!response.get("productId").isJsonNull()) {
144+
productId = response.get("productId").getAsInt();
145+
}
146+
json = response.toString();
147+
}
148+
149+
/**
150+
* @return id of this account.
151+
* */
152+
public final int[] getMass() {
153+
return mass;
154+
}
155+
156+
/**
157+
* @return first name of the account holder.
158+
* */
159+
public final String getDeviceName() {
160+
return deviceName;
161+
}
162+
163+
/**
164+
* @return last name of the account holder.
165+
* */
166+
public final int getDeviceNum() {
167+
return deviceNum;
168+
}
169+
170+
/**
171+
* @return whether this account can make sub accounts (i.e is a parent account.)
172+
* */
173+
public final String getPort() {
174+
return port;
175+
}
176+
177+
/**
178+
* @return the email for this account.
179+
* */
180+
public final int getCount() {
181+
return count;
182+
}
183+
184+
/**
185+
* @return the email of the creator of this account.
186+
* */
187+
public final HashMap<String, Integer> getMeasurement() {
188+
return measurement;
189+
}
190+
191+
/**
192+
* @return creator reference for this account.
193+
* */
194+
public final String getClientReportedCreateTimestamp() {
195+
return clientReportedCreateTimestamp;
196+
}
197+
198+
/**
199+
* @return array of child account ids for this account.
200+
* */
201+
public final int getNtpOffset() {
202+
return ntpOffset;
203+
}
204+
205+
/**
206+
* @return number of printing credits.
207+
* */
208+
public final int getAgeOfData() {
209+
return ageOfData;
210+
}
211+
212+
/**
213+
* @return Number of computers used by this account.
214+
* */
215+
public final int getComputerId() {
216+
return computerId;
217+
}
218+
219+
/**
220+
* @return total prints done by this account.
221+
* */
222+
public final String getVendor() {
223+
return vendor;
224+
}
225+
226+
/**
227+
* @return an array of client versions used for this account.
228+
* */
229+
public final String getProduct() {
230+
return product;
231+
}
232+
233+
/**
234+
* @return an array of connected clients.
235+
* */
236+
public final int getVendorId() {
237+
return vendorId;
238+
}
239+
240+
/**
241+
* @return a hashamp of the tags this account has.
242+
* */
243+
public final int getProductId() {
244+
return productId;
245+
}
246+
247+
/**
248+
* @return the original response string.
249+
* */
250+
public final String toString() {
251+
return json;
252+
}
253+
254+
}

‎src/test/java/com/printnode/api/AppTest.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ public class AppTest {
3838
public APIClient makeClient() throws IOException,InterruptedException{
3939
Thread.sleep(1000);
4040
Auth anAuth = new Auth();
41-
anAuth.setApiKey("god");
41+
anAuth.setApiKey("MyAuth");
4242
APIClient aClient = new APIClient(anAuth);
43-
aClient.setApiUrl("https://apidev.printnode.com");
43+
aClient.setApiUrl("https://api.printnode.com");
4444
return aClient;
4545
}
4646

4747
@Test
4848
public void testAuth() throws IOException,InterruptedException{
4949
Auth testAuth = new Auth();
50-
testAuth.setApiKey("god");
50+
testAuth.setApiKey("MyAuth");
5151

5252
APIClient testClient = new APIClient(testAuth);
53-
testClient.setApiUrl("https://apidev.printnode.com");
53+
testClient.setApiUrl("https://api.printnode.com");
5454

5555
Whoami aRequest = testClient.getWhoami();
5656

@@ -86,14 +86,22 @@ public void testPrintJobs() throws IOException,InterruptedException{
8686
assertTrue("Failed: should be instance of Computer",printjobs[0].getPrinter().getComputer() instanceof Computer);
8787
}
8888

89+
@Test
90+
public void testScales() throws IOException,InterruptedException {
91+
APIClient aClient = makeClient();
92+
93+
Scale aScale = aClient.getScales(0)[0];
94+
95+
assertTrue("Failed: should exist", aScale.getProduct() != null);
96+
}
97+
8998
@Test
9099
public void testSubmitPrintJob() throws IOException,InterruptedException{
91100
APIClient aClient = makeClient();
92101

93102
Printer aPrinter = aClient.getPrinters("")[0];
94103

95104
PrintJobJson myPrintJobCreation = new PrintJobJson(aPrinter.getId(),"PrintNode-Java","pdf_uri", "http://something","From PrintNode-Java Client");
96-
97105
int response = aClient.createPrintJob(myPrintJobCreation);
98106
}
99107

@@ -168,6 +176,7 @@ public void testAPIKeyFunctionality() throws IOException,InterruptedException{
168176

169177
assertTrue("Failed: was false or something else", apiKeyDelete);
170178

179+
childClient.deleteAccount();
171180
}
172181

173182
@Test

0 commit comments

Comments
 (0)