Skip to content

Commit

Permalink
Merge pull request #49 from orhanobut/development
Browse files Browse the repository at this point in the history
Release 1.5-SNAPSHOT
  • Loading branch information
kardeslik committed Jan 30, 2015
2 parents 24f6a62 + 3d4abcf commit b663dbb
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 133 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

VERSION_NAME=1.4-SNAPSHOT
VERSION_CODE=5
VERSION_NAME=1.5-SNAPSHOT
VERSION_CODE=6
GROUP=com.orhanobut

POM_DESCRIPTION=Android Network Solution
Expand Down
52 changes: 45 additions & 7 deletions wasp/src/main/java/com/orhanobut/wasp/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,68 @@
* @author Orhan Obut
*/
final class Logger {

private static final int CHUNK_SIZE = 4000;
private static final String TAG = "Wasp";

static void d(String message) {
Log.d(TAG, message);
log(Log.DEBUG, message);
}

static void e(String message) {
Log.e(TAG, message);
log(Log.ERROR, message);
}

static void w(String message) {
Log.w(TAG, message);
log(Log.WARN, message);
}

static void i(String message) {
Log.i(TAG, message);
log(Log.INFO, message);
}

static void v(String message) {
Log.v(TAG, message);
log(Log.VERBOSE, message);
}

static void wtf(String message) {
Log.wtf(TAG, message);
log(Log.ASSERT, message);
}

private static void log(int logType, String message) {
int length = message.length();
if (length <= CHUNK_SIZE) {
logChunk(logType, message);
return;
}

for (int i = 0; i < length; i += CHUNK_SIZE) {
int end = Math.min(length, i + CHUNK_SIZE);
logChunk(logType, message.substring(i, end));
}
}

private static void logChunk(int logType, String chunk) {
switch (logType) {
case Log.ERROR:
Log.e(TAG, chunk);
break;
case Log.INFO:
Log.i(TAG, chunk);
break;
case Log.VERBOSE:
Log.v(TAG, chunk);
break;
case Log.WARN:
Log.w(TAG, chunk);
break;
case Log.ASSERT:
Log.wtf(TAG, chunk);
break;
case Log.DEBUG:
// Fall through, log debug by default
default:
Log.d(TAG, chunk);
break;
}
}
}
23 changes: 11 additions & 12 deletions wasp/src/main/java/com/orhanobut/wasp/MockFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.text.TextUtils;

import com.orhanobut.wasp.parsers.Parser;
import com.orhanobut.wasp.utils.CollectionUtils;
import com.orhanobut.wasp.utils.IOUtils;
import com.orhanobut.wasp.utils.JsonUtil;
Expand Down Expand Up @@ -41,21 +42,10 @@ static MockFactory getDefault(Context context) {
}

