Small refactoring

This commit is contained in:
Dima-1 2020-11-22 23:51:42 +02:00
parent 4a3de9a51b
commit 5d69794e27

View file

@ -98,12 +98,10 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil {
return editingPOI(getUserDetailsApi(), GET, "validate_login", false); return editingPOI(getUserDetailsApi(), GET, "validate_login", false);
} }
private OsmBugResult editingPOI(String url, String requestMethod, String userOperation, private OsmBugResult editingPOI(String url, String requestMethod, String userOperation, boolean anonymous) {
boolean anonymous) {
OsmOAuthAuthorizationAdapter authorizationAdapter = new OsmOAuthAuthorizationAdapter(app); OsmOAuthAuthorizationAdapter authorizationAdapter = new OsmOAuthAuthorizationAdapter(app);
OsmBugResult result = new OsmBugResult(); OsmBugResult result = new OsmBugResult();
if (!anonymous) { if (authorizationAdapter.isValidToken() && !anonymous) {
if (authorizationAdapter.isValidToken()) {
try { try {
result = performOAuthRequest(url, requestMethod, userOperation, authorizationAdapter); result = performOAuthRequest(url, requestMethod, userOperation, authorizationAdapter);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -118,7 +116,7 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil {
} }
} else { } else {
try { try {
result = performBasicRequest(url, requestMethod); result = performBasicRequest(url, requestMethod, anonymous);
} catch (FileNotFoundException | NullPointerException e) { } catch (FileNotFoundException | NullPointerException e) {
// that's tricky case why NPE is thrown to fix that problem httpClient could be used // that's tricky case why NPE is thrown to fix that problem httpClient could be used
String msg = app.getString(R.string.auth_failed); String msg = app.getString(R.string.auth_failed);
@ -132,19 +130,20 @@ public class OsmBugsRemoteUtil implements OsmBugsUtil {
result.warning = e.getMessage() + " link unavailable"; result.warning = e.getMessage() + " link unavailable";
} }
} }
}
return result; return result;
} }
private OsmBugResult performBasicRequest(String url, String requestMethod) throws IOException { private OsmBugResult performBasicRequest(String url, String requestMethod, boolean anonymous) throws IOException {
OsmBugResult result = new OsmBugResult(); OsmBugResult result = new OsmBugResult();
HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url); HttpURLConnection connection = NetworkUtils.getHttpURLConnection(url);
log.info("Editing poi " + url); log.info("Editing poi " + url);
connection.setConnectTimeout(15000); connection.setConnectTimeout(15000);
connection.setRequestMethod(requestMethod); connection.setRequestMethod(requestMethod);
connection.setRequestProperty("User-Agent", Version.getFullVersion(app)); connection.setRequestProperty("User-Agent", Version.getFullVersion(app));
if (!anonymous) {
String token = settings.USER_NAME.get() + ":" + settings.USER_PASSWORD.get(); String token = settings.USER_NAME.get() + ":" + settings.USER_PASSWORD.get();
connection.addRequestProperty("Authorization", "Basic " + Base64.encode(token.getBytes(StandardCharsets.UTF_8))); connection.addRequestProperty("Authorization", "Basic " + Base64.encode(token.getBytes(StandardCharsets.UTF_8)));
}
connection.setDoInput(true); connection.setDoInput(true);
connection.connect(); connection.connect();
String msg = connection.getResponseMessage(); String msg = connection.getResponseMessage();