Skip to content

Commit 55a4a83

Browse files
committed
Use org.joda.time.DateTime to handle datetime objects
All datetime fields in the models are now parsed to `org.joda.time.DateTime` in UTC. This is set as a convention to avoid any inconsistencies in the timezone sent in the v2 API. `DateTimeUtils.parseISODateTimeString` will automatically detect the timezone in datetime string and return a `DateTime` object, given that the string is in ISO8601 format. Signed-off-by: Abeer <abeer.u@instamojo.com>
1 parent 6200a8c commit 55a4a83

18 files changed

Lines changed: 131 additions & 74 deletions

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies {
5656
compile group: 'commons-codec', name: 'commons-codec', version: '1.11'
5757
compile group: 'commons-logging', name: 'commons-logging', version: '1.2'
5858
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
59+
compile group: 'joda-time', name: 'joda-time', version: '2.10.1'
5960
}
6061

6162
jacocoTestReport {

src/main/java/com/instamojo/wrapper/api/InstamojoImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.instamojo.wrapper.response.ApiListResponse;
1111
import com.instamojo.wrapper.response.ApiResponse;
1212
import com.instamojo.wrapper.util.Constants;
13+
import com.instamojo.wrapper.util.GsonWrapper;
1314
import com.instamojo.wrapper.util.HttpUtils;
1415
import org.apache.commons.codec.digest.HmacAlgorithms;
1516
import org.apache.commons.codec.digest.HmacUtils;
@@ -28,7 +29,7 @@ public class InstamojoImpl implements Instamojo {
2829

2930
public InstamojoImpl(ApiContext context) {
3031
this.context = context;
31-
this.gson = new Gson();
32+
this.gson = GsonWrapper.getGson();
3233
}
3334

3435
@Override

src/main/java/com/instamojo/wrapper/model/Invoice.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.instamojo.wrapper.model;
22

33
import com.google.gson.annotations.SerializedName;
4+
import org.joda.time.DateTime;
45

56
import java.util.Date;
67

@@ -12,16 +13,16 @@ public class Invoice {
1213
private String userUri;
1314

1415
@SerializedName("created_at")
15-
private Date createdAt;
16+
private DateTime createdAt;
1617

1718
@SerializedName("issue_date")
18-
private Date issueDate;
19+
private DateTime issueDate;
1920

2021
@SerializedName("file")
2122
private String fileUrl;
2223

2324
@SerializedName("last_modified")
24-
private Date modifiedAt;
25+
private DateTime modifiedAt;
2526

2627
public String getId() {
2728
return id;
@@ -39,19 +40,19 @@ public void setUserUri(String userUri) {
3940
this.userUri = userUri;
4041
}
4142

42-
public Date getCreatedAt() {
43+
public DateTime getCreatedAt() {
4344
return createdAt;
4445
}
4546

46-
public void setCreatedAt(Date createdAt) {
47+
public void setCreatedAt(DateTime createdAt) {
4748
this.createdAt = createdAt;
4849
}
4950

50-
public Date getIssueDate() {
51+
public DateTime getIssueDate() {
5152
return issueDate;
5253
}
5354

54-
public void setIssueDate(Date issueDate) {
55+
public void setIssueDate(DateTime issueDate) {
5556
this.issueDate = issueDate;
5657
}
5758

@@ -63,11 +64,11 @@ public void setFileUrl(String fileUrl) {
6364
this.fileUrl = fileUrl;
6465
}
6566

66-
public Date getModifiedAt() {
67+
public DateTime getModifiedAt() {
6768
return modifiedAt;
6869
}
6970

70-
public void setModifiedAt(Date modifiedAt) {
71+
public void setModifiedAt(DateTime modifiedAt) {
7172
this.modifiedAt = modifiedAt;
7273
}
7374

src/main/java/com/instamojo/wrapper/model/PaymentOrder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.instamojo.wrapper.model;
22

33
import com.google.gson.annotations.SerializedName;
4+
import org.joda.time.DateTime;
45

56
import java.util.List;
67

@@ -68,7 +69,7 @@ public class PaymentOrder {
6869
* The created at.
6970
*/
7071
@SerializedName("created_at")
71-
private String createdAt;
72+
private DateTime createdAt;
7273

7374
/**
7475
* The resource uri.
@@ -288,7 +289,7 @@ public void setRedirectUrl(String redirectUrl) {
288289
*
289290
* @return the created at
290291
*/
291-
public String getCreatedAt() {
292+
public DateTime getCreatedAt() {
292293
return createdAt;
293294
}
294295

@@ -297,7 +298,7 @@ public String getCreatedAt() {
297298
*
298299
* @param createdAt the new created at
299300
*/
300-
public void setCreatedAt(String createdAt) {
301+
public void setCreatedAt(DateTime createdAt) {
301302
this.createdAt = createdAt;
302303
}
303304

src/main/java/com/instamojo/wrapper/model/PaymentRequest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.instamojo.wrapper.model;
22

33
import com.google.gson.annotations.SerializedName;
4+
import org.joda.time.DateTime;
45

5-
import java.util.Date;
66
import java.util.List;
77

88
public class PaymentRequest {
@@ -50,10 +50,10 @@ public class PaymentRequest {
5050
private String webhookUrl;
5151

5252
@SerializedName("scheduled_at")
53-
private Date scheduledAt;
53+
private DateTime scheduledAt;
5454

5555
@SerializedName("expires_at")
56-
private Date expiresAt;
56+
private DateTime expiresAt;
5757

5858
@SerializedName("allow_repeated_payments")
5959
private Boolean allowRepeatedPayments;
@@ -71,10 +71,10 @@ public class PaymentRequest {
7171
private Boolean markFulfilled;
7272

7373
@SerializedName("created_at")
74-
private Date createdAt;
74+
private DateTime createdAt;
7575

7676
@SerializedName("modified_at")
77-
private Date modifiedAt;
77+
private DateTime modifiedAt;
7878

7979
@SerializedName("resource_uri")
8080
private String resourceUri;
@@ -207,19 +207,19 @@ public void setWebhookUrl(String webhookUrl) {
207207
this.webhookUrl = webhookUrl;
208208
}
209209

210-
public Date getScheduledAt() {
210+
public DateTime getScheduledAt() {
211211
return scheduledAt;
212212
}
213213

214-
public void setScheduledAt(Date scheduledAt) {
214+
public void setScheduledAt(DateTime scheduledAt) {
215215
this.scheduledAt = scheduledAt;
216216
}
217217

218-
public Date getExpiresAt() {
218+
public DateTime getExpiresAt() {
219219
return expiresAt;
220220
}
221221

222-
public void setExpiresAt(Date expiresAt) {
222+
public void setExpiresAt(DateTime expiresAt) {
223223
this.expiresAt = expiresAt;
224224
}
225225

@@ -263,19 +263,19 @@ public void setMarkFulfilled(Boolean markFulfilled) {
263263
this.markFulfilled = markFulfilled;
264264
}
265265

266-
public Date getCreatedAt() {
266+
public DateTime getCreatedAt() {
267267
return createdAt;
268268
}
269269

270-
public void setCreatedAt(Date createdAt) {
270+
public void setCreatedAt(DateTime createdAt) {
271271
this.createdAt = createdAt;
272272
}
273273

274-
public Date getModifiedAt() {
274+
public DateTime getModifiedAt() {
275275
return modifiedAt;
276276
}
277277

278-
public void setModifiedAt(Date modifiedAt) {
278+
public void setModifiedAt(DateTime modifiedAt) {
279279
this.modifiedAt = modifiedAt;
280280
}
281281

src/main/java/com/instamojo/wrapper/model/Payout.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.instamojo.wrapper.model;
22

33
import com.google.gson.annotations.SerializedName;
4+
import org.joda.time.DateTime;
45

56
import java.util.Date;
67

@@ -13,7 +14,7 @@ public class Payout {
1314
private Boolean status;
1415

1516
@SerializedName("paid_out_at")
16-
private Date paidOutAt;
17+
private DateTime paidOutAt;
1718

1819
private String currency;
1920

@@ -71,11 +72,11 @@ public void setStatus(Boolean status) {
7172
this.status = status;
7273
}
7374

74-
public Date getPaidOutAt() {
75+
public DateTime getPaidOutAt() {
7576
return paidOutAt;
7677
}
7778

78-
public void setPaidOutAt(Date paidOutAt) {
79+
public void setPaidOutAt(DateTime paidOutAt) {
7980
this.paidOutAt = paidOutAt;
8081
}
8182

src/main/java/com/instamojo/wrapper/model/Refund.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.gson.annotations.SerializedName;
44

55
import java.util.Date;
6+
import org.joda.time.DateTime;
67

78
/**
89
* The Class Refund.
@@ -31,7 +32,7 @@ public class Refund {
3132
private Double totalAmount;
3233

3334
@SerializedName("created_at")
34-
private Date createdAt;
35+
private DateTime createdAt;
3536

3637
public String getId() {
3738
return id;
@@ -89,11 +90,11 @@ public void setTotalAmount(Double totalAmount) {
8990
this.totalAmount = totalAmount;
9091
}
9192

92-
public Date getCreatedAt() {
93+
public DateTime getCreatedAt() {
9394
return createdAt;
9495
}
9596

96-
public void setCreatedAt(Date createdAt) {
97+
public void setCreatedAt(DateTime createdAt) {
9798
this.createdAt = createdAt;
9899
}
99100

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.instamojo.wrapper.util;
2+
3+
import org.joda.time.DateTime;
4+
import org.joda.time.DateTimeZone;
5+
import org.joda.time.format.ISODateTimeFormat;
6+
7+
public class DateTimeUtils {
8+
9+
public static DateTime parseISODateTimeString(String datetime) {
10+
return ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC).parseDateTime(datetime);
11+
}
12+
13+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.instamojo.wrapper.util;
2+
import com.google.gson.*;
3+
import org.joda.time.DateTime;
4+
import org.joda.time.DateTimeZone;
5+
import org.joda.time.format.ISODateTimeFormat;
6+
7+
import java.lang.reflect.Type;
8+
9+
10+
final class DateTimeDeserializer implements JsonDeserializer<DateTime> {
11+
12+
@Override
13+
public DateTime deserialize(JsonElement value, Type type, JsonDeserializationContext context) throws JsonParseException {
14+
return value == null ? null : DateTimeUtils.parseISODateTimeString(value.getAsString());
15+
}
16+
}
17+
18+
final class DateTimeSerializer implements JsonSerializer<DateTime> {
19+
20+
@Override
21+
public JsonElement serialize(DateTime dt, Type type, JsonSerializationContext context) throws JsonParseException {
22+
return dt == null ? null : new JsonPrimitive(dt.toString());
23+
}
24+
25+
}
26+
27+
28+
public class GsonWrapper {
29+
30+
public static Gson getGson() {
31+
return new GsonBuilder()
32+
.registerTypeAdapter(DateTime.class, new DateTimeDeserializer())
33+
.registerTypeAdapter(DateTime.class, new DateTimeSerializer())
34+
.create();
35+
}
36+
37+
}

src/main/java/com/instamojo/wrapper/util/HttpUtils.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,8 @@ public static String post(String url, Map<String, String> customHeaders, String
150150
}
151151

152152
private static boolean isErrorStatus(int statusCode) {
153-
if (statusCode >= 400 && statusCode < 600) {
154-
return true;
155-
}
153+
return statusCode >= 400 && statusCode < 600;
156154

157-
return false;
158155
}
159156

160157
private static void populateHeaders(HttpRequestBase httpRequestBase, Map<String, String> customHeaders) {

0 commit comments

Comments
 (0)