Skip to content

Commit 6ff2444

Browse files
committed
Added UserSharedPreferenceAdapter for User's private data
Mixed other minor errors
1 parent ff6ec3d commit 6ff2444

File tree

4 files changed

+64
-30
lines changed

4 files changed

+64
-30
lines changed

drfapi/src/main/java/com/civilmachines/drfapi/DjangoBaseRequest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import android.content.Context;
2020
import android.support.annotation.Nullable;
2121

22-
import com.android.volley.AuthFailureError;
2322
import com.android.volley.NetworkResponse;
2423
import com.android.volley.Response;
2524
import com.android.volley.toolbox.JsonRequest;
@@ -44,7 +43,6 @@ public abstract class DjangoBaseRequest<T> extends JsonRequest<T> {
4443
private Context cont;
4544

4645
// Change these static variable to define how token is stored in Android app and sent on server
47-
public static String key_token = "token";
4846
public static String keyAuthorizationHeader = "Authorization";
4947
public static String keyTokenPrefix = "Bearer ";
5048

@@ -79,18 +77,17 @@ public DjangoBaseRequest(int method,
7977
* Sets Content-Type to application/json
8078
* Checks for presence of token in SharedPreferenceAdapter and sets it.
8179
* @return Map<String, String> a Map of headers
82-
* @throws AuthFailureError from super
8380
* @author Himanshu Shankar (https://himanshus.com)
8481
*/
8582
@Override
86-
public Map<String, String> getHeaders() throws AuthFailureError {
87-
SharedPreferenceAdapter shaPre;
83+
public Map<String, String> getHeaders() {
84+
UserSharedPreferenceAdapter shaPre;
8885

8986
Map<String, String> headers = new HashMap<>();
9087
headers.put("Content-Type", "application/json");
91-
shaPre = new SharedPreferenceAdapter(cont);
88+
shaPre = new UserSharedPreferenceAdapter(cont);
9289

93-
String token = shaPre.getString(key_token);
90+
String token = shaPre.getToken();
9491

9592
if(token != null){
9693
headers.put(keyAuthorizationHeader, keyTokenPrefix + token);

drfapi/src/main/java/com/civilmachines/drfapi/DjangoErrorListener.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
public abstract class DjangoErrorListener implements Response.ErrorListener {
3333

3434
public abstract void onNetworkError(String response);
35+
3536
public abstract void onAuthFailureError(String response);
37+
3638
public abstract void onTimeoutError(String response);
3739
public abstract void onNoConnectionError(String response);
3840
public abstract void onParseError(String response);
@@ -84,12 +86,15 @@ public void onErrorResponse(VolleyError error) {
8486
switch (statusCode) {
8587
case 400: {
8688
onBadRequestError("Server configuration has some error.");
89+
break;
8790
}
8891
case 404: {
8992
onNotFoundError("API Endpoint not found.");
93+
break;
9094
}
9195
default: {
9296
onDefaultHTMLError(response);
97+
break;
9398
}
9499
}
95100
} else {
@@ -103,23 +108,26 @@ public void onErrorResponse(VolleyError error) {
103108
// Method not allowed error
104109
onMethodNotAllowedError(error_response.optString("detail",
105110
"Invalid method used in request."));
111+
break;
106112
}
107113
case 404: {
108114
// Object not found error
109115
onNotFoundError(error_response.optString("detail",
110116
"Object with provided detail does not exists."));
117+
break;
111118
}
112119
case 400: {
113120
// Bad request error
114-
if (error_response.optString("detail") != null)
121+
if (error_response.optString("detail", null) != null)
115122
onBadRequestError(error_response.optString("detail"));
116123
else
117124
onBadRequestError(error_response);
125+
break;
118126
}
119127
case 403 | 401: {
120-
// Must trigger a logout signal
121128
onForbiddenError(error_response.optString("detail",
122129
"You're not allowed to make this request."));
130+
break;
123131
}
124132
case 422: {
125133
// Similar use case as of bad request, used in drf_user
@@ -130,16 +138,19 @@ else if (error_response.has("detail"))
130138
onUnprocessableEntityError(error_response.optString("detail"));
131139
else
132140
onUnprocessableEntityError(error_response);
141+
break;
133142

134143
}
135144
case 415: {
136145
// Request sent in unsupported media type, such as text
137146
onUnsupportedMediaTypeError(error_response.optString("detail",
138147
"Request sent in invalid format."));
148+
break;
139149
}
140150
default: {
141151
// Default case scenario
142152
onDefaultJsonError(error_response);
153+
break;
143154
}
144155
}
145156
} catch (JSONException ex) {
@@ -159,8 +170,15 @@ else if (error instanceof NoConnectionError)
159170
onNoConnectionError(response);
160171
else if (error instanceof NetworkError)
161172
onNetworkError(response);
162-
else if( error instanceof AuthFailureError)
163-
onAuthFailureError(response);
173+
else if (error instanceof AuthFailureError) {
174+
JSONObject error_response;
175+
try {
176+
error_response = new JSONObject(response);
177+
onForbiddenError(error_response.optString("detail", "Couldn't perform task because of permission error."));
178+
} catch (JSONException ex) {
179+
onAuthFailureError(response);
180+
}
181+
}
164182
else
165183
onDefaultError(response);
166184
}

drfapi/src/main/java/com/civilmachines/drfapi/SharedPreferenceAdapter.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
public class SharedPreferenceAdapter {
3030

31-
static private SharedPreferences main;
32-
static private SharedPreferences.Editor edit;
31+
SharedPreferences main;
32+
private SharedPreferences.Editor edit;
3333

3434
/**
3535
* This function initializes the shared preference with Activity
@@ -44,22 +44,11 @@ public SharedPreferenceAdapter(Context cont){
4444
}
4545

4646
public SharedPreferenceAdapter(Activity act, String FileName){
47-
main = act.getSharedPreferences(FileName, Activity.MODE_PRIVATE);
47+
main = act.getSharedPreferences(FileName, Activity.MODE_PRIVATE);
4848
}
4949

5050
public SharedPreferenceAdapter(Context cont, String AdapterName){
51-
main = cont.getSharedPreferences(AdapterName, Context.MODE_PRIVATE);
52-
}
53-
54-
/* public SharedPreferenceAdapter(BusinessRecyclerView_Adapter businessRecyclerView_adapter) {
55-
main = businessRecyclerView_adapter.onClick(View v);
56-
}
57-
*/
58-
59-
public boolean onLoggedIn(String val){
60-
edit = main.edit();
61-
edit.putString("login", val);
62-
return edit.commit();
51+
main = cont.getSharedPreferences(AdapterName, Context.MODE_PRIVATE);
6352
}
6453

6554
public int getInt(String key){
@@ -70,10 +59,6 @@ public String getString(String key){
7059
return main.getString(key, null);
7160
}
7261

73-
public String isLoggedIn(){
74-
return main.getString("token", null);
75-
}
76-
7762
public boolean saveData(String key, String val){
7863
edit = main.edit();
7964
edit.putString(key, val);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.civilmachines.drfapi;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.support.annotation.NonNull;
6+
7+
public class UserSharedPreferenceAdapter extends SharedPreferenceAdapter {
8+
public static String userPrivate = "user_private";
9+
public static String keyToken = "token";
10+
11+
public UserSharedPreferenceAdapter(Context cont) {
12+
super(cont, userPrivate);
13+
}
14+
15+
public UserSharedPreferenceAdapter(Activity act) {
16+
super(act, userPrivate);
17+
}
18+
19+
public boolean isLoggedIn() {
20+
return main.contains(keyToken) && super.getString(keyToken).length() > 10;
21+
}
22+
23+
public String getToken() {
24+
return getString(keyToken);
25+
}
26+
27+
public boolean saveToken(@NonNull String token) {
28+
return saveData(keyToken, token);
29+
}
30+
31+
public void logOut() {
32+
super.clearData();
33+
}
34+
}

0 commit comments

Comments
 (0)