2
2
3
3
import com .fasterxml .jackson .core .JsonProcessingException ;
4
4
import com .fasterxml .jackson .databind .ObjectMapper ;
5
+ import com .javadiscord .jdi .core .api .utils .CacheHandler ;
5
6
import com .javadiscord .jdi .internal .api .DiscordRequest ;
6
7
import com .javadiscord .jdi .internal .api .DiscordRequestDispatcher ;
7
8
import com .javadiscord .jdi .internal .api .DiscordResponse ;
8
9
import com .javadiscord .jdi .internal .api .DiscordResponseFuture ;
9
10
import com .javadiscord .jdi .internal .cache .Cache ;
10
11
11
- import java .lang .reflect .Field ;
12
12
import java .util .List ;
13
13
import java .util .Map ;
14
14
15
15
public class DiscordResponseParser {
16
16
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper ();
17
17
private final DiscordRequestDispatcher dispatcher ;
18
- private final Cache cache ;
18
+ private final CacheHandler cacheHandler ;
19
19
20
20
public DiscordResponseParser (DiscordRequestDispatcher dispatcher , Cache cache ) {
21
21
this .dispatcher = dispatcher ;
22
- this .cache = cache ;
22
+ this .cacheHandler = new CacheHandler ( cache ) ;
23
23
}
24
24
25
25
public <T > AsyncResponse <List <T >> callAndParseList (Class <T > clazz , DiscordRequest request ) {
@@ -31,10 +31,7 @@ public <T> AsyncResponse<List<T>> callAndParseList(Class<T> clazz, DiscordReques
31
31
try {
32
32
List <T > resultList = parseResponseFromList (clazz , response .body ());
33
33
asyncResponse .setResult (resultList );
34
- for (T result : resultList ) {
35
- cacheResult (result );
36
- }
37
- } catch (NoSuchFieldException e ) { // ignore
34
+ cacheHandler .cacheResult (resultList );
38
35
} catch (Exception e ) {
39
36
asyncResponse .setException (e );
40
37
}
@@ -62,10 +59,7 @@ public <T> AsyncResponse<List<T>> callAndParseMap(String key, DiscordRequest req
62
59
try {
63
60
List <T > resultList = parseResponseFromMap (key , response .body ());
64
61
asyncResponse .setResult (resultList );
65
- for (T result : resultList ) {
66
- cacheResult (result );
67
- }
68
- } catch (NoSuchFieldException e ) { // ignore
62
+ cacheHandler .cacheResult (resultList );
69
63
} catch (Exception e ){
70
64
asyncResponse .setException (e );
71
65
}
@@ -102,34 +96,15 @@ private <T> void success(
102
96
try {
103
97
T result = OBJECT_MAPPER .readValue (response .body (), type );
104
98
asyncResponse .setResult (result );
105
- cacheResult (result );
106
- } catch (NoSuchFieldException e ) { // ignore
107
- } catch (JsonProcessingException | IllegalAccessException e ) {
99
+ cacheHandler .cacheResult (result );
100
+ } catch (JsonProcessingException e ) {
108
101
asyncResponse .setException (e );
109
102
}
110
103
} else {
111
104
asyncResponse .setException (errorResponseException (response ));
112
105
}
113
106
}
114
107
115
- private <T > void cacheResult (T result ) throws IllegalAccessException , NoSuchFieldException {
116
- Field guildIdField = result .getClass ().getDeclaredField ("guildId" );
117
- Field idField = result .getClass ().getDeclaredField ("id" );
118
-
119
- long guildId = getLongFromField (guildIdField );
120
- long id = getLongFromField (idField );
121
-
122
- cache .getCacheForGuild (guildId ).add (id , result );
123
- }
124
-
125
- private long getLongFromField (Field field ) throws IllegalAccessException {
126
- field .setAccessible (true );
127
- if (field .getType () == String .class ) {
128
- return Long .parseLong ((String ) field .get (field .getName ()));
129
- }
130
- return (long ) field .get (field .getName ());
131
- }
132
-
133
108
private boolean isSuccessfulResponse (DiscordResponse response ) {
134
109
return response .status () >= 200 && response .status () < 300 ;
135
110
}
0 commit comments