Commit 3718701a authored by Ivan Del Pino's avatar Ivan Del Pino Committed by Rafael Jesus Hernández Vasquez

refactor(glpi): remove duplicate code

Signed-off-by: default avatarIvan Del Pino <idelpino@teclib.com>
parent 27a7f4bd
......@@ -26,6 +26,7 @@
package org.glpi.api;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import com.google.gson.JsonArray;
......@@ -64,7 +65,6 @@ public class GLPI extends ServiceGenerator {
public GLPI(Context context, String glpiUrl) {
start(glpiUrl);
this.context = context;
interfaces = retrofit.create(Routes.class);
}
......@@ -75,49 +75,8 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void initSessionByUserToken(String userToken, final InitSessionCallback callback) {
initSessionByUserToken(null, userToken, callback);
}
/**
* Request a session token to uses other api endpoints.
*
* @param appToken authorization string provided by the GLPI api configuration. [Optional]
* @param userToken defined in User Preference (See 'Remote access key' on GLPI)
* @param callback here you are going to get the asynchronous response
*/
public void initSessionByUserToken(String appToken, String userToken, final InitSessionCallback callback) {
this.appToken = appToken;
Call<InitSession> responseCall = interfaces.initSessionByUserToken("user_token " + userToken.trim());
responseCall.enqueue(new Callback<InitSession>() {
@Override
public void onResponse(Call<InitSession> call, Response<InitSession> response) {
if (response.isSuccessful()) {
try {
sessionToken = response.body().getSessionToken();
} catch (NullPointerException ex) {
Log.d("initSession", ex.getMessage());
}
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<InitSession> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
this.appToken = null;
responseInitSession(callback, interfaces.initSessionByUserToken("user_token " + userToken.trim()));
}
/**
......@@ -129,9 +88,7 @@ public class GLPI extends ServiceGenerator {
*/
public String initSessionByCredentialsSync(String user, String password) {
String authorization = Helpers.base64encode(user + ":" + password);
Call<InitSession> responseCall = interfaces.initSessionByCredentials("Basic " + authorization.trim());
try {
sessionToken = responseCall.execute().body().getSessionToken();
return sessionToken;
......@@ -148,67 +105,9 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void initSessionByCredentials(String user, String password, final InitSessionCallback callback) {
initSessionByCredentials(null, user, password, callback);
}
/**
* Request a session token to uses other api endpoints. with a couple login & password: 2 parameters to login with user authentication
*
* @param appToken authorization string provided by the GLPI api configuration. [Optional]
* @param user valid user on GLPI
* @param password valid password on GLPI
* @param callback here you are going to get the asynchronous response
*/
public void initSessionByCredentials(String appToken, String user, String password, final InitSessionCallback callback) {
this.appToken = appToken;
this.appToken = null;
String authorization = Helpers.base64encode(user + ":" + password);
Call<InitSession> responseCall = interfaces.initSessionByCredentials("Basic " + authorization.trim());
responseCall.enqueue(new Callback<InitSession>() {
@Override
public void onResponse(Call<InitSession> call, Response<InitSession> response) {
if (response.isSuccessful()) {
try {
sessionToken = response.body().getSessionToken();
} catch (NullPointerException ex) {
Log.d("initSession", ex.getMessage());
}
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<InitSession> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
/**
* Create a valid Array with common headers needs
*
* @return Map<String , String> with all the headers
*/
private Map<String, String> getHeader() {
Map<String, String> map = new HashMap<>();
map.put("Session-Token", this.sessionToken);
if (appToken != null) {
map.put("App-Token", appToken);
}
return map;
responseInitSession(callback, interfaces.initSessionByCredentials("Basic " + authorization.trim()));
}
/**
......@@ -217,29 +116,7 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void getMyProfiles(final JsonObjectCallback callback) {
Call<JsonObject> responseCall = interfaces.getMyProfiles(getHeader());
responseCall.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
responseJsonObject(callback, interfaces.getMyProfiles(getHeader()));
}
/**
......@@ -248,29 +125,7 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void getActiveProfile(final JsonObjectCallback callback) {
Call<JsonObject> responseCall = interfaces.getActiveProfile(getHeader());
responseCall.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
responseJsonObject(callback, interfaces.getActiveProfile(getHeader()));
}
/**
......@@ -279,29 +134,7 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void getMyEntities(final JsonObjectCallback callback) {
Call<JsonObject> responseCall = interfaces.getMyEntities(getHeader());
responseCall.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
responseJsonObject(callback, interfaces.getMyEntities(getHeader()));
}
/**
......@@ -310,29 +143,7 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void getActiveEntities(final JsonObjectCallback callback) {
Call<JsonObject> responseCall = interfaces.getActiveEntities(getHeader());
responseCall.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
responseJsonObject(callback, interfaces.getActiveEntities(getHeader()));
}
/**
......@@ -341,188 +152,31 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void getFullSession(final JsonObjectCallback callback) {
Call<JsonObject> responseCall = interfaces.getFullSession(getHeader());
responseCall.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
/**
* Return the current $CFG_GLPI.
*
* @param callback here you are going to get the asynchronous response
*/
public void getGlpiConfig(final JsonObjectCallback callback) {
Call<JsonObject> responseCall = interfaces.getGlpiConfig(getHeader());
responseCall.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
/**
* Destroy a session identified by a session token.
*
* @param callback here you are going to get the asynchronous response
*/
public void killSession(final VoidCallback callback) {
Call<Void> responseCall = interfaces.killSession(getHeader());
responseCall.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.isSuccessful()) {
GLPI.this.appToken = null;
GLPI.this.sessionToken = "";
callback.onResponse(context.getResources().getString(R.string.kill_session_success));
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
/**
* Return a collection of rows of the itemtype.
*
* @param itemType These are the item type available on GLPI
* @param callback here you are going to get the asynchronous response
*/
public void getAllItems(itemType itemType, final JsonArrayCallback callback) {
getAllItems(new GetAllItemQuery(GLPI.this.context), itemType, callback);
}
/**
* Return a collection of rows of the itemtype.
*
* @param options These are the parameters of this endpoint for example expand_dropdowns or get_hateoas
* @param itemType These are the item type available on GLPI
* @param callback here you are going to get the asynchronous response
*/
public void getAllItems(GetAllItemQuery options, itemType itemType, final JsonArrayCallback callback) {
Call<JsonArray> responseCall = interfaces.getAllItem(getHeader(), itemType.name(), options.getQuery());
responseCall.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
/**
* Return the instance fields of itemtype identified by id.
*
* @param itemType These are the item type available on GLPI
* @param id unique identifier of the itemtype
* @param callback here you are going to get the asynchronous response
*/
public void getItem(itemType itemType, String id, final JsonObjectCallback callback) {
getItem(new GetAnItemQuery(), itemType, id, callback);
}
/**
* Return the instance fields of itemtype identified by id.
*
* @param itemType These are the item type available on GLPI
* @param id unique identifier of the itemtype
* @param callback here you are going to get the asynchronous response
*/
public void getItem(String itemType, String id, final JsonObjectCallback callback) {
getItem(new GetAnItemQuery(), itemType, id, callback);
responseJsonObject(callback, interfaces.getFullSession(getHeader()));
}
/**
* Return the instance fields of itemtype identified by id.
* Return the current $CFG_GLPI.
*
* @param options These are the parameters of this endpoint for example expand_dropdowns or get_hateoas
* @param itemType These are the item type available on GLPI
* @param id unique identifier of the itemtype
* @param callback here you are going to get the asynchronous response
*/
public void getItem(GetAnItemQuery options, itemType itemType, String id, final JsonObjectCallback callback) {
getItem(options, itemType.name(), id, callback);
public void getGlpiConfig(final JsonObjectCallback callback) {
responseJsonObject(callback, interfaces.getGlpiConfig(getHeader()));
}
/**
* Return the instance fields of itemtype identified by id.
* Destroy a session identified by a session token.
*
* @param options These are the parameters of this endpoint for example expand_dropdowns or get_hateoas
* @param itemType These are the item type available on GLPI
* @param id unique identifier of the itemtype
* @param callback here you are going to get the asynchronous response
*/
public void getItem(GetAnItemQuery options, String itemType, String id, final JsonObjectCallback callback) {
Call<JsonObject> responseCall = interfaces.getAnItem(getHeader(), itemType, id, options.getQuery());
responseCall.enqueue(new Callback<JsonObject>() {
public void killSession(final VoidCallback callback) {
interfaces.killSession(getHeader()).enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
public void onResponse(@NonNull Call<Void> call, @NonNull Response<Void> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
GLPI.this.appToken = null;
GLPI.this.sessionToken = "";
callback.onResponse(context.getResources().getString(R.string.kill_session_success));
} else {
String errorMessage;
try {
......@@ -533,36 +187,44 @@ public class GLPI extends ServiceGenerator {
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
public void onFailure(Call<Void> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
/**
* Return a collection of rows of the sub_itemtype for the identified item.
* Return a collection of rows of the itemtype.
*
* @param itemType These are the item type available on GLPI
* @param id unique identifier of the parent itemtype
* @param subItemType These are the item type available on GLPI
* @param callback here you are going to get the asynchronous response
* @param itemType These are the item type available on GLPI
* @param callback here you are going to get the asynchronous response
*/
public void getSubItems(String itemType, String id, String subItemType, final JsonObjectCallback callback) {
getSubItems(new GetSubItemQuery(GLPI.this.context), itemType, id, subItemType, callback);
public void getAllItems(itemType itemType, final JsonArrayCallback callback) {
Map<String, String> query = new GetAllItemQuery(this.context).getQuery();
responseVoid(callback, interfaces.getAllItem(getHeader(), itemType.name(), query));
}
/**
* Return a collection of rows of the sub_itemtype for the identified item.
* Return the instance fields of itemtype identified by id.
*
* @param itemType These are the item type available on GLPI
* @param id unique identifier of the parent itemtype
* @param subItemType These are the item type available on GLPI
* @param callback here you are going to get the asynchronous response
* @param itemType These are the item type available on GLPI
* @param id unique identifier of the itemtype
* @param callback here you are going to get the asynchronous response
*/
public void getSubItems(itemType itemType, String id, itemType subItemType, final JsonObjectCallback callback) {
getSubItems(new GetSubItemQuery(GLPI.this.context), itemType, id, subItemType, callback);
public void getItem(itemType itemType, String id, final JsonObjectCallback callback) {
responseJsonObject(callback, interfaces.getAnItem(getHeader(), itemType.name(), id, new GetAnItemQuery().getQuery()));
}
/**
* Return the instance fields of itemtype identified by id.
*
* @param itemType These are the item type available on GLPI
* @param id unique identifier of the itemtype
* @param callback here you are going to get the asynchronous response
*/
public void getItem(String itemType, String id, final JsonObjectCallback callback) {
responseJsonObject(callback, interfaces.getAnItem(getHeader(), itemType, id, new GetAnItemQuery().getQuery()));
}
/**
......@@ -573,43 +235,20 @@ public class GLPI extends ServiceGenerator {
* @param subItemType These are the item type available on GLPI
* @param callback here you are going to get the asynchronous response
*/
public void getSubItems(GetSubItemQuery options, itemType itemType, String id, itemType subItemType, final JsonObjectCallback callback) {
getSubItems(options, itemType.name(), id, subItemType.name(), callback);
public void getSubItems(String itemType, String id, String subItemType, final JsonObjectCallback callback) {
responseJsonObject(callback, interfaces.getSubItem(getHeader(), itemType, id, subItemType, new GetSubItemQuery(this.context).getQuery()));
}
/**
* Return a collection of rows of the sub_itemtype for the identified item.
*
* @param options These are the parameters of this endpoint for example expand_dropdowns or get_hateoas
* @param itemType These are the item type available on GLPI
* @param id unique identifier of the parent itemtype
* @param subItemType These are the item type available on GLPI
* @param callback here you are going to get the asynchronous response
*/
public void getSubItems(GetSubItemQuery options, String itemType, String id, String subItemType, final JsonObjectCallback callback) {
Call<JsonObject> responseCall = interfaces.getSubItem(getHeader(), itemType, id, subItemType, options.getQuery());
responseCall.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
public void getSubItems(itemType itemType, String id, itemType subItemType, final JsonObjectCallback callback) {
responseJsonObject(callback, interfaces.getSubItem(getHeader(), itemType.name(), id, subItemType.name(), new GetSubItemQuery(this.context).getQuery()));
}
/**
......@@ -619,31 +258,9 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void changeActiveProfile(String profilesId, final VoidCallback callback) {
ChangeActiveProfileRequest requestPost = new ChangeActiveProfileRequest(profilesId);
Call<Void> responseCall = interfaces.changeActiveProfile(getHeader(), requestPost);
responseCall.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.isSuccessful()) {
callback.onResponse(context.getResources().getString(R.string.change_active_profile_success));
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
int message = R.string.change_active_profile_success;
responseVoid(callback, interfaces.changeActiveProfile(getHeader(), requestPost), message);
}
/**
......@@ -654,31 +271,9 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void changeActiveEntities(String entitiesId, Boolean is_recursive, final VoidCallback callback) {
ChangeActiveEntitiesRequest requestPost = new ChangeActiveEntitiesRequest(entitiesId, is_recursive.toString());
Call<Void> responseCall = interfaces.changeActiveEntities(getHeader(), requestPost);
responseCall.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.isSuccessful()) {
callback.onResponse(context.getResources().getString(R.string.change_active_entities_success));
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
int message = R.string.change_active_entities_success;
responseVoid(callback, interfaces.changeActiveEntities(getHeader(), requestPost), message);
}
/**
......@@ -689,29 +284,7 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void addItems(itemType itemType, Object payload, final JsonArrayCallback callback) {
Call<JsonArray> responseCall = interfaces.addItem(getHeader(), itemType.name(), payload);
responseCall.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
responseVoid(callback, interfaces.addItem(getHeader(), itemType.name(), payload));
}
/**
......@@ -723,29 +296,7 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/
public void updateItems(itemType itemType, String id, Object payload, final JsonArrayCallback callback) {
Call<JsonArray> responseCall = interfaces.updateItem(getHeader(), itemType.name(), id, payload);
responseCall.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
if (response.isSuccessful()) {
callback.onResponse(response.body());
} else {
String errorMessage;
try {
errorMessage = response.errorBody().string();
} catch (Exception ex) {
errorMessage = context.getResources().getString(R.string.error_generic);
}
callback.onFailure(errorMessage);
}
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
responseVoid(callback, interfaces.updateItem(getHeader(), itemType.name(), id, payload));
}
/**
......@@ -756,8 +307,64 @@ public class GLPI extends ServiceGenerator {
* @param callback here you are going to get the asynchronous response
*/