From 90422c3a45eb4138ed8223c97d6d40342d8bfb65 Mon Sep 17 00:00:00 2001 From: Patrick Dill Date: Sun, 17 May 2020 20:54:15 -0400 Subject: [PATCH] attribute names Response.status -> Reponse.status_code Response.success -> Response.ok Response.content -> Response.text --- README.md | 53 ++++++++++++++++++++----------------------- default.project.json | 14 ++++++++++++ docs/api-reference.md | 6 ++--- docs/index.md | 46 +++++++++++++++++++++---------------- http/src/response.lua | 21 +++++++++++++---- rojo.json | 10 -------- 6 files changed, 84 insertions(+), 66 deletions(-) create mode 100644 default.project.json delete mode 100644 rojo.json diff --git a/README.md b/README.md index 3ac752b..b96f25f 100644 --- a/README.md +++ b/README.md @@ -1,47 +1,44 @@ - -# Roblox Requests 0.2.0 +# Roblox Requests **Roblox Requests** brings user-friendly HTTP to Roblox with no need for the manual labor of HttpService. +--- + With Requests you can send robust, human-readable HTTP requests without ever having to deal with the underlying HttpService. No more manual query strings or encoding POST data. -## Roblox Requests Features - -- Sessions with cookie persistence, base URLs/headers -- Automatic query string building -- Automatic JSON body encoding -- Builtin Response object and JSON decoding -- Domain based Key/Value cookies -- Multipart form building including file encoding and upload -- Global ratelimiting with per-session config options - ---- - -An example of what you can do with Requests: +The Power of Roblox Requests: ```lua -local http = require(game.ReplicatedStorage.http) +local r = http.get("https://api.github.com/orgs/Roblox/repos") -local r = http.post("https://httpbin.org/post", { - query = {arg="value"}, - data = {this="json"} -}) +print(r.status_code, r.message) +-- 200 OK -print(r.content_type, - r:json().url) +repos = r:json() +print(#repos) +-- 30 --- output: +print(r.content_type) -- application/json --- https://httpbin.org/post?arg=value +print(r.encoding) +-- utf-8 +print(r.headers["x-ratelimit-remaining"]) +-- 59 ``` -See [similar code with HttpService.](https://gist.github.com/jpatrickdill/8fe2a82c47c1bdf679eb1a1c5f07d7a0) +Roblox Requests will bring simple support for all internet resources to your game. -Requests' powerful API allows you to send HTTP/1.1 requests without the need of manual labor. You'll never -have to add query strings to URLs or encode your POST data again. +## Roblox Requests Features +- Sessions with Cookie Persistence +- Default Headers, URL prefixes +- Automatic Query Strings +- JSON Body Encoding, JSON Response Decoding +- Elegant Key/Value Cookies + - Domain/Path filters +- Multipart File Encoding and Upload +- Global/Per-Session Ratelimiting -## [Documentation](https://jpatrickdill.github.io/roblox-requests/guide/installation/) [In this documentation](https://jpatrickdill.github.io/roblox-requests/guide/installation/) you'll find step-by-step instructions to get the most out of Roblox Requests. diff --git a/default.project.json b/default.project.json new file mode 100644 index 0000000..2981b93 --- /dev/null +++ b/default.project.json @@ -0,0 +1,14 @@ +{ + "name": "roblox-requests", + "tree": { + "$className": "DataModel", + + "ReplicatedStorage": { + "$className": "ReplicatedStorage", + + "http": { + "$path": "http" + } + } + } +} \ No newline at end of file diff --git a/docs/api-reference.md b/docs/api-reference.md index 59af28c..83d16e0 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -84,11 +84,11 @@ will not block the current thread. - **request** (Request) - Request that generated this response - **url** (string) - Requests' URL - **method** (string) - Request's HTTP method -- **success** (bool) - `true` if response was successful -- **code** (number) - Status code of reponse +- **ok** (bool) - `true` if response has OK status code (200 \<= code \< 300) +- **status_code** (number) - Status code of reponse - **message** (string) - Status message of response - **headers** (dictionary) - Headers sent in response -- **content** (string) - Response body +- **text** (string) - Response body - **cookies** (CookieJar) - New cookies sent in this response ### Response:json diff --git a/docs/index.md b/docs/index.md index e5e6216..dd8b658 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,39 +1,45 @@ -# Roblox Requests {{ version }} +# Roblox Requests **Roblox Requests** brings user-friendly HTTP to Roblox with no need for the manual labor of HttpService. --- -An example of what you can do with Requests: +With Requests you can send robust, human-readable HTTP requests without ever having to deal with the underlying HttpService. +No more manual query strings or encoding POST data. + +The Power of Roblox Requests: ```lua -local http = require(game.ReplicatedStorage.http) +local r = http.get("https://api.github.com/orgs/Roblox/repos") -local r = http.post("https://httpbin.org/post", { query = {arg="value"}, - data = {this="json"} }) +print(r.status_code, r.message) +-- 200 OK -print(r.content_type, - r:json().url) +repos = r:json() +print(#repos) +-- 30 --- output: +print(r.content_type) -- application/json --- https://httpbin.org/post?arg=value +print(r.encoding) +-- utf-8 +print(r.headers["x-ratelimit-remaining"]) +-- 59 ``` -See [similar code with HttpService.](https://gist.github.com/jpatrickdill/8fe2a82c47c1bdf679eb1a1c5f07d7a0) +Roblox Requests will bring simple support for all internet resources to your game. -Requests' powerful API allows you to send HTTP/1.1 requests without the need of manual labor. You'll never -have to add query strings to URLs or encode your POST data again. +## Roblox Requests Features -### Roblox Requests Features +- Sessions with Cookie Persistence +- Default Headers, URL prefixes +- Automatic Query Strings +- JSON Body Encoding, JSON Response Decoding +- Elegant Key/Value Cookies + - Domain/Path filters +- Multipart File Encoding and Upload +- Global/Per-Session Ratelimiting -- Sessions with cookie persistence, base URLs/headers -- Automatic query string building -- Automatic JSON body encoding -- Builtin Response object and JSON decoding -- Domain based Key/Value cookies -- Multipart form building including file encoding and upload -- Global ratelimiting with per-session config options Roblox Requests was inspired by the well known [Python Requests](https://2.python-requests.org/en/master/) library. diff --git a/http/src/response.lua b/http/src/response.lua index 2e79a82..442e4ef 100644 --- a/http/src/response.lua +++ b/http/src/response.lua @@ -26,14 +26,25 @@ function Response.new(req, resp, rt) self.method = req.method -- response data - self.success = resp.Success - self.code = resp.StatusCode + self.code = resp.StatusCode -- deprecated + self.status_code = resp.StatusCode + + self.success = resp.Success -- deprecated + self.ok = self.status_code >= 200 and self.status_code < 300 + self.message = resp.StatusMessage self.headers = CaseInsensitive(resp.Headers) - self.content = resp.Body + + self.content = resp.Body -- deprecated + self.text = resp.Body + -- additional metadata for quick access - self.content_type = self.headers["content-type"] + local type_encoding = self.headers["content-type"]:split(";") + self.content_type = type_encoding[1] + self.encoding = (type_encoding[2] and type_encoding[2]:split("=")[2]) or "" -- or "utf-8" + + self.content_length = self.headers["content-length"] or #self.content -- cookies @@ -47,7 +58,7 @@ function Response.new(req, resp, rt) end function Response:__tostring() - return self.content + return self.text end function Response:json() diff --git a/rojo.json b/rojo.json deleted file mode 100644 index dd71fc3..0000000 --- a/rojo.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "name": "roblox-requests", - "servePort": 8000, - "partitions": { - "http": { - "path": "http", - "target": "ReplicatedStorage.http" - } - } -}