2 * Copyright (c) 2017. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.snooker.json;
19 import com.google.gson.Gson;
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.Rounds;
33 import org.hedgecode.snooker.api.Season;
34 import org.hedgecode.snooker.api.Seedings;
35 import org.hedgecode.snooker.api.SnookerScoreAPI;
36 import org.hedgecode.snooker.gson.SnookerGsonBuilder;
37 import org.hedgecode.snooker.request.RequestException;
38 import org.hedgecode.snooker.request.RequestParams;
39 import org.hedgecode.snooker.request.RequestType;
42 * Implementation of Interface {@link SnookerScoreAPI}
43 * with deserialize from input JSON strings.
45 * @author Dmitry Samoshin aka gotty
47 public class JsonSnookerScore implements SnookerScoreAPI {
49 public static final int UNKNOWN_PLAYER_ID = 376;
51 private final Gson GSON = SnookerGsonBuilder.build();
53 private static JsonSnookerScore _instance;
55 public static JsonSnookerScore getInstance() {
56 if (_instance == null)
57 _instance = new JsonSnookerScore();
61 protected JsonSnookerScore() {
65 public Event getEvent(int eventId) throws APIException {
68 events = GSON.fromJson(
69 JsonStringToken.token(
70 RequestType.request(RequestType.EVENT, eventId)
74 } catch (RequestException e) {
75 throw new APIException(
76 APIException.Type.REQUEST, e.getMessage()
79 return events.length > 0 ? events[0] : null;
83 public Match getMatch(int eventId, int roundId, int matchNumber) throws APIException {
86 matches = GSON.fromJson(
87 JsonStringToken.token(
91 eventId, roundId, matchNumber
97 } catch (RequestException e) {
98 throw new APIException(
99 APIException.Type.REQUEST, e.getMessage()
102 return matches.length > 0 ? matches[0] : null;
106 public Player getPlayer(int playerId) throws APIException {
109 players = GSON.fromJson(
110 JsonStringToken.token(
111 RequestType.request(RequestType.PLAYER, playerId)
115 } catch (RequestException e) {
116 throw new APIException(
117 APIException.Type.REQUEST, e.getMessage()
120 return players.length > 0 ? players[0] : null;
124 public Events getSeasonEvents(Season season) throws APIException {
127 events = new JsonEvents(
129 JsonStringToken.token(
131 RequestType.SEASON_EVENTS,
133 RequestType.SEASON_EVENTS, 0, season
140 } catch (RequestException e) {
141 throw new APIException(
142 APIException.Type.REQUEST, e.getMessage()
149 public Matches getEventMatches(int eventId) throws APIException {
152 matches = new JsonMatches(
154 JsonStringToken.token(
156 RequestType.EVENT_MATCHES,
163 } catch (RequestException e) {
164 throw new APIException(
165 APIException.Type.REQUEST, e.getMessage()
172 public OngoingMatches getOngoingMatches() throws APIException {
173 OngoingMatches ongoingMatches;
175 ongoingMatches = new JsonOngoingMatches(
177 JsonStringToken.token(
179 RequestType.ONGOING_MATCHES,
183 JsonOngoingMatch[].class
186 } catch (RequestException e) {
187 throw new APIException(
188 APIException.Type.REQUEST, e.getMessage()
191 return ongoingMatches;
195 public Matches getPlayerMatches(int playerId, Season season) throws APIException {
198 matches = new JsonMatches(
200 JsonStringToken.token(
202 RequestType.PLAYER_MATCHES,
204 RequestType.PLAYER_MATCHES, playerId, season
211 } catch (RequestException e) {
212 throw new APIException(
213 APIException.Type.REQUEST, e.getMessage()
220 public Players getEventPlayers(int eventId) throws APIException {
223 players = new JsonPlayers(
225 JsonStringToken.token(
227 RequestType.EVENT_PLAYERS,
234 } catch (RequestException e) {
235 throw new APIException(
236 APIException.Type.REQUEST, e.getMessage()
243 public Players getPlayers(Season season, PlayerCategory category) throws APIException {
246 players = new JsonPlayers(
248 JsonStringToken.token(
250 RequestType.SEASON_PLAYERS,
259 } catch (RequestException e) {
260 throw new APIException(
261 APIException.Type.REQUEST, e.getMessage()
268 public Rankings getRankings(Season season, RankingType type) throws APIException {
271 rankings = new JsonRankings(
273 JsonStringToken.token(
284 } catch (RequestException e) {
285 throw new APIException(
286 APIException.Type.REQUEST, e.getMessage()
293 public Rounds getEventRounds(int eventId) throws APIException {
296 rounds = new JsonRounds(
298 JsonStringToken.token(
300 RequestType.EVENT_ROUNDS,
307 } catch (RequestException e) {
308 throw new APIException(
309 APIException.Type.REQUEST, e.getMessage()
316 public Seedings getEventSeedings(int eventId) throws APIException {
319 seedings = new JsonSeedings(
321 JsonStringToken.token(
323 RequestType.EVENT_SEEDING,
330 } catch (RequestException e) {
331 throw new APIException(
332 APIException.Type.REQUEST, e.getMessage()
339 public void setCurrentSeason(Season season) throws APIException {
340 throw new APIException(
341 APIException.Type.INFO, "This feature is only available in Cached API"
346 public Season currentSeason() throws APIException {
347 throw new APIException(
348 APIException.Type.INFO, "This feature is only available in Cached API"