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.request;
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
23 import java.net.URLConnection;
24 import java.nio.charset.Charset;
25 import java.nio.charset.StandardCharsets;
28 * Abstract Data Requester from the portal api.snooker.org.
30 * @author Dmitry Samoshin aka gotty
32 public abstract class AbstractRequester implements Requester {
34 protected static final String API_SNOOKER_URL = "http://api.snooker.org/";
35 protected static final Charset API_SNOOKER_CHARSET = StandardCharsets.UTF_8;
37 protected abstract String getRequestUrl(int id) throws RequestException;
39 protected abstract String getRequestUrl(RequestParams params) throws RequestException;
42 public String request(int id) throws RequestException {
49 public String request(RequestParams params) throws RequestException {
55 private String _request(String requestUrl) throws RequestException {
56 StringBuilder result = new StringBuilder();
58 URL url = new URL(requestUrl);
59 URLConnection urlConnection = url.openConnection();
60 BufferedReader br = new BufferedReader(
61 new InputStreamReader(
62 urlConnection.getInputStream(), API_SNOOKER_CHARSET
66 while ((inputLine = br.readLine()) != null) {
67 result.append(inputLine);
70 } catch (IOException e) {
71 throw new RequestException(requestUrl, e.getMessage());
73 return result.toString();
76 protected boolean isCorrectId(int id) {