Skip to content

Commit 9997c24

Browse files
author
Max Presman
committed
retrofit manager adjustments
1 parent 0f07a69 commit 9997c24

29 files changed

Lines changed: 195 additions & 140 deletions

‎src/main/java/com/pubnub/api/PubNub.java‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.pubnub.api.endpoints.push.ListPushProvisions;
2525
import com.pubnub.api.managers.BasePathManager;
2626
import com.pubnub.api.managers.PublishSequenceManager;
27+
import com.pubnub.api.managers.RetrofitManager;
2728
import com.pubnub.api.managers.SubscriptionManager;
2829
import com.pubnub.api.vendor.Crypto;
2930
import lombok.AccessLevel;
@@ -45,6 +46,8 @@ public class PubNub {
4546
private BasePathManager basePathManager;
4647
@Getter(AccessLevel.NONE)
4748
private PublishSequenceManager publishSequenceManager;
49+
@Getter(AccessLevel.NONE)
50+
private RetrofitManager retrofitManager;
4851

4952

5053
private static final int TIMESTAMP_DIVIDER = 1000;
@@ -54,8 +57,9 @@ public class PubNub {
5457

5558
public PubNub(final PNConfiguration initialConfig) {
5659
this.configuration = initialConfig;
57-
this.subscriptionManager = new SubscriptionManager(this);
5860
this.basePathManager = new BasePathManager(initialConfig);
61+
this.retrofitManager = new RetrofitManager(this, basePathManager);
62+
this.subscriptionManager = new SubscriptionManager(this, retrofitManager);
5963
this.publishSequenceManager = new PublishSequenceManager(MAX_SEQUENCE);
6064
}
6165

@@ -84,78 +88,78 @@ public final UnsubscribeBuilder unsubscribe() {
8488
// start push
8589

8690
public final AddChannelsToPush addPushNotificationsOnChannels() {
87-
return new AddChannelsToPush(this);
91+
return new AddChannelsToPush(this, this.retrofitManager.getTransactionInstance());
8892
}
8993

9094
public final RemoveChannelsFromPush removePushNotificationsFromChannels() {
91-
return new RemoveChannelsFromPush(this);
95+
return new RemoveChannelsFromPush(this, this.retrofitManager.getTransactionInstance());
9296
}
9397

9498
public final RemoveAllPushChannelsForDevice removeAllPushNotificationsFromDeviceWithPushToken() {
95-
return new RemoveAllPushChannelsForDevice(this);
99+
return new RemoveAllPushChannelsForDevice(this, this.retrofitManager.getTransactionInstance());
96100
}
97101

98102
public final ListPushProvisions auditPushChannelProvisions() {
99-
return new ListPushProvisions(this);
103+
return new ListPushProvisions(this, this.retrofitManager.getTransactionInstance());
100104
}
101105

102106
// end push
103107

104108
public final WhereNow whereNow() {
105-
return new WhereNow(this);
109+
return new WhereNow(this, this.retrofitManager.getTransactionInstance());
106110
}
107111

108112
public final HereNow hereNow() {
109-
return new HereNow(this);
113+
return new HereNow(this, this.retrofitManager.getTransactionInstance());
110114
}
111115

112116
public final Time time() {
113-
return new Time(this);
117+
return new Time(this, this.retrofitManager.getTransactionInstance());
114118
}
115119

116120
public final History history() {
117-
return new History(this);
121+
return new History(this, this.retrofitManager.getTransactionInstance());
118122
}
119123

120124

121125
public final Audit audit() {
122-
return new Audit(this);
126+
return new Audit(this, this.retrofitManager.getTransactionInstance());
123127
}
124128

125129
public final Grant grant() {
126-
return new Grant(this);
130+
return new Grant(this, this.retrofitManager.getTransactionInstance());
127131
}
128132

129133
public final GetState getPresenceState() {
130-
return new GetState(this);
134+
return new GetState(this, this.retrofitManager.getTransactionInstance());
131135
}
132136

133137
public final SetState setPresenceState() {
134-
return new SetState(this, subscriptionManager);
138+
return new SetState(this, subscriptionManager, this.retrofitManager.getTransactionInstance());
135139
}
136140

137141
public final Publish publish() {
138-
return new Publish(this, publishSequenceManager);
142+
return new Publish(this, publishSequenceManager, this.retrofitManager.getTransactionInstance());
139143
}
140144

141145
public final ListAllChannelGroup listAllChannelGroups() {
142-
return new ListAllChannelGroup(this);
146+
return new ListAllChannelGroup(this, this.retrofitManager.getTransactionInstance());
143147
}
144148

145149
public final AllChannelsChannelGroup listChannelsForChannelGroup() {
146-
return new AllChannelsChannelGroup(this);
150+
return new AllChannelsChannelGroup(this, this.retrofitManager.getTransactionInstance());
147151
}
148152

149153
public final AddChannelChannelGroup addChannelsToChannelGroup() {
150-
return new AddChannelChannelGroup(this);
154+
return new AddChannelChannelGroup(this, this.retrofitManager.getTransactionInstance());
151155
}
152156

153157
public final RemoveChannelChannelGroup removeChannelsFromChannelGroup() {
154-
return new RemoveChannelChannelGroup(this);
158+
return new RemoveChannelChannelGroup(this, this.retrofitManager.getTransactionInstance());
155159
}
156160

157161
public final DeleteChannelGroup deleteChannelGroup() {
158-
return new DeleteChannelGroup(this);
162+
return new DeleteChannelGroup(this, this.retrofitManager.getTransactionInstance());
159163
}
160164

161165
// public methods

‎src/main/java/com/pubnub/api/endpoints/Endpoint.java‎

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55
import com.pubnub.api.PubNubException;
66
import com.pubnub.api.builder.PubNubErrorBuilder;
77
import com.pubnub.api.callbacks.PNCallback;
8-
import com.pubnub.api.enums.PNLogVerbosity;
98
import com.pubnub.api.enums.PNOperationType;
109
import com.pubnub.api.enums.PNStatusCategory;
1110
import com.pubnub.api.models.consumer.PNErrorData;
1211
import com.pubnub.api.models.consumer.PNStatus;
1312
import lombok.AccessLevel;
1413
import lombok.Getter;
15-
import okhttp3.OkHttpClient;
16-
import okhttp3.logging.HttpLoggingInterceptor;
1714
import retrofit2.Call;
1815
import retrofit2.Response;
1916
import retrofit2.Retrofit;
20-
import retrofit2.converter.jackson.JacksonConverterFactory;
2117

2218
import java.io.IOException;
2319
import java.net.ConnectException;
@@ -26,12 +22,14 @@
2622
import java.util.HashMap;
2723
import java.util.List;
2824
import java.util.Map;
29-
import java.util.concurrent.TimeUnit;
3025

3126
public abstract class Endpoint<Input, Output> {
3227

3328
@Getter(AccessLevel.PROTECTED)
3429
private PubNub pubnub;
30+
@Getter(AccessLevel.PROTECTED)
31+
private Retrofit retrofit;
32+
3533
@Getter(AccessLevel.NONE)
3634
private PNCallback<Output> cachedCallback;
3735

@@ -49,8 +47,9 @@ public abstract class Endpoint<Input, Output> {
4947
private static final int SERVER_RESPONSE_FORBIDDEN = 403;
5048
private static final int SERVER_RESPONSE_BAD_REQUEST = 400;
5149

52-
public Endpoint(final PubNub pubnubInstance) {
50+
public Endpoint(final PubNub pubnubInstance, Retrofit retrofit) {
5351
this.pubnub = pubnubInstance;
52+
this.retrofit = retrofit;
5453
}
5554

5655

@@ -239,24 +238,6 @@ private PNStatus createStatusResponse(PNStatusCategory category, Response<Input>
239238
return pnStatus.build();
240239
}
241240

242-
protected final Retrofit createRetrofit() {
243-
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
244-
httpClient.readTimeout(this.getRequestTimeout(), TimeUnit.SECONDS);
245-
httpClient.connectTimeout(this.getConnectTimeout(), TimeUnit.SECONDS);
246-
247-
if (pubnub.getConfiguration().getLogVerbosity() == PNLogVerbosity.BODY) {
248-
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
249-
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
250-
httpClient.addInterceptor(logging);
251-
}
252-
253-
return new Retrofit.Builder()
254-
.baseUrl(pubnub.getBaseUrl())
255-
.addConverterFactory(JacksonConverterFactory.create())
256-
.client(httpClient.build())
257-
.build();
258-
}
259-
260241
protected final Map<String, String> createBaseParams() {
261242
Map<String, String> params = new HashMap<>();
262243

@@ -286,11 +267,6 @@ protected List<String> getAffectedChannelGroups() {
286267

287268
protected abstract Output createResponse(Response<Input> input) throws PubNubException;
288269

289-
// add hooks for timeout
290-
protected abstract int getConnectTimeout();
291-
292-
protected abstract int getRequestTimeout();
293-
294270
protected abstract PNOperationType getOperationType();
295271

296272
protected abstract boolean isAuthRequired();

‎src/main/java/com/pubnub/api/endpoints/History.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import lombok.experimental.Accessors;
1515
import retrofit2.Call;
1616
import retrofit2.Response;
17+
import retrofit2.Retrofit;
1718
import retrofit2.http.GET;
1819
import retrofit2.http.Path;
1920
import retrofit2.http.QueryMap;
@@ -39,8 +40,8 @@ public class History extends Endpoint<JsonNode, PNHistoryResult> {
3940
@Setter
4041
private Boolean includeTimetoken;
4142

42-
public History(PubNub pubnubInstance) {
43-
super(pubnubInstance);
43+
public History(PubNub pubnub, Retrofit retrofit) {
44+
super(pubnub, retrofit);
4445
}
4546

4647
private interface HistoryService {
@@ -60,7 +61,7 @@ protected void validateParams() throws PubNubException {
6061
@Override
6162
protected Call<JsonNode> doWork(Map<String, String> params) {
6263

63-
HistoryService service = this.createRetrofit().create(HistoryService.class);
64+
HistoryService service = this.getRetrofit().create(HistoryService.class);
6465

6566
if (reverse != null) {
6667
params.put("reverse", String.valueOf(reverse));

‎src/main/java/com/pubnub/api/endpoints/Time.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.pubnub.api.models.consumer.PNTimeResult;
88
import retrofit2.Call;
99
import retrofit2.Response;
10+
import retrofit2.Retrofit;
1011
import retrofit2.http.GET;
1112
import retrofit2.http.QueryMap;
1213

@@ -15,8 +16,8 @@
1516

1617
public class Time extends Endpoint<List<Long>, PNTimeResult> {
1718

18-
public Time(PubNub pubnub) {
19-
super(pubnub);
19+
public Time(PubNub pubnub, Retrofit retrofit) {
20+
super(pubnub, retrofit);
2021
}
2122

2223
private interface TimeService {
@@ -31,7 +32,7 @@ protected final void validateParams() throws PubNubException {
3132

3233
@Override
3334
protected final Call<List<Long>> doWork(Map<String, String> params) {
34-
TimeService service = this.createRetrofit().create(TimeService.class);
35+
TimeService service = this.getRetrofit().create(TimeService.class);
3536
return service.fetchTime(params);
3637
}
3738

‎src/main/java/com/pubnub/api/endpoints/access/Audit.java‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import lombok.experimental.Accessors;
1414
import retrofit2.Call;
1515
import retrofit2.Response;
16+
import retrofit2.Retrofit;
1617

1718
import java.util.ArrayList;
1819
import java.util.List;
@@ -28,8 +29,8 @@ public class Audit extends Endpoint<Envelope<AccessManagerAuditPayload>, PNAcces
2829
@Setter
2930
private String channelGroup;
3031

31-
public Audit(PubNub pubnub) {
32-
super(pubnub);
32+
public Audit(PubNub pubnub, Retrofit retrofit) {
33+
super(pubnub, retrofit);
3334
authKeys = new ArrayList<>();
3435
}
3536

@@ -82,7 +83,7 @@ protected Call<Envelope<AccessManagerAuditPayload>> doWork(Map<String, String> q
8283

8384
queryParams.put("signature", signature);
8485

85-
AccessManagerService service = this.createRetrofit().create(AccessManagerService.class);
86+
AccessManagerService service = this.getRetrofit().create(AccessManagerService.class);
8687
return service.audit(this.getPubnub().getConfiguration().getSubscribeKey(), queryParams);
8788
}
8889

@@ -106,14 +107,6 @@ protected PNAccessManagerAuditResult createResponse(final Response<Envelope<Acce
106107
return pnAccessManagerAuditResult.build();
107108
}
108109

109-
protected int getConnectTimeout() {
110-
return this.getPubnub().getConfiguration().getConnectTimeout();
111-
}
112-
113-
protected int getRequestTimeout() {
114-
return this.getPubnub().getConfiguration().getNonSubscribeRequestTimeout();
115-
}
116-
117110
@Override
118111
protected PNOperationType getOperationType() {
119112
return PNOperationType.PNAccessManagerAudit;

‎src/main/java/com/pubnub/api/endpoints/access/Grant.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import lombok.experimental.Accessors;
1818
import retrofit2.Call;
1919
import retrofit2.Response;
20+
import retrofit2.Retrofit;
2021

2122
import java.io.IOException;
2223
import java.util.ArrayList;
@@ -45,8 +46,8 @@ public class Grant extends Endpoint<Envelope<AccessManagerGrantPayload>, PNAcces
4546
@Setter
4647
private List<String> channelGroups;
4748

48-
public Grant(PubNub pubnub) {
49-
super(pubnub);
49+
public Grant(PubNub pubnub, Retrofit retrofit) {
50+
super(pubnub, retrofit);
5051
authKeys = new ArrayList<>();
5152
channels = new ArrayList<>();
5253
channelGroups = new ArrayList<>();
@@ -107,7 +108,7 @@ protected Call<Envelope<AccessManagerGrantPayload>> doWork(Map<String, String> q
107108

108109
queryParams.put("signature", signature);
109110

110-
AccessManagerService service = this.createRetrofit().create(AccessManagerService.class);
111+
AccessManagerService service = this.getRetrofit().create(AccessManagerService.class);
111112
return service.grant(this.getPubnub().getConfiguration().getSubscribeKey(), queryParams);
112113
}
113114

‎src/main/java/com/pubnub/api/endpoints/channel_groups/AddChannelChannelGroup.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import lombok.experimental.Accessors;
1313
import retrofit2.Call;
1414
import retrofit2.Response;
15+
import retrofit2.Retrofit;
1516

1617
import java.util.ArrayList;
1718
import java.util.List;
@@ -25,8 +26,8 @@ public class AddChannelChannelGroup extends Endpoint<Envelope, PNChannelGroupsAd
2526
private List<String> channels;
2627

2728

28-
public AddChannelChannelGroup(PubNub pubnub) {
29-
super(pubnub);
29+
public AddChannelChannelGroup(PubNub pubnub, Retrofit retrofit) {
30+
super(pubnub, retrofit);
3031
channels = new ArrayList<>();
3132
}
3233

@@ -42,7 +43,7 @@ protected void validateParams() throws PubNubException {
4243

4344
@Override
4445
protected Call<Envelope> doWork(final Map<String, String> params) {
45-
ChannelGroupService service = this.createRetrofit().create(ChannelGroupService.class);
46+
ChannelGroupService service = this.getRetrofit().create(ChannelGroupService.class);
4647

4748
if (channels.size() > 0) {
4849
params.put("add", PubNubUtil.joinString(channels, ","));

‎src/main/java/com/pubnub/api/endpoints/channel_groups/AllChannelsChannelGroup.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import lombok.experimental.Accessors;
1212
import retrofit2.Call;
1313
import retrofit2.Response;
14+
import retrofit2.Retrofit;
1415

1516
import java.util.ArrayList;
1617
import java.util.List;
@@ -21,8 +22,8 @@ public class AllChannelsChannelGroup extends Endpoint<Envelope<Object>, PNChanne
2122
@Setter
2223
private String channelGroup;
2324

24-
public AllChannelsChannelGroup(PubNub pubnub) {
25-
super(pubnub);
25+
public AllChannelsChannelGroup(PubNub pubnub, Retrofit retrofit) {
26+
super(pubnub, retrofit);
2627
}
2728

2829
@Override
@@ -34,7 +35,7 @@ protected void validateParams() throws PubNubException {
3435

3536
@Override
3637
protected Call<Envelope<Object>> doWork(Map<String, String> params) {
37-
ChannelGroupService service = this.createRetrofit().create(ChannelGroupService.class);
38+
ChannelGroupService service = this.getRetrofit().create(ChannelGroupService.class);
3839

3940
return service.allChannelsChannelGroup(this.getPubnub().getConfiguration().getSubscribeKey(), channelGroup, params);
4041
}

0 commit comments

Comments
 (0)