Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Wes2Lee1096 committed Apr 18, 2019
2 parents 7f39589 + 9fc4906 commit d8dd8da
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 122 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.joiintheclub.BackEnd;

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

import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -10,24 +11,70 @@ public class GroupRequest {
public boolean isPending;
private int RequestID;

JSONObject requestGET = new JSONObject();
AtomicReference<JSONObject> responseGET = new AtomicReference<>(new JSONObject());
JSONObject requestPOST = new JSONObject();
AtomicReference<JSONObject> responsePOST = new AtomicReference<>(new JSONObject());
//loadRequest called when a page needs to render any pending requests in the DB
public static boolean loadRequest(String leaderID) {

//input user ID requesting
//input Group ID requested
public static void sendRequest(String UserID, String GroupID){
Group x = new Group();
JSONObject requestGET = new JSONObject();
AtomicReference<JSONObject> responseGET = new AtomicReference<>(new JSONObject());

Membership y = new Membership();
//y.GetLeader(x.Get());
try {
requestGET.put("user_id", leaderID);
} catch (JSONException e) {
e.printStackTrace();
return false;
}

responseGET.set(Requester.requester("/group/request", "GET", requestGET));
try {
if (Requester.handleJSON(responseGET.get())) {
//Searches for "user_id" as a key in the responsePOST.
Object userID = responseGET.get().get("user_id");

if (userID.toString().equals("0")) {
//credentials not found, returning false
return false;
} else {
//Set the "user_id" from DB, login to true
//setUserID(userID.toString());
//setIsLoggedIn(true);
//System.out.println(getUserID());
return true;
}
} else {
//Error returned from the DB
return false;
}
} catch (JSONException e) {
e.printStackTrace();
return false;
}
}

//sendRequest called when a user submits a request to a group
public static boolean sendRequest(String userID, String groupID){

JSONObject requestPOST = new JSONObject();
AtomicReference<JSONObject> responsePOST = new AtomicReference<>(new JSONObject());

//use Requester to store Leader's ID to notify the leader
//push to requester a new request and its status
//Populate JSON request object with values passed into function
try {
requestPOST.put("user_id", userID);
requestPOST.put("group_id", groupID);
} catch (JSONException e) {
e.printStackTrace();
return false;
}

//Verifies email and password with the DB. Keeps response in loginResponseGET JSON object
responsePOST.set(Requester.requester("/group/request", "POST", requestPOST));

//Unpacks the Response message sent by the requester
//No expected response. If no error thrown, the request was created (returns true)
return Requester.handleJSON(responsePOST.get());
}

public static void acceptRequest(){
//update membership status
Group currentGroup=new Group();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
package com.example.joiintheclub.BackEnd;

import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import static android.support.constraint.Constraints.TAG;


public class HttpRequest extends AsyncTask<String, Void, String> {

//ON BACKGROUND Thread
@Override
protected String doInBackground(String... params) {
String stringUrl = params[0];
String requestMethod = params[1].toUpperCase();
String requestBodyStr = params[2];
String responseBodyStr = "";
String inputLine;

BufferedReader in = null;

Expand Down Expand Up @@ -78,23 +70,23 @@ protected String doInBackground(String... params) {
break;
}

while ((responseBodyStr = in.readLine()) != null) {
Log.i(TAG, responseBodyStr);
}
responseBodyStr = in.readLine();

in.close();

System.out.println("ON BACKGROUND THREAD:");
System.out.println(responseBodyStr);

con.disconnect();

}
catch(IOException e) {
e.printStackTrace();
}

return responseBodyStr;
}
//Sets the global responseStr from the BACKGROUND THREAD
Requester.responseStr = responseBodyStr;

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
return responseBodyStr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,19 @@

import java.util.concurrent.ExecutionException;


class Requester {

//Global responseStr is set from the background thread
static String responseStr = "";

//Call handleJSON to check if API returned an error
// @param JSONObject response : the JSON response from the server
// returns false if API returned an error
static boolean handleJSON(JSONObject response) {
return !response.has("err");
}

//Call requester to interface when needing to interface with the api
// @param String targetUrl : the specific api resource that the information is stored on.
// e.g. '/user' when needing to get, create, update, or delete user information
Expand All @@ -17,29 +28,32 @@ class Requester {
// returns null (if failed) or (if success) JSONObject requestBody : a json object of keys and values (see above) of information from the api
static JSONObject requester(String targetUrl, String method, JSONObject requestBody) {

//Url of api
String baseUrl = "http://35.185.248.192:5050/api";
String totalUrl = baseUrl + targetUrl;
String responseStr = "";
requesterHelper(targetUrl, method, requestBody);

HttpRequest request = new HttpRequest();
System.out.println(responseStr);

try {
responseStr = request.execute(totalUrl, method, requestBody.toString()).get();
} catch (ExecutionException e) {
e.printStackTrace();
return null;
} catch (InterruptedException e) {
return new JSONObject(responseStr);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}

//requesterHelper creates a background thread independent from the requester class
private static void requesterHelper(String targetUrl, String method, JSONObject requestBody) {

//Url of api
String baseUrl = "http://35.185.248.192:5050/api";
String totalUrl = baseUrl + targetUrl;

//Executes a background thread, making a request to the server
try {
return new JSONObject(responseStr);
} catch (JSONException e) {
new HttpRequest().execute(totalUrl, method, requestBody.toString()).get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
}
}

Expand Down
Loading

0 comments on commit d8dd8da

Please sign in to comment.