Skip to content
This repository has been archived by the owner on Aug 4, 2019. It is now read-only.

Commit

Permalink
Response class #9, authentication test #10, Add promise for async call
Browse files Browse the repository at this point in the history
…closed #8
  • Loading branch information
EhsanMashhadi committed Nov 13, 2017
1 parent 6c87af3 commit 3ed2829
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 27 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'com.android.library'
android {
compileSdkVersion 26
defaultConfig {
minSdkVersion 14
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
Expand Down Expand Up @@ -32,5 +32,7 @@ dependencies {
compile 'com.google.code.gson:gson:2.8.0'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.github.franmontiel:PersistentCookieJar:v1.0.1'
compile 'org.jdeferred:jdeferred-android-aar:1.2.6'


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import android.support.test.runner.AndroidJUnit4;

import com.google.gson.Gson;

import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -27,24 +26,47 @@ public class AuthenticatorTest {
@Test
public void Authentication() {

final CountDownLatch signal = new CountDownLatch(1);
final CountDownLatch countDownLatch = new CountDownLatch(1);

restAdapter = new RestAdapter("https://nc.carrene.com/apiv1/", Authenticator.getAuthenticator());

HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("email", "hamed@carrene.com");
hashMap.put("password", "123456");

try {
restAdapter.login(hashMap);
restAdapter.login(hashMap).then(result -> {
Assert.assertEquals(result.getStatus(), 200);
String token = result.getField("token");
Assert.assertNotNull(token);
//TODO fix create member
// Assert.assertTrue(restAdapter.authenticator.isAuthenticated());

countDownLatch.countDown();


}).fail(result -> {
countDownLatch.countDown();
});

try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}

} catch (AlreadyAuthenticatedError alreadyAuthenticatedError) {
alreadyAuthenticatedError.printStackTrace();
}


}

@After
public void logout() {

restAdapter.logout();
Assert.assertFalse(restAdapter.authenticator.isAuthenticated());
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package nuesoft.restfulpy.client.adapter;

import org.jdeferred.Deferred;
import org.jdeferred.DoneCallback;
import org.jdeferred.FailCallback;

import java.util.Map;

import nuesoft.restfulpy.client.exception.AlreadyAuthenticatedError;
import nuesoft.restfulpy.client.exception.AuthenticationRequiredError;
import nuesoft.restfulpy.client.model.BaseModel;
import nuesoft.restfulpy.client.webService.Authenticator;
import nuesoft.restfulpy.client.webService.Request;
import nuesoft.restfulpy.client.webService.Response;


/**
* Created by mysterious on 11/6/17.
Expand All @@ -28,12 +34,21 @@ public String getBaseUrl() {
return this.baseUrl;
}

public void login(Map<String, Object> credentials) throws AlreadyAuthenticatedError {
public Deferred<Response, Exception, Integer> login(Map<String, Object> credentials) throws AlreadyAuthenticatedError {

if (authenticator.isAuthenticated()) {
throw new AlreadyAuthenticatedError();
}
this.request("sessions", "POST").addParameters(credentials).send();
Deferred<Response, Exception, Integer> deferred = this.request("sessions", "POST").addParameters(credentials).send();
deferred.then(result -> {
this.authenticator.setJWT(result.getField("token"));
deferred.resolve(result);

}).fail(result -> {
this.authenticator.deleteJWT();
deferred.reject(result);
});
return deferred;
}

public Request request(String resources, String verb) {
Expand All @@ -47,7 +62,7 @@ public Request request(String resources, String verb) {

public void logout() {

authenticator.deleteJWT();
this.authenticator.deleteJWT();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@ public void removeAuthenticationHeaders(Request request) {
request.headers.newBuilder().removeAll(Authenticator.TOKEN_REQUEST_HEADER_KEY).build();
}

// public void checkResponse(Response response) {
//
// String token = response.headers.get(this.TOKEN_RESPONSE_HEADER_KEY);
// if (token != null) {
// this.token = token;
// }
// }

public boolean isAuthenticated() {

return this.member != null;
Expand Down
23 changes: 12 additions & 11 deletions app/src/main/java/nuesoft/restfulpy/client/webService/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.google.gson.Gson;

import org.jdeferred.Deferred;
import org.jdeferred.impl.DeferredObject;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.CountDownLatch;

import nuesoft.restfulpy.client.adapter.RestAdapter;
import nuesoft.restfulpy.client.exception.AuthenticationRequiredError;
Expand All @@ -16,6 +18,7 @@
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;


/**
* Created by mysterious on 8/24/17.
*/
Expand Down Expand Up @@ -94,10 +97,11 @@ public Request addParameters(Object payload) {
}


public void send() {
public Deferred<Response, Exception, Integer> send() {

OkHttpClient client = new OkHttpClient();
Deferred<Response, Exception, Integer> deferred = new DeferredObject<>();

OkHttpClient client = new OkHttpClient();
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder();

if (this.headers != null) {
Expand All @@ -123,27 +127,24 @@ public void send() {

requestBuilder.method(this.verb.toUpperCase(), requestBody).url(url);

CountDownLatch countDownLatch = new CountDownLatch(1);
client.newCall(requestBuilder.build()).enqueue(new Callback() {

@Override
public void onResponse(Call call, okhttp3.Response response) throws IOException {

countDownLatch.countDown();
Response myResponse = new Response(response);
deferred.resolve(myResponse);

}

@Override
public void onFailure(Call call, IOException e) {

countDownLatch.countDown();
deferred.reject(e);

}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}

return deferred;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package nuesoft.restfulpy.client.webService;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
* Created by mysterious on 8/23/17.
*/

public class Response {

private okhttp3.Response response;

//Saving because string() method can call just once
private String body;

private String json;

public Response(okhttp3.Response response) {

try {
this.response = response;
this.body = response.body().string();
this.json = null;
} catch (IOException e) {
e.printStackTrace();
}
}

public int getStatus() {

return this.response.code();
}

public String getHeader(String key) {

return this.response.headers().get(key);
}

public String getIdentity() {

return this.getHeader("X-Identity");
}

public boolean isAuthenticated() {

return getIdentity() != null;
}

public String getBody() {

if (this.getStatus() == 200 && this.body != null)
return this.body;

return null;
}

public String getField(String name) {
//TODO check for json array
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(this.getBody());
String value = (String) jsonObj.get(name);
return value;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}

0 comments on commit 3ed2829

Please sign in to comment.