1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
package com.jcabi.odesk; |
31 | |
|
32 | |
import com.jcabi.aspects.Immutable; |
33 | |
import com.jcabi.aspects.Loggable; |
34 | |
import com.jcabi.http.Request; |
35 | |
import com.jcabi.http.RequestURI; |
36 | |
import com.jcabi.http.response.JsonResponse; |
37 | |
import com.jcabi.http.response.RestResponse; |
38 | |
import java.io.IOException; |
39 | |
import java.math.BigDecimal; |
40 | |
import java.net.HttpURLConnection; |
41 | |
import java.util.ArrayList; |
42 | |
import java.util.Collection; |
43 | |
import javax.json.JsonString; |
44 | |
import javax.validation.constraints.NotNull; |
45 | |
import lombok.EqualsAndHashCode; |
46 | |
import lombok.ToString; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@Immutable |
56 | 0 | @ToString |
57 | |
@Loggable(Loggable.DEBUG) |
58 | 0 | @EqualsAndHashCode(of = "entry") |
59 | |
final class RtAdjustments implements Adjustments { |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
private final transient Request entry; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | 0 | RtAdjustments(final Request req, final String name) { |
72 | 0 | this.entry = req.uri() |
73 | |
.path("v2/teams") |
74 | |
.path(name) |
75 | |
.path("adjustments.json") |
76 | |
.back(); |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
@Override |
81 | |
@NotNull(message = "adjustment ID is never NULL") |
82 | |
public String add( |
83 | |
@NotNull(message = "engagement ref can't be NULL") |
84 | |
final String engagement, |
85 | |
@NotNull(message = "charge amount can't be NULL") |
86 | |
final BigDecimal charge, |
87 | |
@NotNull(message = "comments can't be NULL") final String comments, |
88 | |
@NotNull(message = "notes can't be NULL") final String notes) |
89 | |
throws IOException { |
90 | 0 | RequestURI uri = this.entry.uri() |
91 | |
.queryParam("engagement__reference", engagement) |
92 | |
.queryParam("comments", comments) |
93 | |
.queryParam("notes", notes); |
94 | 0 | if (charge != null && charge.compareTo(BigDecimal.ZERO) != 0) { |
95 | 0 | uri = uri.queryParam("charge_amount", charge.toString()); |
96 | |
} |
97 | 0 | return uri.back().method(Request.POST).fetch() |
98 | |
.as(RestResponse.class) |
99 | |
.assertStatus(HttpURLConnection.HTTP_OK) |
100 | |
.as(JsonResponse.class) |
101 | |
.json().readObject().getJsonObject("adjustment") |
102 | |
.getString("reference"); |
103 | |
} |
104 | |
|
105 | |
@Override |
106 | |
@NotNull(message = "iterable of adjustments is never NULL") |
107 | |
public Iterable<String> iterate() throws IOException { |
108 | 0 | final Collection<JsonString> values = this.entry.fetch() |
109 | |
.as(RestResponse.class) |
110 | |
.assertStatus(HttpURLConnection.HTTP_OK) |
111 | |
.as(JsonResponse.class) |
112 | |
.json().readObject().getJsonArray("adjustments") |
113 | |
.getValuesAs(JsonString.class); |
114 | 0 | final Collection<String> refs = new ArrayList<String>(values.size()); |
115 | 0 | for (final JsonString val : values) { |
116 | 0 | refs.add(val.getString()); |
117 | 0 | } |
118 | 0 | return refs; |
119 | |
} |
120 | |
} |