rest api improvement

pull/5/head
Felix Prahl-Kamps 2018-06-10 18:46:23 +02:00
parent d3c25b8be9
commit b87fb9a4c5
6 changed files with 295 additions and 195 deletions

View File

@ -12,10 +12,15 @@ import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.android.volley.VolleyError;
import de.overview.wg.its.mispauth.R;
import de.overview.wg.its.mispauth.adapter.ExtOrgAdapter;
import de.overview.wg.its.mispauth.auxiliary.PreferenceManager;
import de.overview.wg.its.mispauth.auxiliary.ReadableError;
import de.overview.wg.its.mispauth.model.Organisation;
import de.overview.wg.its.mispauth.network.MispRequest;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
@ -59,6 +64,7 @@ public class MainActivity extends AppCompatActivity {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
@ -108,7 +114,7 @@ public class MainActivity extends AppCompatActivity {
a.setDescription("Ferrari has nothing to share");
a.setSector("Fast cars");
a.setNationality("Italy");
a.setUserCount(67);
a.setLocal(false);
externalOrganisations = new Organisation[]{a};
}

View File

@ -153,7 +153,7 @@ public class SettingsActivity extends AppCompatActivity {
progressBar.setVisibility(View.VISIBLE);
request.myUserInformation(new MispRequest.UserInformationCallback() {
request.myUserInformation(new MispRequest.UserCallback() {
@Override
public void onResult(JSONObject myUserInformation) {
@ -163,7 +163,7 @@ public class SettingsActivity extends AppCompatActivity {
int orgID = user.getOrgId();
request.OrganisationInformation(orgID, new MispRequest.OrganisationInformationCallback() {
request.OrganisationInformation(orgID, new MispRequest.OrganisationCallback() {
@Override
public void onResult(JSONObject organisationInformation) {

View File

@ -2,7 +2,11 @@ package de.overview.wg.its.mispauth.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.android.volley.VolleyError;
import de.overview.wg.its.mispauth.R;
import de.overview.wg.its.mispauth.model.Organisation;
import de.overview.wg.its.mispauth.network.MispRequest;
import org.json.JSONObject;
public class SyncActivity extends AppCompatActivity {
@ -11,4 +15,20 @@ public class SyncActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sync);
}
private void uploadOrganisation(Organisation org) {
MispRequest mispRequest = MispRequest.Instance(this);
mispRequest.addOrganisation(org, new MispRequest.OrganisationCallback() {
@Override
public void onResult(JSONObject organisationInformation) {
}
@Override
public void onError(VolleyError volleyError) {
}
});
}
}

View File

@ -3,11 +3,34 @@ package de.overview.wg.its.mispauth.auxiliary;
import com.android.volley.AuthFailureError;
import com.android.volley.NoConnectionError;
import com.android.volley.VolleyError;
import org.json.JSONException;
import org.json.JSONObject;
import java.nio.charset.StandardCharsets;
public class ReadableError {
public static String toReadable(VolleyError volleyError) {
if (volleyError.networkResponse != null) {
try {
JSONObject response = new JSONObject(new String(volleyError.networkResponse.data, StandardCharsets.UTF_8));
JSONObject error = response.getJSONObject("errors");
String name = response.getString("name");
String errorName = error.getJSONArray("name").get(0).toString();
if(!errorName.equals("")) {
return errorName;
} else if (!name.equals("")) {
return name;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
if (volleyError instanceof NoConnectionError) {
return "Connection failed";
} else if (volleyError instanceof AuthFailureError) {

View File

@ -58,7 +58,6 @@ public class Organisation {
e.printStackTrace();
}
}
public JSONObject toJSON() {
JSONObject org = new JSONObject();

View File

@ -10,6 +10,7 @@ import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import de.overview.wg.its.mispauth.auxiliary.PreferenceManager;
import de.overview.wg.its.mispauth.model.Organisation;
import org.json.JSONException;
import org.json.JSONObject;
@ -21,12 +22,11 @@ import java.util.Map;
*/
public class MispRequest {
private static final String TAG = "MISP-TAG";
private static final String TAG = "DEBUG";
private static MispRequest instance;
private RequestQueue requestQueue;
private PreferenceManager preferenceManager;
private String serverUrl, apiKey;
/**
@ -35,13 +35,19 @@ public class MispRequest {
private MispRequest(Context context) {
requestQueue = Volley.newRequestQueue(context);
preferenceManager = PreferenceManager.Instance(context);
loadSavedCredentials();
}
private void loadSavedCredentials() {
serverUrl = preferenceManager.getMyServerUrl();
apiKey = preferenceManager.getMyServerApiKey();
}
/**
* @param orgId organisation ID on the MISP-Instance
* @param callback returns a single Organisation-JSON
*/
public void OrganisationInformation(int orgId, final OrganisationInformationCallback callback) {
public void OrganisationInformation(int orgId, final OrganisationCallback callback) {
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
@Override
@ -76,9 +82,10 @@ public class MispRequest {
/**
* Typically used to get the organisation linked with this user
*
* @param callback return user associated with this API-Key
*/
public void myUserInformation(final UserInformationCallback callback) {
public void myUserInformation(final UserCallback callback) {
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
@Override
@ -118,6 +125,43 @@ public class MispRequest {
requestQueue.add(r);
}
/**
* @param organisation The organisation that will be added
* @param callback returns complete organisation JSON
*/
public void addOrganisation(Organisation organisation, final OrganisationCallback callback) {
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
callback.onResult(response.getJSONObject("Organisation"));
return;
} catch (JSONException e) {
e.printStackTrace();
}
callback.onResult(response);
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callback.onError(error);
}
};
Request r = objectRequest(
Request.Method.POST,
serverUrl + "/admin/organisations/add",
organisation.toJSON(),
listener,
errorListener
);
requestQueue.add(r);
}
private JsonObjectRequest objectRequest(int method,
String url,
@ -147,7 +191,6 @@ public class MispRequest {
this.apiKey = apiKey;
}
public static MispRequest Instance(Context context) {
if (instance == null) {
instance = new MispRequest(context);
@ -157,12 +200,21 @@ public class MispRequest {
}
public interface OrganisationInformationCallback {
public interface IntegerCallback {
void onResult(int result);
void onError(VolleyError volleyError);
}
public interface OrganisationCallback {
void onResult(JSONObject organisationInformation);
void onError(VolleyError volleyError);
}
public interface UserInformationCallback {
public interface UserCallback {
void onResult(JSONObject myOrganisationInformation);
void onError(VolleyError volleyError);
}
}