Skip to content

Commit 3848cbb

Browse files
author
Francisco Solis
committed
Added Requests and Responses
(Working on Request to google.com returning error 405)
1 parent 2233a8f commit 3848cbb

File tree

6 files changed

+250
-7
lines changed

6 files changed

+250
-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: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package xyz.theprogramsrc.networkingmodule.builder
2+
3+
import xyz.theprogramsrc.networkingmodule.objects.Request
4+
import xyz.theprogramsrc.networkingmodule.objects.Response
5+
import java.net.HttpURLConnection
6+
import java.net.URL
7+
8+
object ConnectionBuilder {
9+
10+
/**
11+
* Connects to the given [URL]
12+
* @param url The [URL] to connect to
13+
* @return The [Response] of the connection
14+
*/
15+
fun connect(url: String): Response = connect(Request(url))
16+
17+
/**
18+
* Connects to the given [URL] using the given request method
19+
* @param url The [Request] to connect to
20+
* @param method The request method to use
21+
* @return The [Response] of the connection
22+
*/
23+
fun connect(url: String, method: String): Response = connect(Request(url, method))
24+
25+
/**
26+
* Connects to the given [URL] using the given request method and headers
27+
* @param url The [Request] to connect to
28+
* @param method The request method to use
29+
* @param properties The properties to use in the query
30+
* @return The [Response] of the connection
31+
*/
32+
fun connect(url: String, method: String, properties: Map<String, String>): Response = connect(Request(url, method, properties=properties))
33+
34+
/**
35+
* Connects to the given [URL] using the given request method and headers
36+
* @param url The [Request] to connect to
37+
* @param method The request method to use
38+
* @param properties The properties to use in the query
39+
* @param headers The headers to use in the query
40+
* @return The [Response] of the connection
41+
*/
42+
fun connect(url: String, method: String, properties: Map<String, String>, headers: Map<String, String>): Response = connect(Request(url, method, headers, properties))
43+
44+
/**
45+
* Connects to the given [URL] using the given request method and headers
46+
* @param request The [Request] to connect to
47+
* @return The [Response] of the connection
48+
*/
49+
fun connect(request: Request): Response {
50+
// Generate the url and connection
51+
val url = URL(request.url)
52+
val connection = url.openConnection() as HttpURLConnection
53+
54+
// Apply the method
55+
if(connection.requestMethod != request.method){
56+
connection.requestMethod = request.method
57+
}
58+
59+
// Apply User Agent
60+
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36")
61+
62+
// Apply the headers
63+
val headers = request.headers
64+
for (header in headers) {
65+
connection.setRequestProperty(header.key, header.value)
66+
}
67+
68+
// Apply the body or properties if any
69+
val body = if(request.properties.isNotEmpty()) {
70+
val props = request.properties
71+
props.entries.joinToString("&") { "${it.key}=${it.value}" }
72+
} else {
73+
request.body
74+
} ?: ""
75+
connection.doInput = true
76+
connection.doOutput = true
77+
connection.outputStream.write(body.toByteArray())
78+
79+
// Apply timeouts
80+
connection.connectTimeout = request.connectTimeout
81+
connection.readTimeout = request.readTimeout
82+
83+
// Connect
84+
connection.connect()
85+
86+
return Response(request, connection)
87+
}
88+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package xyz.theprogramsrc.networkingmodule.objects
2+
3+
/**
4+
* Represents a request
5+
* @param url The url of the request
6+
* @param method The method of the request
7+
* @param headers The headers of the request
8+
* @param properties The properties of the request
9+
*/
10+
data class Request(val url: String, val method: String = "GET", val headers: Map<String, String> = mapOf(), val properties: Map<String, String> = mapOf()){
11+
12+
/**
13+
* Connection timeout
14+
* @return Timeout in milliseconds
15+
*/
16+
var connectTimeout: Int = 12500
17+
18+
/**
19+
* Read timeout
20+
* @return Timeout in milliseconds
21+
*/
22+
var readTimeout: Int = 12500
23+
24+
/**
25+
* The body of the request
26+
* @return the body
27+
*/
28+
var body: String? = null
29+
30+
/**
31+
* Adds the given header to the request
32+
* @param key The name of the header
33+
* @param value The value of the header
34+
* @return the request with the new header
35+
*/
36+
fun addHeader(key: String, value: String): Request {
37+
return Request(url, method, headers.plus(Pair(key, value)))
38+
}
39+
40+
/**
41+
* Adds the given headers to the request
42+
* @param headers the headers to add
43+
* @return the request with the new headers
44+
*/
45+
fun addHeaders(headers: Map<String, String>): Request = Request(url, method, this.headers.plus(headers))
46+
47+
/**
48+
* Sets the body of the request
49+
* @param body the body of the request
50+
* @return the request with the new body
51+
*/
52+
fun setBody(body: String): Request = run {
53+
this.body = body
54+
this
55+
}
56+
57+
/**
58+
* Adds the given property to the request
59+
* @param key the name of the property
60+
* @param value the value of the property
61+
* @return the request with the new property
62+
*/
63+
fun addProperty(key: String, value: String): Request = Request(url, method, headers, properties.plus(Pair(key, value)))
64+
65+
/**
66+
* Adds the given properties to the request
67+
* @param properties the properties to add
68+
* @return the request with the new properties
69+
*/
70+
fun addProperties(properties: Map<String, String>): Request = Request(url, method, headers, this.properties.plus(properties))
71+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package xyz.theprogramsrc.networkingmodule.objects
2+
3+
import java.io.InputStream
4+
import java.net.HttpURLConnection
5+
6+
class Response(val request: Request, val connection: HttpURLConnection) {
7+
8+
var expectedStatusCode: Int = 200
9+
10+
/**
11+
* Checks if the status code is the same as the expected status code
12+
* @return true if the status code is the same as the expected status code
13+
*/
14+
fun wasSuccessful(): Boolean = connection.responseCode == expectedStatusCode
15+
16+
/**
17+
* Gets the response code
18+
* @return the response code
19+
*/
20+
fun getResponseCode(): Int = connection.responseCode
21+
22+
/**
23+
* Gets the response message
24+
* @return the response message
25+
*/
26+
fun getResponseMessage(): String = connection.responseMessage
27+
28+
/**
29+
* Gets the response stream
30+
* @return the response stream
31+
*/
32+
fun getResponseStream(): InputStream = connection.inputStream
33+
34+
/**
35+
* Gets the response string
36+
* @return the response string
37+
*/
38+
fun getResponseString(): String = connection.inputStream.bufferedReader().use { it.readText() }
39+
40+
/**
41+
* Gets the response headers
42+
* @return the response headers
43+
*/
44+
fun getResponseHeaders(): Map<String, List<String>> = connection.headerFields
45+
46+
/**
47+
* Gets the response header
48+
* @return the response header
49+
*/
50+
fun getResponseHeader(header: String): String? = connection.headerFields[header]?.get(0)
51+
52+
/**
53+
* Gets the response header
54+
* @return the response header
55+
*/
56+
fun getResponseHeader(header: String, index: Int): String? = connection.headerFields[header]?.get(index)
57+
58+
/**
59+
* Gets the response header count
60+
* @return the response header count
61+
*/
62+
fun getResponseHeaderCount(header: String): Int = connection.headerFields[header]?.size ?: 0
63+
64+
/**
65+
* Gets the response header names
66+
* @return the response header names
67+
*/
68+
fun getResponseHeaderNames(): List<String> = connection.headerFields.keys.toList()
69+
70+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package xyz.theprogramsrc.networkingmodule.objects.connection
2+
3+
internal class ConnectToStringTest {
4+
5+
// @Test
6+
// fun testConnectToString(){
7+
// TODO: For Some reason this request returns error 405, even if is just a GET request to google, maybe because there is a body and query? Maybe because the doInput and/or doOutput is true
8+
// val response = ConnectionBuilder.connect("https://google.com/")
9+
// println(response.request.url + " - " + response.request.method + " - " + response.request.headers + " - " + response.request.body + " - " + response.request.properties)
10+
// assertEquals(200, response.getResponseCode())
11+
// }
12+
}

0 commit comments

Comments
 (0)