-
-
Notifications
You must be signed in to change notification settings - Fork 392
/
WebRequestor.java
156 lines (143 loc) · 5.07 KB
/
WebRequestor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (c) 2010-2020 Mark Allen, Norbert Bartels.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.restfb;
import static com.restfb.util.StringUtils.isBlank;
import static com.restfb.util.StringUtils.trimToEmpty;
import static java.lang.String.format;
import java.io.IOException;
import java.util.List;
/**
* Specifies how a class that sends {@code HTTP} requests to the Facebook API endpoint must operate.
*
* @author <a href="http://restfb.com">Mark Allen</a>
*/
public interface WebRequestor {
/**
* Encapsulates an HTTP response body and status code.
*
* @author <a href="http://restfb.com">Mark Allen</a>
*/
class Response {
/**
* HTTP response status code (e.g. 200).
*/
private Integer statusCode;
/**
* HTTP response body as text.
*/
private String body;
/**
* Creates a response with the given HTTP status code and response body as text.
*
* @param statusCode
* The HTTP status code of the response.
* @param body
* The response body as text.
*/
public Response(Integer statusCode, String body) {
this.statusCode = statusCode;
this.body = trimToEmpty(body);
}
/**
* Gets the HTTP status code.
*
* @return The HTTP status code.
*/
public Integer getStatusCode() {
return statusCode;
}
/**
* Gets the HTTP response body as text.
*
* @return The HTTP response body as text.
*/
public String getBody() {
return body;
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
if (isBlank(getBody())) {
return format("HTTP status code %d and an empty response body.", getStatusCode());
}
return format("HTTP status code %d and response body: %s", getStatusCode(), getBody());
}
}
/**
* Given a Facebook API endpoint URL, execute a {@code GET} against it.
*
* @param url
* The URL to make a {@code GET} request for, including URL parameters.
* @return HTTP response data.
* @throws IOException
* If an error occurs while performing the {@code GET} operation.
* @since 1.5
*/
Response executeGet(String url) throws IOException;
/**
* Given a Facebook API endpoint URL and parameter string, execute a {@code POST} to the endpoint URL.
*
* @param url
* The URL to {@code POST} to.
* @param parameters
* The parameters to be {@code POST}ed.
* @return HTTP response data.
* @throws IOException
* If an error occurs while performing the {@code POST}.
*/
Response executePost(String url, String parameters) throws IOException;
/**
* Given a Facebook API endpoint URL and parameter string, execute a {@code POST} to the endpoint URL.
*
* @param url
* The URL to {@code POST} to.
* @param parameters
* The parameters to be {@code POST}ed.
* @param binaryAttachments
* Optional binary attachments to be included in the {@code POST} body (e.g. photos and videos).
* @return HTTP response data.
* @throws IOException
* If an error occurs while performing the {@code POST}.
*/
Response executePost(String url, String parameters, List<BinaryAttachment> binaryAttachments) throws IOException;
/**
* Given a Facebook API endpoint URL and parameter string, execute a {@code DELETE} to the endpoint URL.
*
* @param url
* The URL to submit the {@code DELETE} to.
* @return HTTP response data.
* @throws IOException
* If an error occurs while performing the {@code DELETE}.
*/
Response executeDelete(String url) throws IOException;
/**
* Provides access to the facebook header information.
*
* The fields <code>x-fb-rev</code>, <code>x-fb-trace-id</code> and <code>x-fb-debug</code> are checked and returned
* in a single container of the type {@link DebugHeaderInfo}
*
* @return container with the explained facebook debug header information
*/
DebugHeaderInfo getDebugHeaderInfo();
}