Skip to content

Instantly share code, notes, and snippets.

@FranKusmiruk
Last active May 1, 2023 04:48
Show Gist options
  • Save FranKusmiruk/4f1a9bf1952c4af2aea77be51d652b22 to your computer and use it in GitHub Desktop.
Save FranKusmiruk/4f1a9bf1952c4af2aea77be51d652b22 to your computer and use it in GitHub Desktop.
Reqn made with skript-reflect
options:
version: 1.0.3
import:
java.io.OutputStreamWriter
java.nio.charset.StandardCharsets
java.util.zip.GZIPInputStream
java.util.zip.InflaterInputStream
java.util.zip.Inflater
java.lang.StringBuilder
java.io.BufferedReader
java.io.InputStreamReader
java.lang.System
java.net.URL
java.net.URLEncoder
on script load:
set {web-requests::methods::*} to "GET", "POST", "PUT", "DELETE", "PATCH"
create section with {_target}, {_method}, {_body}, {_headers::*} and store it in {web-requests::send-handler}:
set {_connection} to {_target}.openConnection()
{_connection}.setRequestProperty("User-Agent", "Reqn.sk/{@version} (https://gist.github.com/FranKusmiruk/)")
loop {_headers::*}:
set {_header::*} to loop-value split by ": "
{_connection}.setRequestProperty({_header::1}, {_header::2})
{_connection}.setUseCaches(false)
{_connection}.setRequestMethod({_method})
if {_method} isn't "GET":
if {_body} is empty:
stop
{_connection}.setRequestProperty("Content-Length", "%size of ...{_body}.getBytes()%")
{_connection}.setDoOutput(true)
set {_output-stream} to try {_connection}.getOutputStream()
if {_output-stream} is set:
set {_out} to new OutputStreamWriter({_output-stream}, StandardCharsets.UTF_8)
{_out}.write({_body})
{_out}.close()
set {web-requests::response::status-line} to {_connection}.getHeaderField(0)
set {web-requests::response::headers} to {_connection}.getHeaderFields()
set {_response} to {_connection}.getErrorStream() otherwise try {_connection}.getInputStream()
if {_response} is set:
set {_encoding} to {_connection}.getContentEncoding()
if {_encoding} is "gzip":
set {_response} to new GZIPInputStream({_response})
else if {_encoding} is "deflate":
set {_response} to new InflaterInputStream({_response}, new Inflater(true))
set {_response-body} to new StringBuilder()
set {_buffered-reader} to new BufferedReader(new InputStreamReader({_response}, StandardCharsets.UTF_8))
set {_line} to {_buffered-reader}.readLine()
set {_final} to 1000
while {_line} isn't {_final}:
{_response-body}.append({_line}).append(System.lineSeparator())
set {_line} to {_buffered-reader}.readLine() otherwise {_final}
{_buffered-reader}.close()
set {web-requests::response::status-code} to {_connection}.getResponseCode()
set {web-requests::response::message} to {_connection}.getResponseMessage()
set {web-requests::response::body} to try {_response-body}.toString()
if {_connection} is set:
{_connection}.disconnect()
effect:
patterns:
send [a[n]] [http] [%-string%] [web] request to [[the] url] %string% [with [the] header[s] %-strings% [and [the] body %-strings%]]
send [a[n]] [http] [%-string%] [web] request to [[the] url] %string% [with [the] body %-strings% [and [the] header[s] %-strings%]]
trigger:
set {_method} to expr-1 in upper case otherwise "GET"
if {web-requests::methods::*} doesn't contain {_method}:
stop
set {_body::*} to exprs-4 if matched pattern is 1, else exprs-3
if {_body::*} is set:
set {_body} to join {_body::*} by System.lineSeparator()
else:
set {_body} to ""
set {_headers::*} to exprs-3 if matched pattern is 1, else exprs-4
set {_url} to expr-2
replace all "§" with "&" in {_url}
set {_url} to new URL({_url})
delay the effect
run section {web-requests::send-handler} async with {_url}, {_method}, {_body}, {_headers::*} and wait
continue
expression response body:
get:
return {web-requests::response::body}
expression [response] status code:
get:
return {web-requests::response::status-code}
expression response header[s]:
get:
loop ...{web-requests::response::headers}.entrySet():
set {_values} to join ...loop-value.getValue() by ", "
add "%loop-value.getKey()%: %{_values}%" to {_headers::*}
return {_headers::*}
expression response header key[s]:
get:
return ...{web-requests::response::headers}.keySet()
expression response header values:
get:
loop ...{web-requests::response::headers}.values():
add ...loop-value to {_values::*}
return {_values::*}
expression %string% [response] header [value[s]]:
get:
return ...{web-requests::response::headers}.get(expr-1)
expression (http|ur(i|l)) (safe|encoded|escaped) %strings%:
get:
loop exprs-1:
add URLEncoder.encode(loop-value, StandardCharsets.UTF_8) to {_encoded::*}
return {_encoded::*}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment