Skip to content

Commit 799d38e

Browse files
author
Francisco Solis
committed
Updates & Fixes
* Removed Old Classes * Created new Request Class * Updates to Response * Added Tests for different classes
1 parent 3848cbb commit 799d38e

File tree

6 files changed

+223
-175
lines changed

6 files changed

+223
-175
lines changed

src/main/kotlin/xyz/theprogramsrc/networkingmodule/builder/ConnectionBuilder.kt

Lines changed: 0 additions & 88 deletions
This file was deleted.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package xyz.theprogramsrc.networkingmodule.builder
2+
3+
import xyz.theprogramsrc.networkingmodule.objects.Response
4+
import java.net.HttpURLConnection
5+
import java.net.URL
6+
7+
/**
8+
* This class is used to build a request
9+
* @param url The url of the request
10+
*/
11+
class Request(val url: String){
12+
13+
/**
14+
* The method of the request.
15+
* Defaults to GET
16+
*/
17+
var method: String = "GET"
18+
19+
/**
20+
* The headers of the request.
21+
* Defaults to map with User-Agent
22+
*/
23+
var headers: MutableMap<String, String> = mutableMapOf(
24+
"User-Agent" to "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
25+
)
26+
27+
/**
28+
* The body of the request. (Set to empty string to ignore).
29+
* Defaults to empty string
30+
*/
31+
var body: String = ""
32+
33+
/**
34+
* The timeout of the request (Set to -1 to disable).
35+
* Defaults to -1
36+
*/
37+
var timeout: Int = -1
38+
39+
/**
40+
* True if the request should use the cache, false otherwise.
41+
* Defaults to true
42+
*/
43+
var useCache: Boolean = true
44+
45+
/**
46+
* Sets the request method to GET
47+
* @return this
48+
*/
49+
fun get() = this.apply { method = "GET" }
50+
51+
/**
52+
* Sets the request method to POST
53+
* @return this
54+
*/
55+
fun post() = this.apply { method = "POST" }
56+
57+
/**
58+
* Sets the request method to PUT
59+
* @return this
60+
*/
61+
fun put() = this.apply { method = "PUT" }
62+
63+
/**
64+
* Sets the request method to DELETE
65+
* @return this
66+
*/
67+
fun delete() = this.apply { method = "DELETE" }
68+
69+
/**
70+
* Sets the body of the request
71+
* @param body The body of the request
72+
* @return this
73+
*/
74+
fun body(body: String) = this.apply { this.body = body }
75+
76+
/**
77+
* Sets the timeout of the request
78+
* @param timeout The timeout of the request. (Set to -1 to disable)
79+
* @return this
80+
*/
81+
fun timeout(timeout: Int) = this.apply { this.timeout = timeout }
82+
83+
/**
84+
* Sets the header of the request. If the header already exists, it will be overwritten
85+
* @param key The key of the header
86+
* @param value The value of the header
87+
* @return this
88+
*/
89+
fun header(key: String, value: String) = this.apply { headers[key] = value }
90+
91+
/**
92+
* Sets if the request should use the cache
93+
* @param useCache True if the request should use the cache, false otherwise
94+
* @return this
95+
*/
96+
fun useCache(useCache: Boolean) = this.apply { this.useCache = useCache }
97+
98+
/**
99+
* Sends the request and returns a response
100+
* @return the [Response]
101+
*/
102+
fun connect(): Response {
103+
val url = URL(
104+
if(this.method == "GET" && this.body.isNotBlank()) {
105+
"${this.url}?${this.body}"
106+
} else {
107+
this.url
108+
}
109+
)
110+
val connection = url.openConnection() as HttpURLConnection
111+
connection.requestMethod = this.method
112+
connection.doOutput = this.method == "POST" || this.method == "PUT" || (this.body.isNotBlank() && this.method != "GET")
113+
connection.doInput = true
114+
connection.useCaches = this.useCache
115+
if(this.timeout != -1) connection.connectTimeout = this.timeout
116+
if(this.timeout != -1) connection.readTimeout = this.timeout
117+
this.headers.forEach { connection.setRequestProperty(it.key, it.value) }
118+
if(this.body.isNotEmpty() && this.method != "GET") connection.outputStream.write(this.body.toByteArray())
119+
return Response(connection)
120+
}
121+
}

src/main/kotlin/xyz/theprogramsrc/networkingmodule/objects/Request.kt

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/main/kotlin/xyz/theprogramsrc/networkingmodule/objects/Response.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package xyz.theprogramsrc.networkingmodule.objects
33
import java.io.InputStream
44
import java.net.HttpURLConnection
55

6-
class Response(val request: Request, val connection: HttpURLConnection) {
6+
class Response(val connection: HttpURLConnection) {
77

8+
/**
9+
* The expected response code
10+
*/
811
var expectedStatusCode: Int = 200
912

1013
/**
@@ -14,10 +17,10 @@ class Response(val request: Request, val connection: HttpURLConnection) {
1417
fun wasSuccessful(): Boolean = connection.responseCode == expectedStatusCode
1518

1619
/**
17-
* Gets the response code
18-
* @return the response code
20+
* Gets the status code
21+
* @return the status code
1922
*/
20-
fun getResponseCode(): Int = connection.responseCode
23+
fun getStatusCode(): Int = connection.responseCode
2124

2225
/**
2326
* Gets the response message

0 commit comments

Comments
 (0)