Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the sample code for overriding the default Android network service #653

Merged
merged 7 commits into from
Jul 26, 2024
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,42 @@ class MyCustomNetworkService implements Networking {

private final ExecutorService executorService = //

/**
* Perfmoring an asynchronous network request.
yangyansong-adbe marked this conversation as resolved.
Show resolved Hide resolved
*
* @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.
yangyansong-adbe marked this conversation as resolved.
Show resolved Hide resolved

// 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) {
yangyansong-adbe marked this conversation as resolved.
Show resolved Hide resolved
// 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());
}
});
}
});
}

/**
Expand Down
Loading