2020import com .google .gson .JsonElement ;
2121import com .google .gson .JsonObject ;
2222import com .google .gson .JsonParser ;
23+ import okhttp3 .OkHttpClient ;
24+ import okhttp3 .Request ;
25+ import okhttp3 .Response ;
26+ import okhttp3 .ResponseBody ;
2327import org .jetbrains .annotations .NotNull ;
2428import org .slf4j .Logger ;
2529import org .slf4j .LoggerFactory ;
2630
2731import java .io .IOException ;
28- import java .io .InputStreamReader ;
2932import java .io .Reader ;
30- import java .net .HttpURLConnection ;
31- import java .net .URL ;
3233import java .util .ArrayList ;
3334import java .util .HashMap ;
3435import java .util .List ;
4041 */
4142public final class ApResolver {
4243 private static final String BASE_URL = "http://apresolve.spotify.com/" ;
43- private static final Map <String , List <String >> pool = new HashMap <>(3 );
4444 private static final Logger LOGGER = LoggerFactory .getLogger (ApResolver .class );
45- private static volatile boolean poolReady = false ;
4645
47- public static void fillPool () throws IOException {
48- if (!poolReady ) request ("accesspoint" , "dealer" , "spclient" );
46+ private final OkHttpClient client ;
47+ private final Map <String , List <String >> pool = new HashMap <>(3 );
48+ private volatile boolean poolReady = false ;
49+
50+ public ApResolver (OkHttpClient client ) throws IOException {
51+ this .client = client ;
52+ fillPool ();
53+ }
54+
55+ private void fillPool () throws IOException {
56+ request ("accesspoint" , "dealer" , "spclient" );
4957 }
5058
51- public static void refreshPool () throws IOException {
59+ public void refreshPool () throws IOException {
5260 poolReady = false ;
5361 pool .clear ();
54- request ( "accesspoint" , "dealer" , "spclient" );
62+ fillPool ( );
5563 }
5664
5765 @ NotNull
@@ -62,8 +70,7 @@ private static List<String> getUrls(@NotNull JsonObject body, @NotNull String ty
6270 return list ;
6371 }
6472
65- @ NotNull
66- private static Map <String , List <String >> request (@ NotNull String ... types ) throws IOException {
73+ private void request (@ NotNull String ... types ) throws IOException {
6774 if (types .length == 0 ) throw new IllegalArgumentException ();
6875
6976 StringBuilder url = new StringBuilder (BASE_URL + "?" );
@@ -72,30 +79,30 @@ private static Map<String, List<String>> request(@NotNull String... types) throw
7279 url .append ("type=" ).append (types [i ]);
7380 }
7481
75- HttpURLConnection conn = (HttpURLConnection ) new URL (url .toString ()).openConnection ();
76- conn .connect ();
77-
78- try (Reader reader = new InputStreamReader (conn .getInputStream ())) {
79- JsonObject obj = JsonParser .parseReader (reader ).getAsJsonObject ();
80- HashMap <String , List <String >> map = new HashMap <>();
81- for (String type : types )
82- map .put (type , getUrls (obj , type ));
82+ Request request = new Request .Builder ()
83+ .url (url .toString ())
84+ .build ();
85+ try (Response response = client .newCall (request ).execute ()) {
86+ ResponseBody body = response .body ();
87+ if (body == null ) throw new IOException ("No body" );
88+ try (Reader reader = body .charStream ()) {
89+ JsonObject obj = JsonParser .parseReader (reader ).getAsJsonObject ();
90+ HashMap <String , List <String >> map = new HashMap <>();
91+ for (String type : types )
92+ map .put (type , getUrls (obj , type ));
93+
94+ synchronized (pool ) {
95+ pool .putAll (map );
96+ poolReady = true ;
97+ pool .notifyAll ();
98+ }
8399
84- synchronized (pool ) {
85- pool .putAll (map );
86- poolReady = true ;
87- pool .notifyAll ();
100+ LOGGER .info ("Loaded aps into pool: " + pool );
88101 }
89-
90- LOGGER .info ("Loaded aps into pool: " + pool );
91-
92- return map ;
93- } finally {
94- conn .disconnect ();
95102 }
96103 }
97104
98- private static void waitForPool () {
105+ private void waitForPool () {
99106 if (!poolReady ) {
100107 synchronized (pool ) {
101108 try {
@@ -108,7 +115,7 @@ private static void waitForPool() {
108115 }
109116
110117 @ NotNull
111- private static String getRandomOf (@ NotNull String type ) {
118+ private String getRandomOf (@ NotNull String type ) {
112119 waitForPool ();
113120
114121 List <String > urls = pool .get (type );
@@ -117,17 +124,17 @@ private static String getRandomOf(@NotNull String type) {
117124 }
118125
119126 @ NotNull
120- public static String getRandomDealer () {
127+ public String getRandomDealer () {
121128 return getRandomOf ("dealer" );
122129 }
123130
124131 @ NotNull
125- public static String getRandomSpclient () {
132+ public String getRandomSpclient () {
126133 return getRandomOf ("spclient" );
127134 }
128135
129136 @ NotNull
130- public static String getRandomAccesspoint () {
137+ public String getRandomAccesspoint () {
131138 return getRandomOf ("accesspoint" );
132139 }
133140}
0 commit comments