77import com .pubnub .api .PubNubUtil ;
88import com .pubnub .api .builder .PubNubErrorBuilder ;
99import com .pubnub .api .callbacks .PNCallback ;
10+ import com .pubnub .api .endpoints .remoteaction .RemoteAction ;
1011import com .pubnub .api .enums .PNOperationType ;
1112import com .pubnub .api .enums .PNStatusCategory ;
1213import com .pubnub .api .managers .MapperManager ;
2122import org .jetbrains .annotations .Nullable ;
2223
2324import java .io .IOException ;
25+ import java .net .HttpURLConnection ;
2426import java .net .SocketException ;
2527import java .net .SocketTimeoutException ;
2628import java .net .UnknownHostException ;
4143import retrofit2 .Response ;
4244
4345@ Log
44- public abstract class Endpoint <Input , Output > {
46+ public abstract class Endpoint <Input , Output > implements RemoteAction < Output > {
4547
4648 @ Getter (AccessLevel .PROTECTED )
4749 private PubNub pubnub ;
@@ -68,10 +70,6 @@ public abstract class Endpoint<Input, Output> {
6870 @ Getter (AccessLevel .NONE )
6971 private boolean silenceFailures ;
7072
71- private static final int SERVER_RESPONSE_SUCCESS = 200 ;
72- private static final int SERVER_RESPONSE_FORBIDDEN = 403 ;
73- private static final int SERVER_RESPONSE_BAD_REQUEST = 400 ;
74-
7573 private MapperManager mapper ;
7674
7775 public Endpoint (PubNub pubnubInstance , TelemetryManager telemetry , RetrofitManager retrofitInstance ) {
@@ -81,6 +79,7 @@ public Endpoint(PubNub pubnubInstance, TelemetryManager telemetry, RetrofitManag
8179 this .telemetryManager = telemetry ;
8280 }
8381
82+ @ Override
8483 @ Nullable
8584 public Output sync () throws PubNubException {
8685 this .validateParams ();
@@ -99,7 +98,7 @@ public Output sync() throws PubNubException {
9998 .build ();
10099 }
101100
102- if (! serverResponse . isSuccessful () || serverResponse . code () != SERVER_RESPONSE_SUCCESS ) {
101+ if (isError ( serverResponse ) ) {
103102 String responseBodyText ;
104103 JsonElement responseBody ;
105104
@@ -130,6 +129,7 @@ public Output sync() throws PubNubException {
130129 return response ;
131130 }
132131
132+ @ Override
133133 public void async (@ NotNull final PNCallback <Output > callback ) {
134134 cachedCallback = callback ;
135135
@@ -149,7 +149,7 @@ public void async(@NotNull final PNCallback<Output> callback) {
149149 public void onResponse (Call <Input > performedCall , Response <Input > response ) {
150150 Output callbackResponse ;
151151
152- if (! response . isSuccessful () || response . code () != SERVER_RESPONSE_SUCCESS ) {
152+ if (isError ( response ) ) {
153153
154154 String responseBodyText ;
155155 JsonElement responseBody ;
@@ -182,7 +182,7 @@ public void onResponse(Call<Input> performedCall, Response<Input> response) {
182182 .statusCode (response .code ())
183183 .build ();
184184
185- if (response .code () == SERVER_RESPONSE_FORBIDDEN ) {
185+ if (response .code () == HttpURLConnection . HTTP_FORBIDDEN ) {
186186 pnStatusCategory = PNStatusCategory .PNAccessDeniedCategory ;
187187
188188 if (responseBodyPayload != null && mapper .hasField (responseBodyPayload , "channels" )) {
@@ -207,7 +207,7 @@ public void onResponse(Call<Input> performedCall, Response<Input> response) {
207207
208208 }
209209
210- if (response .code () == SERVER_RESPONSE_BAD_REQUEST ) {
210+ if (response .code () == HttpURLConnection . HTTP_BAD_REQUEST ) {
211211 pnStatusCategory = PNStatusCategory .PNBadRequestCategory ;
212212 }
213213
@@ -271,6 +271,7 @@ public void onFailure(Call<Input> performedCall, Throwable throwable) {
271271 });
272272 }
273273
274+ @ Override
274275 public void retry () {
275276 silenceFailures = false ;
276277 async (cachedCallback );
@@ -279,13 +280,18 @@ public void retry() {
279280 /**
280281 * cancel the operation but do not alert anybody, useful for restarting the heartbeats and subscribe loops.
281282 */
283+ @ Override
282284 public void silentCancel () {
283285 if (call != null && !call .isCanceled ()) {
284286 this .silenceFailures = true ;
285287 call .cancel ();
286288 }
287289 }
288290
291+ protected boolean isError (Response <Input > response ) {
292+ return response .code () != HttpURLConnection .HTTP_OK ;
293+ }
294+
289295 private PNStatus createStatusResponse (PNStatusCategory category , Response <Input > response , Exception throwable ,
290296 ArrayList <String > errorChannels , ArrayList <String > errorChannelGroups ) {
291297 PNStatus .PNStatusBuilder pnStatus = PNStatus .builder ();
0 commit comments