Skip to content

Commit 65d707b

Browse files
committed
⭐ Added PUT and PATCH request!
1 parent dd4a32f commit 65d707b

2 files changed

Lines changed: 55 additions & 21 deletions

File tree

src/main/java/com/github/diegonighty/http/HttpCloseableConnection.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
import com.github.diegonighty.http.request.types.HttpDeleteRequest;
44
import com.github.diegonighty.http.request.types.HttpGetRequest;
5-
import com.github.diegonighty.http.request.types.HttpPostRequest;
5+
import com.github.diegonighty.http.request.types.HttpInputRequest;
66
import com.github.diegonighty.http.request.types.WrappedHttpDeleteRequest;
77
import com.github.diegonighty.http.request.types.WrappedHttpGetRequest;
8-
import com.github.diegonighty.http.request.types.WrappedHttpPostRequest;
8+
import com.github.diegonighty.http.request.types.WrappedHttpInputRequest;
9+
910
import java.io.IOException;
1011
import java.net.HttpURLConnection;
1112
import java.net.ProtocolException;
@@ -64,20 +65,50 @@ public HttpConnection<T> addRequestFields(Map<String, Object> map) {
6465
return this;
6566
}
6667

68+
/**
69+
* {@inheritDoc}
70+
*/
6771
@Override
6872
public HttpGetRequest<T> createGetRequest() {
6973
setMethod(HttpMethod.GET);
7074

7175
return new WrappedHttpGetRequest<>(connection);
7276
}
7377

78+
/**
79+
* {@inheritDoc}
80+
*/
7481
@Override
75-
public HttpPostRequest<T> createPostRequest() {
82+
public HttpInputRequest<T> createPostRequest() {
7683
setMethod(HttpMethod.POST);
7784

78-
return new WrappedHttpPostRequest<>(connection);
85+
return new WrappedHttpInputRequest<>(connection);
7986
}
8087

88+
/**
89+
* {@inheritDoc}
90+
*/
91+
@Override
92+
public HttpInputRequest<T> createPatchRequest() {
93+
connection.setRequestProperty("X-HTTP-Method-Override", "PATCH");
94+
setMethod(HttpMethod.POST);
95+
96+
return new WrappedHttpInputRequest<>(connection);
97+
}
98+
99+
/**
100+
* {@inheritDoc}
101+
*/
102+
@Override
103+
public HttpInputRequest<T> createPutRequest() {
104+
setMethod(HttpMethod.PUT);
105+
106+
return new WrappedHttpInputRequest<>(connection);
107+
}
108+
109+
/**
110+
* {@inheritDoc}
111+
*/
81112
@Override
82113
public HttpDeleteRequest createDeleteRequest() {
83114
setMethod(HttpMethod.DELETE);

src/main/java/com/github/diegonighty/http/HttpConnection.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,74 @@
22

33
import com.github.diegonighty.http.request.types.HttpDeleteRequest;
44
import com.github.diegonighty.http.request.types.HttpGetRequest;
5-
import com.github.diegonighty.http.request.types.HttpPostRequest;
5+
import com.github.diegonighty.http.request.types.HttpInputRequest;
66
import com.github.diegonighty.http.util.HeaderMap;
77
import java.util.Map;
88

99
public interface HttpConnection<T> {
1010

1111
/**
1212
* Add header to http request
13-
* @see <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields"> List of header fields and usage </a>
14-
*
1513
* @param field Field that will be added in the headers of the request
1614
* @param value Value of the field, this will be serialized to string
17-
*
1815
* @return The same connection with the changes
16+
* @see <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields"> List of header fields and usage </a>
1917
*/
2018
default <V> HttpConnection<T> addRequestField(RequestField field, V value) {
2119
return addRequestField(field.parse(), value);
2220
}
2321

2422
/**
2523
* Add header to http request
26-
* @see <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields"> List of header fields and usage </a>
27-
*
2824
* @param field Field that will be added in the headers of the request
2925
* @param value Value of the field, this will be serialized to string
30-
*
3126
* @return The same connection with the changes
27+
* @see <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields"> List of header fields and usage </a>
3228
*/
3329
<V> HttpConnection<T> addRequestField(String field, V value);
3430

3531
/**
3632
* Add headers to http request
37-
* @see <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields"> List of header fields and usage </a>
38-
*
3933
* @param map Map containing all fields and values, the values will be serialized to string
40-
*
4134
* @return The same connection with the changes
35+
* @see <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields"> List of header fields and usage </a>
4236
*/
4337
default HttpConnection<T> addRequestFields(HeaderMap map) {
4438
return addRequestFields(map.getHeaderMap());
4539
}
4640

4741
/**
4842
* Add headers to http request
49-
* @see <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields"> List of header fields and usage </a>
50-
*
5143
* @param map Map containing all fields and values, the values will be serialized to string
52-
*
5344
* @return The same connection with the changes
45+
* @see <a href="https://en.wikipedia.org/wiki/List_of_HTTP_header_fields"> List of header fields and usage </a>
5446
*/
5547
HttpConnection<T> addRequestFields(Map<String, Object> map);
5648

5749
/**
5850
* Performs a get request
59-
*
6051
* @return get request
6152
*/
6253
HttpGetRequest<T> createGetRequest();
6354

6455
/**
6556
* Performs a post request
66-
*
6757
* @return post request
6858
*/
69-
HttpPostRequest<T> createPostRequest();
59+
HttpInputRequest<T> createPostRequest();
60+
61+
/**
62+
* Performs a patch request
63+
* @return patch request (it really is a POST request, but spoofed with a header inside the connection)
64+
*/
65+
HttpInputRequest<T> createPatchRequest();
66+
67+
/**
68+
* Performs a put request
69+
*
70+
* @return put request
71+
*/
72+
HttpInputRequest<T> createPutRequest();
7073

7174
/**
7275
* Performs a delete request
@@ -83,7 +86,7 @@ enum HttpMethod {
8386

8487
GET,
8588
POST,
86-
UPDATE,
89+
PUT,
8790
DELETE
8891

8992
}

0 commit comments

Comments
 (0)