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;
26 * Abstract Data Requester from the portal api.snooker.org.
28 * @author Dmitry Samoshin aka gotty
30 public abstract class AbstractRequester implements Requester {
32 protected static final String API_SNOOKER_URL = "http://api.snooker.org/";
34 protected abstract String getRequestUrl(int id) throws RequestException;
36 protected abstract String getRequestUrl(RequestParams params) throws RequestException;
39 public String request(int id) throws RequestException {
46 public String request(RequestParams params) throws RequestException {
52 private String _request(String requestUrl) throws RequestException {
53 StringBuilder result = new StringBuilder();
55 URL url = new URL(requestUrl);
56 URLConnection urlConnection = url.openConnection();
57 BufferedReader br = new BufferedReader(
58 new InputStreamReader(
59 urlConnection.getInputStream()
63 while ((inputLine = br.readLine()) != null) {
64 result.append(inputLine);
67 } catch (IOException e) {
68 throw new RequestException(requestUrl, e.getMessage());
70 return result.toString();
73 protected boolean isCorrectId(int id) {