@Override
public <T> void invokeRequest(WaspRequest waspRequest, CallBack<T> callBack) {
public <T> void invokeRequest(WaspRequest waspRequest, CallBack<T> callBack, Parser parser) {
WaspMock mock = waspRequest.getMock();
int statusCode = mock.getStatusCode();

if (statusCode < 200 || statusCode > 299) {
callBack.onError(new WaspError(
"mock url",
statusCode,
new HashMap<String, String>(),
"Mock test fail",
new byte[0],
0));
return;
}

MethodInfo methodInfo = waspRequest.getMethodInfo();
String responseString;

Expand All @@ -81,6 +71,15 @@ public <T> void invokeRequest(WaspRequest waspRequest, CallBack<T> callBack) {
waspRequest.getUrl(), statusCode, Collections.EMPTY_MAP, responseString, responseString.length(), 0
);

if (statusCode < 200 || statusCode > 299) {
callBack.onError(new WaspError(
parser,
waspResponse,
"Mock error message!"
));
return;
}

callBack.onSuccess((T) waspResponse);
}

Expand Down
19 changes: 13 additions & 6 deletions wasp/src/main/java/com/orhanobut/wasp/NetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.orhanobut.wasp.parsers.Parser;
import com.orhanobut.wasp.utils.LogLevel;
import com.orhanobut.wasp.utils.NetworkMode;
import com.orhanobut.wasp.utils.RequestInterceptor;

import java.lang.reflect.InvocationHandler;
Expand Down Expand Up @@ -31,6 +32,7 @@ final class NetworkHandler implements InvocationHandler {
private final ClassLoader classLoader;
private final RequestInterceptor requestInterceptor;
private final LogLevel logLevel;
private final NetworkMode networkMode;

private NetworkHandler(Class<?> service, Wasp.Builder builder) {
this.service = service;
Expand All @@ -40,6 +42,7 @@ private NetworkHandler(Class<?> service, Wasp.Builder builder) {
this.endPoint = builder.getEndPointUrl();
this.requestInterceptor = builder.getRequestInterceptor();
this.logLevel = builder.getLogLevel();
this.networkMode = builder.getNetworkMode();

ClassLoader loader = service.getClassLoader();
this.classLoader = loader != null ? loader : ClassLoader.getSystemClassLoader();
Expand Down Expand Up @@ -106,23 +109,27 @@ public Object invoke(Object proxy, final Method method, Object[] args) throws Th
@Override
public void onSuccess(WaspResponse response) {
response.log(logLevel);
Object result = parser.fromJson(response.getBody(), methodInfo.getResponseObjectType());
new ResponseWrapper(callBack, result).submitResponse();
try {
Object result = parser.fromJson(response.getBody(), methodInfo.getResponseObjectType());
new ResponseWrapper(callBack, result).submitResponse();
} catch (Exception e) {
callBack.onError(new WaspError(parser, response, e.getMessage()));
}
}

@Override
public void onError(WaspError error) {
error.logWaspError(logLevel);
error.log(logLevel);
callBack.onError(error);
}
};

if (methodInfo.isMocked()) {
MockFactory.getDefault(context).invokeRequest(waspRequest, responseCallBack);
if (networkMode == NetworkMode.MOCK && methodInfo.isMocked()) {
MockFactory.getDefault(context).invokeRequest(waspRequest, responseCallBack, parser);
return null;
}

networkStack.invokeRequest(waspRequest, responseCallBack);
networkStack.invokeRequest(waspRequest, responseCallBack, parser);
return null;
}
}
4 changes: 3 additions & 1 deletion wasp/src/main/java/com/orhanobut/wasp/NetworkStack.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.orhanobut.wasp;

import com.orhanobut.wasp.parsers.Parser;

/**
* @author Orhan Obut
*/
interface NetworkStack {

<T> void invokeRequest(WaspRequest waspRequest, CallBack<T> callBack);
<T> void invokeRequest(WaspRequest waspRequest, CallBack<T> callBack, Parser parser);
}
21 changes: 11 additions & 10 deletions wasp/src/main/java/com/orhanobut/wasp/VolleyImageHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.orhanobut.wasp;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.ViewGroup;
Expand All @@ -8,32 +9,32 @@

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.orhanobut.wasp.utils.WaspBitmapCache;

/**
* @author Orhan Obut
*/
class VolleyImageHandler implements ImageHandler {

private static WaspBitmapCache bitmapCache;
private static ImageLoader imageLoader;

private ImageLoader imageLoader;
private WaspImage waspImage;

/**
* Current ImageContainer. (either in-flight or finished)
*/
private ImageLoader.ImageContainer imageContainer;

VolleyImageHandler(Context context) {
this.imageLoader = new ImageLoader(
Volley.newRequestQueue(context),
new WaspBitmapCache()
);
}

@Override
public void init(WaspImage waspImage) {
this.waspImage = waspImage;
if (bitmapCache == null) {
bitmapCache = new WaspBitmapCache();
}
if (imageLoader == null) {
imageLoader = new ImageLoader(VolleyNetworkStack.getRequestQueue(), bitmapCache);
}
}

@Override
Expand Down Expand Up @@ -123,7 +124,7 @@ public void run() {
waspImage.logSuccess(bitmap);
return;
}

int defaultImage = waspImage.getDefaultImage();
if (defaultImage != 0) {
imageView.setImageResource(defaultImage);
Expand Down
44 changes: 25 additions & 19 deletions wasp/src/main/java/com/orhanobut/wasp/VolleyNetworkStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.HttpStack;
import com.android.volley.toolbox.Volley;
import com.orhanobut.wasp.parsers.Parser;
import com.orhanobut.wasp.utils.WaspHttpStack;
import com.orhanobut.wasp.utils.WaspRetryPolicy;

Expand All @@ -29,28 +30,28 @@ final class VolleyNetworkStack implements NetworkStack {
private static final String METHOD_POST = "POST";
private static final String METHOD_DELETE = "DELETE";

private static RequestQueue requestQueue;
private RequestQueue requestQueue;

private VolleyNetworkStack(Context context, WaspHttpStack stack) {
requestQueue = Volley.newRequestQueue(context, (HttpStack) stack.getHttpStack());
// requestQueue = Volley.newRequestQueue(context);
// requestQueue = Volley.newRequestQueue(context);
}

static VolleyNetworkStack newInstance(Context context, WaspHttpStack stack) {
return new VolleyNetworkStack(context, stack);
}

static RequestQueue getRequestQueue() {
synchronized RequestQueue getRequestQueue() {
if (requestQueue == null) {
throw new NullPointerException("Wasp.Builder must be called");
}
return requestQueue;
}

private void addToQueue(final WaspRequest waspRequest, final CallBack callBack) {
private void addToQueue(final WaspRequest waspRequest, CallBack callBack, Parser parser) {
String url = waspRequest.getUrl();
int method = getMethod(waspRequest.getMethod());
VolleyListener listener = VolleyListener.newInstance(callBack, url);
VolleyListener listener = VolleyListener.newInstance(callBack, url, parser);
Request request = new VolleyRequest(method, url, waspRequest.getBody(), listener) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Expand Down Expand Up @@ -86,8 +87,8 @@ private <T> void addToQueue(Request<T> request) {
}

@Override
public <T> void invokeRequest(WaspRequest waspRequest, CallBack<T> callBack) {
addToQueue(waspRequest, callBack);
public <T> void invokeRequest(WaspRequest waspRequest, CallBack<T> callBack, Parser parser) {
addToQueue(waspRequest, callBack, parser);
}

private static class VolleyListener<T> implements
Expand All @@ -96,14 +97,16 @@ private static class VolleyListener<T> implements

private final CallBack callBack;
private final String url;
private final Parser parser;

private VolleyListener(CallBack callBack, String url) {
private VolleyListener(CallBack callBack, String url, Parser parser) {
this.callBack = callBack;
this.url = url;
this.parser = parser;
}

public static VolleyListener newInstance(CallBack callBack, String url) {
return new VolleyListener(callBack, url);
public static VolleyListener newInstance(CallBack callBack, String url, Parser parser) {
return new VolleyListener(callBack, url, parser);
}

@Override
Expand All @@ -114,25 +117,28 @@ public void onResponse(T response) {
@Override
public void onErrorResponse(VolleyError error) {
int statusCode = WaspError.INVALID_STATUS_CODE;
byte[] body = new byte[0];
String body = "";
int length = 0;
Map<String, String> headers = new HashMap<>();
long delay = 0;
if (error == null) {
callBack.onError(new WaspError(url, statusCode, headers, "No message", body, delay));
callBack.onError(new WaspError(
parser, new WaspResponse(url, statusCode, headers, body, length, delay), "No message"
));
return;
}
if (error.networkResponse != null) {
statusCode = error.networkResponse.statusCode;
headers = error.networkResponse.headers;
body = error.networkResponse.data;
length = error.networkResponse.data.length;
try {
body = new String(error.networkResponse.data, HttpHeaderParser.parseCharset(headers));
} catch (UnsupportedEncodingException e) {
body = "Unable to parse error body!!!!!";
}
}
callBack.onError(new WaspError(
url,
statusCode,
headers,
error.getMessage(),
body,
delay
parser, new WaspResponse(url, statusCode, headers, body, length, delay), error.getMessage()
));
}
}
Expand Down
Loading

0 comments on commit b663dbb

Please sign in to comment.