From a1b574076412d66bc10f9067523ebdacc25eb2db Mon Sep 17 00:00:00 2001 From: yansong Date: Thu, 25 Jul 2024 18:24:37 -0500 Subject: [PATCH 1/4] Give an example of handking the network request/response when exception happens --- .../platform-services/tabs/network-service.md | 73 +++++++++++++++++-- 1 file changed, 65 insertions(+), 8 deletions(-) diff --git a/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md b/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md index 6a5ef14fe2..acac8112a4 100644 --- a/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md +++ b/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md @@ -54,7 +54,9 @@ networkService.connectAsync(networkRequest: networkRequest) { httpConnection in #### Java -1. Create a custom implementation of `HttpConnecting` that represents a response to a network request. This will be used to handle network completion when overriding the network stack in place of internal network connection implementation. +1. Create a custom implementation of `HttpConnecting` that represents a response to a network request. And, create natoher implementation of `HttpConnecting` that represents the error response. This will be used to handle network completion when overriding the network stack in place of internal network connection implementation. + +- MyCustomResponse ```java class MyCustomResponse implements HttpConnecting { @@ -127,6 +129,41 @@ class MyCustomResponse implements HttpConnecting { } ``` +- ErrorResponse + +```java +class ErrorResponse implements HttpConnecting { + + @Override + public InputStream getInputStream() { + return null; + } + + @Override + public InputStream getErrorStream() { + return null; + } + + @Override + public int getResponseCode() { + return -1; + } + + @Override + public String getResponseMessage() { + return ""; + } + + @Override + public String getResponsePropertyValue(String responsePropertyKey) { + return ""; + } + + @Override + public void close() { } +} +``` + 2. Create a custom implementation of `Networking` interface and, implement the `connectAsync` method. The implementation of `connectAsync` should handle the connection establishment with the details provided in the `NetworkRequest` and, notify the caller of a response using the `NetworkCallback` parameter. ```java @@ -137,22 +174,42 @@ class MyCustomNetworkService implements Networking { private final ExecutorService executorService = // + /** + * Perfmoring an asynchronous network request. + * + * @param request {@link NetworkRequest} used for network connection + * @param callback {@link NetworkCallback} that will receive the {@link HttpConnecting} instance asynchronously. + * + */ @Override public void connectAsync(final NetworkRequest request, final NetworkCallback callback) { // Use an executor service for initiating the network request and dispatching the response. executorService.submit( - () -> { + () -> { + try { + // 1. If the network is down, for example the device is under airplane mode, callback should be invoked immediately with a null connection. + // callback.call(null); + + // When the null connection is passed to the callback, the SDK drops the current request and not retry. + + // 2. If the network is available, the SDK should send out the request and invoke the callback with the corresponding connection. final HttpConnecting connection = doConnection(request); + if (callback != null) { + // If a callback was provided, invoke the callback with the connection + callback.call(connection); + } else { + // If no callback is passed by the client, close the connection. + connection.close(); + } + } catch (Exception e) { + // 3. The connectAsync method should never throw exceptions. Catch any exceptions and invoke the callback with an error response. if (callback != null) { - // If a callback was provided, invoke the callback with the connection - callback.call(connection); - } else { - // If no callback is passed by the client, close the connection. - connection.close(); + callback.call(new ErrorResponse()); } - }); + } + }); } /** From 4cc7ca3a4924461fa50b52287947afc52111b8f9 Mon Sep 17 00:00:00 2001 From: yansong Date: Thu, 25 Jul 2024 18:28:58 -0500 Subject: [PATCH 2/4] typo --- .../base/mobile-core/platform-services/tabs/network-service.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md b/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md index acac8112a4..436f6964ca 100644 --- a/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md +++ b/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md @@ -54,7 +54,7 @@ networkService.connectAsync(networkRequest: networkRequest) { httpConnection in #### Java -1. Create a custom implementation of `HttpConnecting` that represents a response to a network request. And, create natoher implementation of `HttpConnecting` that represents the error response. This will be used to handle network completion when overriding the network stack in place of internal network connection implementation. +1. Create a custom implementation of `HttpConnecting` that represents a response to a network request. Also, create antoher implementation of `HttpConnecting` that represents the error response. They will be used to handle network completion when overriding the network stack in place of internal network connection implementation. - MyCustomResponse From ad561454f53eefed898c618191a33dc30307c71b Mon Sep 17 00:00:00 2001 From: yansong Date: Thu, 25 Jul 2024 20:50:36 -0500 Subject: [PATCH 3/4] format + rewording --- .../mobile-core/platform-services/tabs/network-service.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md b/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md index 436f6964ca..1b4e33501b 100644 --- a/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md +++ b/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md @@ -56,7 +56,7 @@ networkService.connectAsync(networkRequest: networkRequest) { httpConnection in 1. Create a custom implementation of `HttpConnecting` that represents a response to a network request. Also, create antoher implementation of `HttpConnecting` that represents the error response. They will be used to handle network completion when overriding the network stack in place of internal network connection implementation. -- MyCustomResponse +* MyCustomResponse ```java class MyCustomResponse implements HttpConnecting { @@ -129,7 +129,7 @@ class MyCustomResponse implements HttpConnecting { } ``` -- ErrorResponse +* ErrorResponse ```java class ErrorResponse implements HttpConnecting { @@ -191,7 +191,7 @@ class MyCustomNetworkService implements Networking { // callback.call(null); - // When the null connection is passed to the callback, the SDK drops the current request and not retry. + // When the null connection is passed to the callback, the SDK will retry the same request at a later time. // 2. If the network is available, the SDK should send out the request and invoke the callback with the corresponding connection. final HttpConnecting connection = doConnection(request); From 901e5ab33e714fb70418c24a45f56abb2496634c Mon Sep 17 00:00:00 2001 From: yansong Date: Fri, 26 Jul 2024 17:47:32 -0500 Subject: [PATCH 4/4] address review comments --- .../platform-services/tabs/network-service.md | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md b/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md index 1b4e33501b..97db947cec 100644 --- a/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md +++ b/src/pages/home/base/mobile-core/platform-services/tabs/network-service.md @@ -175,7 +175,7 @@ class MyCustomNetworkService implements Networking { private final ExecutorService executorService = // /** - * Perfmoring an asynchronous network request. + * Initiate an asynchronous network request. * * @param request {@link NetworkRequest} used for network connection * @param callback {@link NetworkCallback} that will receive the {@link HttpConnecting} instance asynchronously. @@ -187,22 +187,20 @@ class MyCustomNetworkService implements Networking { executorService.submit( () -> { try { - // 1. If the network is down, for example the device is under airplane mode, callback should be invoked immediately with a null connection. + // 1. If the network is down, for example, if the device is in airplane mode, the callback should be invoked immediately with a null connection. When the null connection is passed to the callback, the SDK will treat it as a recoverable failure and handle it accordingly. // callback.call(null); - - // When the null connection is passed to the callback, the SDK will retry the same request at a later time. // 2. If the network is available, the SDK should send out the request and invoke the callback with the corresponding connection. final HttpConnecting connection = doConnection(request); - if (callback != null) { - // If a callback was provided, invoke the callback with the connection - callback.call(connection); - } else { - // If no callback is passed by the client, close the connection. - connection.close(); - } + if (callback != null) { + // If a callback was provided, invoke the callback with the connection + callback.call(connection); + } else { + // If no callback is passed by the client, close the connection. + connection.close(); + } } catch (Exception e) { // 3. The connectAsync method should never throw exceptions. Catch any exceptions and invoke the callback with an error response. if (callback != null) {