Skip to content

Commit 510cbbe

Browse files
author
Francisco Solis
authored
Merge pull request #1 from TheProgramSrc/feat/requests
Updates & Fixes * Removed Old Classes * Created new Request Class * Updates to Response * Added Tests for different classes
2 parents 2233a8f + 799d38e commit 510cbbe

File tree

5 files changed

+298
-7
lines changed

5 files changed

+298
-7
lines changed

build.gradle

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import org.apache.tools.ant.filters.ReplaceTokens
22

33
plugins {
4-
id 'org.jetbrains.kotlin.jvm' version '1.6.0'
4+
id 'org.jetbrains.kotlin.jvm' version '1.6.10'
55
id 'maven-publish'
6-
id 'com.github.johnrengelman.shadow' version '7.1.0'
6+
id 'com.github.johnrengelman.shadow' version '7.1.2'
77
id 'org.jetbrains.dokka' version '1.6.0'
88
}
99

@@ -23,16 +23,15 @@ repositories {
2323
maven { url 'https://oss.sonatype.org/content/repositories/releases/' }
2424
maven { url 'https://oss.sonatype.org/content/groups/public/' }
2525
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
26-
maven { url 'https://repo.codemc.org/repository/maven-public' }
27-
maven { url 'https://repo.codemc.org/repository/nms' }
28-
maven { url 'https://jitpack.io' }
26+
maven { url 'https://repo.codemc.org/repository/maven-public/' }
27+
maven { url 'https://jitpack.io/' }
2928
}
3029

3130
dependencies {
32-
compileOnly 'org.jetbrains.kotlin:kotlin-stdlib:1.6.0'
31+
compileOnly 'org.jetbrains.kotlin:kotlin-stdlib:1.6.10'
3332
compileOnly 'org.spigotmc:spigot-api:1.18.1-R0.1-SNAPSHOT'
3433
compileOnly 'net.md-5:bungeecord-api:1.18-R0.1-SNAPSHOT'
35-
compileOnly 'xyz.theprogramsrc:simplecoreapi:0.1.9-SNAPSHOT'
34+
compileOnly 'xyz.theprogramsrc:simplecoreapi:0.1.12-SNAPSHOT'
3635

3736
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
3837
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package xyz.theprogramsrc.networkingmodule
2+
3+
class Main
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+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package xyz.theprogramsrc.networkingmodule.objects
2+
3+
import java.io.InputStream
4+
import java.net.HttpURLConnection
5+
6+
class Response(val connection: HttpURLConnection) {
7+
8+
/**
9+
* The expected response code
10+
*/
11+
var expectedStatusCode: Int = 200
12+
13+
/**
14+
* Checks if the status code is the same as the expected status code
15+
* @return true if the status code is the same as the expected status code
16+
*/
17+
fun wasSuccessful(): Boolean = connection.responseCode == expectedStatusCode
18+
19+
/**
20+
* Gets the status code
21+
* @return the status code
22+
*/
23+
fun getStatusCode(): Int = connection.responseCode
24+
25+
/**
26+
* Gets the response message
27+
* @return the response message
28+
*/
29+
fun getResponseMessage(): String = connection.responseMessage
30+
31+
/**
32+
* Gets the response stream
33+
* @return the response stream
34+
*/
35+
fun getResponseStream(): InputStream = connection.inputStream
36+
37+
/**
38+
* Gets the response string
39+
* @return the response string
40+
*/
41+
fun getResponseString(): String = connection.inputStream.bufferedReader().use { it.readText() }
42+
43+
/**
44+
* Gets the response headers
45+
* @return the response headers
46+
*/
47+
fun getResponseHeaders(): Map<String, List<String>> = connection.headerFields
48+
49+
/**
50+
* Gets the response header
51+
* @return the response header
52+
*/
53+
fun getResponseHeader(header: String): String? = connection.headerFields[header]?.get(0)
54+
55+
/**
56+
* Gets the response header
57+
* @return the response header
58+
*/
59+
fun getResponseHeader(header: String, index: Int): String? = connection.headerFields[header]?.get(index)
60+
61+
/**
62+
* Gets the response header count
63+
* @return the response header count
64+
*/
65+
fun getResponseHeaderCount(header: String): Int = connection.headerFields[header]?.size ?: 0
66+
67+
/**
68+
* Gets the response header names
69+
* @return the response header names
70+
*/
71+
fun getResponseHeaderNames(): List<String> = connection.headerFields.keys.toList()
72+
73+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package xyz.theprogramsrc.networkingmodule.builder
2+
3+
import org.junit.jupiter.api.Assertions.*
4+
import org.junit.jupiter.api.DisplayName
5+
import org.junit.jupiter.api.Nested
6+
import org.junit.jupiter.api.Test
7+
import xyz.theprogramsrc.simplecoreapi.libs.google.gson.JsonObject
8+
import xyz.theprogramsrc.simplecoreapi.libs.google.gson.JsonParser
9+
10+
@DisplayName("Http Requests Test")
11+
internal class RequestTest {
12+
13+
@Nested
14+
@DisplayName("GET Request")
15+
inner class GetRequest {
16+
17+
@Test fun getRequest() = assertTrue(
18+
Request("https://httpbin.org/get")
19+
.get()
20+
.connect()
21+
.wasSuccessful()
22+
)
23+
24+
}
25+
26+
@Nested
27+
@DisplayName("POST Request")
28+
inner class PostRequest {
29+
30+
@Test fun postRequest() = assertTrue(
31+
Request("https://httpbin.org/post")
32+
.post()
33+
.connect()
34+
.wasSuccessful()
35+
)
36+
37+
}
38+
39+
@Nested
40+
@DisplayName("PUT Request")
41+
inner class PutRequest {
42+
43+
@Test fun putRequest() = assertTrue(
44+
Request("https://httpbin.org/put")
45+
.put()
46+
.connect()
47+
.wasSuccessful()
48+
)
49+
50+
}
51+
52+
@Nested
53+
@DisplayName("DELETE Request")
54+
inner class DeleteRequest {
55+
56+
@Test fun deleteRequest() = assertTrue(
57+
Request("https://httpbin.org/delete")
58+
.delete()
59+
.connect()
60+
.wasSuccessful()
61+
)
62+
63+
}
64+
65+
@Nested
66+
@DisplayName("Headers and Body Request")
67+
inner class HeadersAndBodyRequest {
68+
69+
@Test fun headersRequest() {
70+
val response = Request("https://httpbin.org/get")
71+
.get()
72+
.header("Accept", "application/json")
73+
.header("My-Test-Header", "My-Test-Value")
74+
.connect()
75+
val json = JsonParser.parseString(response.getResponseString()).asJsonObject
76+
val headers = json.getAsJsonObject("headers")
77+
assertEquals("application/json", headers.get("Accept").asString)
78+
assertEquals("My-Test-Value", headers.get("My-Test-Header").asString)
79+
}
80+
81+
@Test fun bodyRequest() {
82+
val response = Request("https://httpbin.org/get")
83+
.get()
84+
.body("argument=value&argument2=value2")
85+
.connect()
86+
val json = JsonParser.parseString(response.getResponseString()).asJsonObject
87+
val args = json.getAsJsonObject("args")
88+
assertEquals("value", args.get("argument").asString)
89+
assertEquals("value2", args.get("argument2").asString)
90+
}
91+
92+
}
93+
94+
95+
}

0 commit comments

Comments
 (0)