[LIB-2] Add snooker-score-api source files
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / json / JsonSnookerScore.java
1 /*
2  * Copyright (c) 2017. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.snooker.json;
18
19 import com.google.gson.Gson;
20
21 import org.hedgecode.snooker.api.APIException;
22 import org.hedgecode.snooker.api.Event;
23 import org.hedgecode.snooker.api.Events;
24 import org.hedgecode.snooker.api.Match;
25 import org.hedgecode.snooker.api.Matches;
26 import org.hedgecode.snooker.api.OngoingMatches;
27 import org.hedgecode.snooker.api.Player;
28 import org.hedgecode.snooker.api.PlayerCategory;
29 import org.hedgecode.snooker.api.Players;
30 import org.hedgecode.snooker.api.RankingType;
31 import org.hedgecode.snooker.api.Rankings;
32 import org.hedgecode.snooker.api.Season;
33 import org.hedgecode.snooker.api.SnookerScoreAPI;
34 import org.hedgecode.snooker.gson.SnookerGsonBuilder;
35 import org.hedgecode.snooker.request.RequestException;
36 import org.hedgecode.snooker.request.RequestParams;
37 import org.hedgecode.snooker.request.RequestType;
38
39 /**
40  * Implementation of Interface {@link SnookerScoreAPI}
41  * with deserialize from input JSON strings.
42  *
43  * @author Dmitry Samoshin aka gotty
44  */
45 public class JsonSnookerScore implements SnookerScoreAPI {
46
47     public static final int UNKNOWN_PLAYER_ID = 376;
48
49     private final Gson GSON = SnookerGsonBuilder.build();
50
51     private static JsonSnookerScore _instance;
52
53     public static JsonSnookerScore getInstance() {
54         if (_instance == null)
55             _instance = new JsonSnookerScore();
56         return _instance;
57     }
58
59     protected JsonSnookerScore() {
60     }
61
62     @Override
63     public Event getEvent(int eventId) throws APIException {
64         Event[] events;
65         try {
66             events = GSON.fromJson(
67                     JsonStringToken.token(
68                             RequestType.request(RequestType.EVENT, eventId)
69                     ),
70                     JsonEvent[].class
71             );
72         } catch (RequestException e) {
73             throw new APIException(
74                     APIException.Type.REQUEST, e.getMessage()
75             );
76         }
77         return events.length > 0 ? events[0] : null;
78     }
79
80     @Override
81     public Match getMatch(int eventId, int roundId, int matchNumber) throws APIException {
82         Match[] matches;
83         try {
84             matches = GSON.fromJson(
85                     JsonStringToken.token(
86                             RequestType.request(
87                                     RequestType.MATCH,
88                                     new RequestParams(
89                                             eventId, roundId, matchNumber
90                                     )
91                             )
92                     ),
93                     JsonMatch[].class
94             );
95         } catch (RequestException e) {
96             throw new APIException(
97                     APIException.Type.REQUEST, e.getMessage()
98             );
99         }
100         return matches.length > 0 ? matches[0] : null;
101     }
102
103     @Override
104     public Player getPlayer(int playerId) throws APIException {
105         Player[] players;
106         try {
107             players = GSON.fromJson(
108                     JsonStringToken.token(
109                             RequestType.request(RequestType.PLAYER, playerId)
110                     ),
111                     JsonPlayer[].class
112             );
113         } catch (RequestException e) {
114             throw new APIException(
115                     APIException.Type.REQUEST, e.getMessage()
116             );
117         }
118         return players.length > 0 ? players[0] : null;
119     }
120
121     @Override
122     public Events getSeasonEvents(Season season) throws APIException {
123         Events events;
124         try {
125             events = new JsonEvents(
126                     GSON.fromJson(
127                             JsonStringToken.token(
128                                     RequestType.request(
129                                             RequestType.SEASON_EVENTS,
130                                             new RequestParams(
131                                                     RequestType.SEASON_EVENTS, 0, season
132                                             )
133                                     )
134                             ),
135                             JsonEvent[].class
136                     )
137             );
138         } catch (RequestException e) {
139             throw new APIException(
140                     APIException.Type.REQUEST, e.getMessage()
141             );
142         }
143         return events;
144     }
145
146     @Override
147     public Matches getEventMatches(int eventId) throws APIException {
148         Matches matches;
149         try {
150             matches = new JsonMatches(
151                     GSON.fromJson(
152                             JsonStringToken.token(
153                                     RequestType.request(
154                                             RequestType.EVENT_MATCHES,
155                                             eventId
156                                     )
157                             ),
158                             JsonMatch[].class
159                     )
160             );
161         } catch (RequestException e) {
162             throw new APIException(
163                     APIException.Type.REQUEST, e.getMessage()
164             );
165         }
166         return matches;
167     }
168
169     @Override
170     public OngoingMatches getOngoingMatches() throws APIException {
171         OngoingMatches ongoingMatches;
172         try {
173             ongoingMatches = new JsonOngoingMatches(
174                     GSON.fromJson(
175                             JsonStringToken.token(
176                                     RequestType.request(
177                                             RequestType.ONGOING_MATCHES,
178                                             null
179                                     )
180                             ),
181                             JsonOngoingMatch[].class
182                     )
183             );
184         } catch (RequestException e) {
185             throw new APIException(
186                     APIException.Type.REQUEST, e.getMessage()
187             );
188         }
189         return ongoingMatches;
190     }
191
192     @Override
193     public Matches getPlayerMatches(int playerId, Season season) throws APIException {
194         Matches matches;
195         try {
196             matches = new JsonMatches(
197                     GSON.fromJson(
198                             JsonStringToken.token(
199                                     RequestType.request(
200                                             RequestType.PLAYER_MATCHES,
201                                             new RequestParams(
202                                                     RequestType.PLAYER_MATCHES, playerId, season
203                                             )
204                                     )
205                             ),
206                             JsonMatch[].class
207                     )
208             );
209         } catch (RequestException e) {
210             throw new APIException(
211                     APIException.Type.REQUEST, e.getMessage()
212             );
213         }
214         return matches;
215     }
216
217     @Override
218     public Players getEventPlayers(int eventId) throws APIException {
219         Players players;
220         try {
221             players = new JsonPlayers(
222                     GSON.fromJson(
223                             JsonStringToken.token(
224                                     RequestType.request(
225                                             RequestType.EVENT_PLAYERS,
226                                             eventId
227                                     )
228                             ),
229                             JsonPlayer[].class
230                     )
231             );
232         } catch (RequestException e) {
233             throw new APIException(
234                     APIException.Type.REQUEST, e.getMessage()
235             );
236         }
237         return players;
238     }
239
240     @Override
241     public Players getPlayers(Season season, PlayerCategory category) throws APIException {
242         Players players;
243         try {
244             players = new JsonPlayers(
245                     GSON.fromJson(
246                             JsonStringToken.token(
247                                     RequestType.request(
248                                             RequestType.SEASON_PLAYERS,
249                                             new RequestParams(
250                                                     season, category
251                                             )
252                                     )
253                             ),
254                             JsonPlayer[].class
255                     )
256             );
257         } catch (RequestException e) {
258             throw new APIException(
259                     APIException.Type.REQUEST, e.getMessage()
260             );
261         }
262         return players;
263     }
264
265     @Override
266     public Rankings getRankings(Season season, RankingType type) throws APIException {
267         Rankings rankings;
268         try {
269             rankings = new JsonRankings(
270                     GSON.fromJson(
271                             JsonStringToken.token(
272                                     RequestType.request(
273                                             RequestType.RANKING,
274                                             new RequestParams(
275                                                     season, type
276                                             )
277                                     )
278                             ),
279                             JsonRanking[].class
280                     )
281             );
282         } catch (RequestException e) {
283             throw new APIException(
284                     APIException.Type.REQUEST, e.getMessage()
285             );
286         }
287         return rankings;
288     }
289
290     @Override
291     public void setCurrentSeason(Season season) throws APIException {
292         throw new APIException(
293                 APIException.Type.INFO, "This feature is only available in Cached API"
294         );
295     }
296
297     @Override
298     public Season currentSeason() throws APIException {
299         throw new APIException(
300                 APIException.Type.INFO, "This feature is only available in Cached API"
301         );
302     }
303
304 }