[LIB-8] Add header X-Requested-By to HTTP requests
[snooker-score-api.git] / src / main / java / org / hedgecode / snooker / request / AbstractRequester.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.request;
18
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22 import java.net.URL;
23 import java.net.URLConnection;
24 import java.nio.charset.Charset;
25 import java.nio.charset.StandardCharsets;
26
27 /**
28  * Abstract Data Requester from the portal api.snooker.org.
29  *
30  * @author Dmitry Samoshin aka gotty
31  */
32 public abstract class AbstractRequester implements Requester {
33
34     protected static final String API_SNOOKER_URL = "http://api.snooker.org/";
35     protected static final Charset API_SNOOKER_CHARSET = StandardCharsets.UTF_8;
36
37     private static final String API_SNOOKER_HEADER_NAME = "X-Requested-By";
38     private static final String API_SNOOKER_HEADER_VALUE = "Hedgecode";
39
40     protected abstract String getRequestUrl(int id) throws RequestException;
41
42     protected abstract String getRequestUrl(RequestParams params) throws RequestException;
43
44     @Override
45     public String request(int id) throws RequestException {
46         return _request(
47                 getRequestUrl(id)
48         );
49     }
50
51     @Override
52     public String request(RequestParams params) throws RequestException {
53         return _request(
54                 getRequestUrl(params)
55         );
56     }
57
58     private String _request(String requestUrl) throws RequestException {
59         StringBuilder result = new StringBuilder();
60         try {
61             URL url = new URL(requestUrl);
62             URLConnection urlConnection = url.openConnection();
63             urlConnection.setRequestProperty(
64                     API_SNOOKER_HEADER_NAME, API_SNOOKER_HEADER_VALUE
65             );
66             BufferedReader br = new BufferedReader(
67                     new InputStreamReader(
68                             urlConnection.getInputStream(), API_SNOOKER_CHARSET
69                     )
70             );
71             String inputLine;
72             while ((inputLine = br.readLine()) != null) {
73                 result.append(inputLine);
74             }
75             br.close();
76         } catch (IOException e) {
77             throw new RequestException(requestUrl, e.getMessage());
78         }
79         return result.toString();
80     }
81
82     protected boolean isCorrectId(int id) {
83         return id > 0;
84     }
85
86 }