Skip to content

Commit

Permalink
Merge pull request #27 from loilo-inc/v0.8.1
Browse files Browse the repository at this point in the history
V0.8.1
  • Loading branch information
hayama-junpei authored Nov 2, 2016
2 parents e61d984 + 6facdbe commit c96f13b
Show file tree
Hide file tree
Showing 37 changed files with 570 additions and 125 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ via Gradle

```groovy
dependencies {
compile 'tv.loilo.promise:promise:0.7.0'
compile 'tv.loilo.promise:promise-support:0.7.0'
compile 'tv.loilo.promise:promise:0.8.1'
compile 'tv.loilo.promise:promise-support:0.8.1'
}
```

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ android_support_version=24.2.1

# for gradle-mvn-push.gradle
# Build and Push -> $ gradlew clean build uploadArchives
VERSION_NAME=0.7.0
#VERSION_NAME=0.7.0-SNAPSHOT
VERSION_CODE=18
VERSION_NAME=0.8.1
#VERSION_NAME=0.8.1-SNAPSHOT
VERSION_CODE=22
GROUP=tv.loilo.promise

POM_DESCRIPTION=A simple promise library for Android(Java and Kotlin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private static String decodeValue(@Nullable final String value) {

try {
return URLDecoder.decode(field, encode);
} catch (UnsupportedEncodingException e) {
} catch (final UnsupportedEncodingException e) {
return field;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,10 @@ public static int getCurrent(long rawCurrent, long rawMax) {

@Override
public String toString() {
return "HttpProgress{" +
"mPhase=" + mPhase +
", mCode=" + mCode +
", mBytesProceeded=" + mBytesProceeded +
", mContentLength=" + mContentLength +
", mMessage='" + mMessage + '\'' +
'}';
if (mPhase == Phase.RESPONSE) {
return String.valueOf(mPhase) + "(" + mCode + ") " + mBytesProceeded + "/" + mContentLength + " bytes";
}

return String.valueOf(mPhase) + " " + mBytesProceeded + "/" + mContentLength + " bytes";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,157 @@

package tv.loilo.promise.http;

import android.support.annotation.Nullable;

import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Protocol;

public class HttpResponseException extends RuntimeException {

@Nullable
private final String mRequestMethod;
@Nullable
private final HttpUrl mRequestUrl;
private final long mSentRequestAtMillis;
private final long mReceivedResponseAtMillis;
@Nullable
private final Protocol mProtocol;
private final int mCode;
@Nullable
private final String mResponseMessage;
@Nullable
private final Headers mHeaders;

public HttpResponseException(final int code) {
public HttpResponseException(@Nullable final String requestMethod,
@Nullable final HttpUrl requestUrl,
final long sentRequestAtMillis,
final long receivedResponseAtMillis,
@Nullable final Protocol protocol,
final int code,
@Nullable final String responseMessage,
@Nullable final Headers headers) {
mRequestMethod = requestMethod;
mRequestUrl = requestUrl;
mSentRequestAtMillis = sentRequestAtMillis;
mReceivedResponseAtMillis = receivedResponseAtMillis;
mProtocol = protocol;
mCode = code;
mResponseMessage = responseMessage;
mHeaders = headers;
}

public HttpResponseException(final int code, final String detailMessage) {
public HttpResponseException(@Nullable final String requestMethod,
@Nullable final HttpUrl requestUrl,
final long sentRequestAtMillis,
final long receivedResponseAtMillis,
@Nullable final Protocol protocol,
final int code,
@Nullable final String responseMessage,
@Nullable final Headers headers,
final String detailMessage) {
super(detailMessage);
mRequestMethod = requestMethod;
mRequestUrl = requestUrl;
mSentRequestAtMillis = sentRequestAtMillis;
mReceivedResponseAtMillis = receivedResponseAtMillis;
mProtocol = protocol;
mCode = code;
mResponseMessage = responseMessage;
mHeaders = headers;
}

public HttpResponseException(final int code, final String detailMessage, final Throwable throwable) {
public HttpResponseException(@Nullable final String requestMethod,
@Nullable final HttpUrl requestUrl,
final long sentRequestAtMillis,
final long receivedResponseAtMillis,
@Nullable final Protocol protocol,
final int code,
@Nullable final String responseMessage,
@Nullable final Headers headers,
final String detailMessage,
final Throwable throwable) {
super(detailMessage, throwable);
mRequestMethod = requestMethod;
mRequestUrl = requestUrl;
mSentRequestAtMillis = sentRequestAtMillis;
mReceivedResponseAtMillis = receivedResponseAtMillis;
mProtocol = protocol;
mCode = code;
mResponseMessage = responseMessage;
mHeaders = headers;
}

public HttpResponseException(final int code, final Throwable throwable) {
public HttpResponseException(@Nullable final String requestMethod,
@Nullable final HttpUrl requestUrl,
final long sentRequestAtMillis,
final long receivedResponseAtMillis,
@Nullable final Protocol protocol,
final int code,
@Nullable final String responseMessage,
@Nullable final Headers headers,
final Throwable throwable) {
super(throwable);
mRequestMethod = requestMethod;
mRequestUrl = requestUrl;
mSentRequestAtMillis = sentRequestAtMillis;
mReceivedResponseAtMillis = receivedResponseAtMillis;
mProtocol = protocol;
mCode = code;
mResponseMessage = responseMessage;
mHeaders = headers;
}

public HttpResponseException(final int code) {
this(null, null, -1L, -1L, null, code, null, null);
}

public HttpResponseException(final int code, final String detailMessage) {
this(null, null, -1L, -1L, null, code, null, null, detailMessage);
}

public HttpResponseException(final int code, final String detailMessage, final Throwable throwable) {
this(null, null, -1L, -1L, null, code, null, null, detailMessage, throwable);
}

public HttpResponseException(final int code, final Throwable throwable) {
this(null, null, -1L, -1L, null, code, null, null, throwable);
}

@Nullable
public String getRequestMethod() {
return mRequestMethod;
}

@Nullable
public HttpUrl getRequestUrl() {
return mRequestUrl;
}

public long getSentRequestAtMillis() {
return mSentRequestAtMillis;
}

public long getReceivedResponseAtMillis() {
return mReceivedResponseAtMillis;
}

@Nullable
public Protocol getProtocol() {
return mProtocol;
}

public int getCode() {
return mCode;
}

@Nullable
public String getResponseMessage() {
return mResponseMessage;
}

@Nullable
public Headers getHeaders() {
return mHeaders;
}
}
14 changes: 10 additions & 4 deletions promise-http/src/main/java/tv/loilo/promise/http/HttpTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,29 @@

public final class HttpTask {
private Call mCall;
private ResponseUnitMonitor.OnResponseListener mOnResponseListener;
private OnResponseListener mOnResponseListener;
private OnFailureListener mOnFailureListener;

public HttpTask(Call call) {
mCall = call;
}

public HttpTask setOnResponseListener(ResponseUnitMonitor.OnResponseListener listener) {
public HttpTask setOnResponseListener(OnResponseListener listener) {
mOnResponseListener = listener;
return this;
}

public HttpTask setOnFailureListener(OnFailureListener listener) {
mOnFailureListener = listener;
return this;
}

public <TValue> HttpTaskAs<TValue> filterBy(@NonNull final ResponseFilter<TValue> filter) {
return new HttpTaskAs<>(mCall, filter);
return new HttpTaskAs<>(mCall, filter, mOnFailureListener);
}

public <TValue extends ResponseUnit> HttpTaskAs<TValue> asResponseUnitBy(@NonNull final ResponseFilter<TValue> filter) {
final ResponseUnitMonitor.OnResponseListener listener = mOnResponseListener;
final OnResponseListener listener = mOnResponseListener;
if (listener != null) {
return filterBy(new ResponseUnitMonitor<>(filter, listener));
}
Expand Down
22 changes: 20 additions & 2 deletions promise-http/src/main/java/tv/loilo/promise/http/HttpTaskAs.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import java.io.IOException;
import java.util.concurrent.CancellationException;
Expand All @@ -41,11 +42,14 @@ public final class HttpTaskAs<TResponse> {
@NonNull
private final ResponseFilter<TResponse> mFilter;
@Nullable
private OnFailureListener mOnFailureListener;
@Nullable
private ProgressReporter<HttpProgress> mReporter;

public HttpTaskAs(@NonNull final Call call, @NonNull final ResponseFilter<TResponse> filter) {
public HttpTaskAs(@NonNull final Call call, @NonNull final ResponseFilter<TResponse> filter, @Nullable OnFailureListener onFailureListener) {
mCall = call;
mFilter = filter;
mOnFailureListener = onFailureListener;
}

public HttpTaskAs<TResponse> progress(@Nullable final ProgressReporter<HttpProgress> reporter) {
Expand All @@ -70,6 +74,13 @@ public void onFailure(Call call, IOException e) {
if (call.isCanceled()) {
deferrable.setCanceled();
} else {
if(mOnFailureListener != null){
try {
mOnFailureListener.onFailure(e);
} catch (final Throwable t){
Log.w("loilo-promise-http", "OnFailureListener: Error occurred.", t);
}
}
deferrable.setFailed(e);
}
}
Expand All @@ -95,7 +106,14 @@ public void onResponse(Call call, Response response) throws IOException {
} catch (final CancellationException e) {
deferrable.setCanceled();
return;
} catch (final Exception e) {
} catch (final Throwable e) {
if(mOnFailureListener != null){
try {
mOnFailureListener.onFailure(e);
} catch (final Throwable t){
Log.w("loilo-promise-http", "OnFailureListener: Error occurred.", t);
}
}
deferrable.setFailed(e);
return;
}
Expand Down
31 changes: 29 additions & 2 deletions promise-http/src/main/java/tv/loilo/promise/http/HttpUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static void ensureSuccessStatusCode(final int code) throws HttpResponseEx
return;
}

throw new HttpResponseException(code);
throw new HttpResponseException(code, Integer.toString(code));
}

public static void ensureSuccessStatusCode(@NonNull final Response response) throws HttpResponseException {
Expand All @@ -54,7 +54,34 @@ public static void ensureSuccessStatusCode(@NonNull final Response response) thr
return;
}

throw new HttpResponseException(code, response.toString());
throw new HttpResponseException(
response.request().method(),
response.request().url(),
response.sentRequestAtMillis(),
response.receivedResponseAtMillis(),
response.protocol(),
code,
response.message(),
response.headers(),
String.valueOf(response.protocol()).toUpperCase() + " " + code + " " + response.message());
}

public static void ensureSuccessStatusCode(@NonNull final ResponseUnit response) throws HttpResponseException {
final int code = response.getCode();
if (isSuccessful(code)) {
return;
}

throw new HttpResponseException(
response.getRequestMethod(),
response.getRequestUrl(),
response.getSentRequestAtMillis(),
response.getReceivedResponseAtMillis(),
response.getProtocol(),
code,
response.getMessage(),
response.getHeaders(),
String.valueOf(response.getProtocol()).toUpperCase() + " " + code + " " + response.getMessage());
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tv.loilo.promise.http;

public interface OnFailureListener {
void onFailure(Throwable e);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2015-2016 LoiLo inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package tv.loilo.promise.http;

public interface OnResponseListener {
void onResponse(ResponseUnit response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public ProgressRequestBody(@NonNull final RequestBody body) {
this(body, null);
}

public RequestBody getOriginalBody() {
return mBody;
}

@Override
public MediaType contentType() {
return mBody.contentType();
Expand Down
Loading

0 comments on commit c96f13b

Please sign in to comment.