Skip to content

Commit

Permalink
Update the sample code for overriding the default Android network ser…
Browse files Browse the repository at this point in the history
…vice (#653)

* Give an example of handking the network request/response when exception happens

* typo

* format + rewording

* address review comments
  • Loading branch information
yangyansong-adbe authored Jul 26, 2024
1 parent 6ef49ec commit 4fb3b61
Showing 1 changed file with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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. 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

```java
class MyCustomResponse implements HttpConnecting {
Expand Down Expand Up @@ -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
Expand All @@ -137,22 +174,40 @@ class MyCustomNetworkService implements Networking {

private final ExecutorService executorService = //

/**
* 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.
*
*/
@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, 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);

// 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);
// 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 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) {
callback.call(new ErrorResponse());
}
}
});
}

/**
Expand Down

0 comments on commit 4fb3b61

Please sign in to comment.