Skip to content

Commit

Permalink
added asynchronous API
Browse files Browse the repository at this point in the history
  • Loading branch information
faob-dev committed Mar 28, 2018
1 parent 46ee295 commit 7278954
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
@@ -1,5 +1,5 @@
group 'io.faob'
version '0.1.0'
version '0.2.0'

apply plugin: 'java'

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/io/faob/utopian/handlers/ErrorHandler.java
@@ -0,0 +1,5 @@
package io.faob.utopian.handlers;

public interface ErrorHandler<E> {
void handleError(E e);
}
5 changes: 5 additions & 0 deletions src/main/java/io/faob/utopian/handlers/ResponseHandler.java
@@ -0,0 +1,5 @@
package io.faob.utopian.handlers;

public interface ResponseHandler<T> {
void handleResponse(T r);
}
15 changes: 14 additions & 1 deletion src/main/java/io/faob/utopian/http/HttpManager.java
@@ -1,6 +1,9 @@
package io.faob.utopian.http;


import io.faob.utopian.handlers.ErrorHandler;
import io.faob.utopian.handlers.ResponseHandler;

/**
* Manager that handles http related stuff.
* <p>Calls can be executed synchronously with {@link #get}, or asynchronously (not implemented yet).
Expand All @@ -12,10 +15,20 @@
public interface HttpManager<T> {

/**
* Synchronously send the request and return its response.
* Synchronously sends request and return its response.
*
* @return Response body type.
* @throws Exception if a problem occurred talking to the server.
*/
T get() throws Exception;


/**
* Asynchronously sends request and notify {@code responseHandler} on response or
* notify {@code errorHandler} when some error occurred.
*
* @param responseHandler This lambda expression gets called on response
* @param errorHandler This lambda expression gets called on error
*/
void getAsync(ResponseHandler<T> responseHandler, ErrorHandler<Exception> errorHandler);
}
29 changes: 26 additions & 3 deletions src/main/java/io/faob/utopian/http/HttpManagerImpl.java
@@ -1,9 +1,11 @@
package io.faob.utopian.http;

import io.faob.utopian.handlers.ErrorHandler;
import io.faob.utopian.handlers.ResponseHandler;
import io.faob.utopian.type.TypeMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.*;

import java.io.IOException;

public class HttpManagerImpl<T> implements HttpManager<T> {
private String url;
Expand All @@ -21,4 +23,25 @@ public T get() throws Exception {
Response response = okHttpClient.newCall(request).execute();
return typeMapper.map(response.body().string());
}

@Override
public void getAsync(ResponseHandler<T> responseHandler, ErrorHandler<Exception> errorHandler) {
Request request = new Request.Builder().url(url).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
errorHandler.handleError(e);
}

@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String jsonString = response.body().string();
responseHandler.handleResponse(typeMapper.map(jsonString));
} catch (Exception e) {
errorHandler.handleError(e);
}
}
});
}
}

0 comments on commit 7278954

Please sign in to comment.