completed abstraction of http requests

This commit is contained in:
aj 2019-05-01 19:13:49 +01:00
parent 69627ffe96
commit 42d327c969
3 changed files with 349 additions and 494 deletions

View File

@ -52,21 +52,15 @@ public class FmNetwork {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getAlbum: " + name + " " + artist);
HttpRequest request;
try {
request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
.header("User-Agent", "fmframework").queryString("method", "album.getinfo")
.queryString("artist", artist).queryString("album", name).queryString("api_key", key)
.queryString("format", "json");
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("album", name);
parameters.put("artist", artist);
if (userName != null)
request.queryString("username", userName);
parameters.put("username", userName);
HttpResponse<JsonNode> response = request.asJson();
if (response.getStatus() == 200) {
JSONObject obj = new JSONObject(response.getBody().toString());
JSONObject obj = makeGetRequest("album.getinfo", parameters);
String nameIn;
String artistIn;
@ -102,8 +96,7 @@ public class FmNetwork {
builder.setListeners(albumJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
}
@ -112,8 +105,7 @@ public class FmNetwork {
builder.setPlayCount(albumJson.getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
}
@ -148,19 +140,6 @@ public class FmNetwork {
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
}
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR (getAlbum): " + name + " " + artist + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getAlbum): " + name + " " + artist + " HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
@ -175,26 +154,12 @@ public class FmNetwork {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getArtist: " + name);
//HttpRequest request;
// try {
// request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
// .header("User-Agent", "fmframework").queryString("method", "artist.getinfo")
// .queryString("artist", name).queryString("api_key", key).queryString("format", "json");
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("artist", name);
if (userName != null)
parameters.put("username", userName);
//request.queryString("username", userName);
// HttpResponse<JsonNode> response = request.asJson();
if (true) {
// if (response.getStatus() == 200) {
// JSONObject obj = new JSONObject(response.getBody().toString());
JSONObject obj = makeGetRequest("artist.getinfo", parameters);
@ -212,8 +177,7 @@ public class FmNetwork {
builder.setMbid(artistJson.getString("mbid"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
ConsoleHandler.getConsole().write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No MBID for " + artistName + " , " + e.getMessage());
}
@ -222,8 +186,7 @@ public class FmNetwork {
builder.setUrl(artistJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No Url for " + artistName + " , " + e.getMessage());
ConsoleHandler.getConsole().write("ERROR: No Url for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + artistName + " , " + e.getMessage());
}
@ -232,8 +195,7 @@ public class FmNetwork {
builder.setListeners(artistJson.getJSONObject("stats").getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No listeners for " + artistName + " , " + e.getMessage());
ConsoleHandler.getConsole().write("ERROR: No listeners for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + artistName + " , " + e.getMessage());
}
@ -264,8 +226,7 @@ public class FmNetwork {
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No wiki for " + artistName + " , " + e.getMessage());
ConsoleHandler.getConsole().write("ERROR: No wiki for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + artistName + " , " + e.getMessage());
}
@ -278,18 +239,6 @@ public class FmNetwork {
else
System.err.println("ERROR: Arist Name Not Found, " + e.getMessage());
}
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getArtist): " + name + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getArtist): " + name + " HTTP REQUEST ERROR");
return null;
}
// }
// catch (UnirestException e) {
// e.printStackTrace();
// }
return null;
@ -306,21 +255,15 @@ public class FmNetwork {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTrack: " + name + " " + artist);
HttpRequest request;
try {
request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
.header("User-Agent", "fmframework").queryString("method", "track.getinfo")
.queryString("artist", artist).queryString("track", name).queryString("api_key", key)
.queryString("format", "json");
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("artist", artist);
parameters.put("track", name);
if (userName != null)
request.queryString("username", userName);
parameters.put("username", userName);
HttpResponse<JsonNode> response = request.asJson();
if (response.getStatus() == 200) {
JSONObject obj = new JSONObject(response.getBody().toString());
JSONObject obj = makeGetRequest("track.getinfo", parameters);
String nameIn;
String artistIn;
@ -356,8 +299,7 @@ public class FmNetwork {
builder.setListeners(trackJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
}
@ -366,8 +308,7 @@ public class FmNetwork {
builder.setPlayCount(trackJson.getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
}
@ -402,19 +343,6 @@ public class FmNetwork {
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
}
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR (getTrack): " + name + " " + artist + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getTrack): " + name + " " + artist + " HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
@ -479,15 +407,31 @@ public class FmNetwork {
protected JSONObject makeGetRequest(String method, HashMap<String, String> parameters) {
return makeGetRequest(method, parameters, null);
}
protected JSONObject makeGetRequest(String method, HashMap<String, String> parameters,
HashMap<String, String> headers) {
HttpRequest request;
try {
request = Unirest.get("http://ws.audioscrobbler.com/2.0/").header("Accept", "application/json")
.header("User-Agent", "fmframework").queryString("api_key", key).queryString("format", "json");
.header("User-Agent", "fmframework").queryString("method", method);
if (headers != null) {
for (String key : headers.keySet()) {
request = request.header(key, headers.get(key));
}
}
if (parameters != null) {
for (String key : parameters.keySet()) {
request = request.queryString(key, parameters.get(key));
System.out.println(key + " " + parameters.get(key));
}
}
request = request.queryString("api_key", key).queryString("format", "json");
HttpResponse<JsonNode> response = request.asJson();
@ -496,10 +440,11 @@ public class FmNetwork {
return new JSONObject(response.getBody().toString());
} else {
System.out.println(response.getBody());
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getAlbum): HTTP REQUEST ERROR");
ConsoleHandler.getConsole().write("ERROR : HTTP Request Error " + response.getStatus());
else
System.err.println("ERROR (getAlbum): HTTP REQUEST ERROR");
System.err.println("ERROR : HTTP Request Error " + response.getStatus());
return null;
}
} catch (UnirestException e) {
@ -508,4 +453,15 @@ public class FmNetwork {
return null;
}
protected class RequestParam {
int intparam;
String strparam;
public RequestParam(int param) {
}
}
}

View File

@ -3,6 +3,7 @@ package sarsoo.fmframework.fm;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
@ -70,33 +71,16 @@ public class FmUserNetwork extends FmNetwork {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getUser");
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getinfo").queryString("user", userName).queryString("api_key", key)
.queryString("format", "json").asJson();
HashMap<String, String> parameters = new HashMap<String, String>();
if (response.getStatus() == 200) {
parameters.put("user", userName);
JSONObject obj = new JSONObject(response.getBody().toString()).getJSONObject("user");
JSONObject obj = makeGetRequest("user.getinfo", parameters).getJSONObject("user");
return new User(obj.getString("name"), obj.getString("realname"), obj.getString("url"),
obj.getString("country"), obj.getInt("age"), obj.getString("gender").charAt(0),
obj.getInt("playcount"));
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getUser): HTTP REQUEST ERROR");
else
System.err.println("ERROR (getUser): HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
/**
@ -132,15 +116,12 @@ public class FmUserNetwork extends FmNetwork {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getLastTrack");
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getrecenttracks").queryString("user", userName)
.queryString("api_key", key).queryString("format", "json").queryString("limit", "1").asJson();
HashMap<String, String> parameters = new HashMap<String, String>();
if (response.getStatus() == 200) {
parameters.put("user", userName);
parameters.put("limit", "1");
JSONArray obj = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
JSONArray obj = makeGetRequest("user.getrecenttracks", parameters).getJSONObject("recenttracks")
.getJSONArray("track");
JSONObject track = (JSONObject) obj.get(0);
@ -151,19 +132,6 @@ public class FmUserNetwork extends FmNetwork {
return trackObj;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getLastTrack): HTTP REQUEST ERROR");
else
System.err.println("ERROR (getLastTrack): HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
/**
@ -180,33 +148,18 @@ public class FmUserNetwork extends FmNetwork {
ZoneId zoneId = ZoneId.systemDefault();
long epoch = local.atStartOfDay(zoneId).toEpochSecond();
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getrecenttracks").queryString("user", userName)
.queryString("from", epoch).queryString("limit", 1).queryString("api_key", key)
.queryString("format", "json").asJson();
HashMap<String, String> parameters = new HashMap<String, String>();
if (response.getStatus() == 200) {
parameters.put("user", userName);
parameters.put("from", Long.toString(epoch));
parameters.put("limit", "1");
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
.getJSONObject("@attr").getInt("total");
JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
return total;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getScrobblesToday): HTTP REQUEST ERROR");
else
System.err.println("ERROR (getScrobblesToday): HTTP REQUEST ERROR");
return 0;
}
} catch (UnirestException e) {
e.printStackTrace();
}
return 0;
}
/**
@ -227,40 +180,23 @@ public class FmUserNetwork extends FmNetwork {
long epoch = startDate.atStartOfDay(zoneId).toEpochSecond();
long endEpoch = epoch + (24 * 60 * 60);
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getrecenttracks").queryString("user", userName)
.queryString("from", epoch).queryString("to", endEpoch).queryString("limit", 1)
.queryString("api_key", key).queryString("format", "json").asJson();
HashMap<String, String> parameters = new HashMap<String, String>();
if (response.getStatus() == 200) {
parameters.put("user", userName);
parameters.put("from", Long.toString(epoch));
parameters.put("to", Long.toString(endEpoch));
parameters.put("limit", "1");
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
.getJSONObject("@attr").getInt("total");
JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
return total;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getScrobbleCountByDate): " + day + " " + month + " "
+ year + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getScrobbleCountByDate): " + day + " " + month + " " + year
+ " HTTP REQUEST ERROR");
return 0;
}
} catch (UnirestException e) {
e.printStackTrace();
}
return 0;
}
/**
* Returns scrobble count of day
* by today - {int day}
* Returns scrobble count of day by today - {int day}
*
* @param day Negative day offset
* @return Scrobble count
@ -276,34 +212,19 @@ public class FmUserNetwork extends FmNetwork {
epoch -= (day * (24 * 60 * 60));
long endEpoch = epoch + (24 * 60 * 60);
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getrecenttracks").queryString("user", userName)
.queryString("from", epoch).queryString("to", endEpoch).queryString("limit", 1)
.queryString("api_key", key).queryString("format", "json").asJson();
HashMap<String, String> parameters = new HashMap<String, String>();
if (response.getStatus() == 200) {
parameters.put("user", userName);
parameters.put("from", Long.toString(epoch));
parameters.put("to", Long.toString(endEpoch));
parameters.put("limit", "1");
int total = new JSONObject(response.getBody().toString()).getJSONObject("recenttracks")
.getJSONObject("@attr").getInt("total");
JSONObject obj = makeGetRequest("user.getrecenttracks", parameters);
int total = obj.getJSONObject("recenttracks").getJSONObject("@attr").getInt("total");
return total;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR (getScrobbleCountByDeltaDay): " + day + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getScrobbleCountByDeltaDay): " + day + " HTTP REQUEST ERROR");
return 0;
}
} catch (UnirestException e) {
e.printStackTrace();
}
return 0;
}
/**
@ -315,16 +236,13 @@ public class FmUserNetwork extends FmNetwork {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTags");
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.gettoptags").queryString("user", userName).queryString("api_key", key)
.queryString("format", "json").asJson();
HashMap<String, String> parameters = new HashMap<String, String>();
if (response.getStatus() == 200) {
parameters.put("user", userName);
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("toptags")
.getJSONArray("tag");
JSONObject obj = makeGetRequest("user.gettoptags", parameters);
JSONArray tagJsonArray = obj.getJSONObject("toptags").getJSONArray("tag");
JSONObject tagJson;
@ -343,20 +261,6 @@ public class FmUserNetwork extends FmNetwork {
return tags;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getTags): HTTP REQUEST ERROR");
else
System.err.println("ERROR (getTags): HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
/**
@ -369,16 +273,16 @@ public class FmUserNetwork extends FmNetwork {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTag: " + tagName);
try {
HttpResponse<JsonNode> response = Unirest.get("http://ws.audioscrobbler.com/2.0/")
.header("Accept", "application/json").header("User-Agent", "fmframework")
.queryString("method", "user.getpersonaltags").queryString("user", userName)
.queryString("tag", tagName).queryString("taggingtype", "artist").queryString("limit", 70)
.queryString("api_key", key).queryString("format", "json").asJson();
HashMap<String, String> parameters = new HashMap<String, String>();
if (response.getStatus() == 200) {
parameters.put("user", userName);
parameters.put("tag", tagName);
parameters.put("taggingtype", "artist");
parameters.put("limit", "70");
JSONArray tagJsonArray = new JSONObject(response.getBody().toString()).getJSONObject("taggings")
JSONObject obj = makeGetRequest("user.getpersonaltags", parameters);
JSONArray tagJsonArray = obj.getJSONObject("taggings")
.getJSONObject("artists").getJSONArray("artist");
JSONObject artistJson;
@ -403,20 +307,6 @@ public class FmUserNetwork extends FmNetwork {
return list;
} else {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR (getTag): " + tagName + " HTTP REQUEST ERROR");
else
System.err.println("ERROR (getTag): " + tagName + " HTTP REQUEST ERROR");
return null;
}
} catch (UnirestException e) {
e.printStackTrace();
}
return null;
}
}

View File

@ -36,4 +36,13 @@ public class FmNetworkTest {
assertEquals(artist.getName(), "Kendrick Lamar");
}
@Test
public void testNullParameters() {
FmNetwork network = new FmNetwork(Key.getKey());
network.makeGetRequest("artist.getinfo", null);
assertTrue(true);
}
}