Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions docs/module/requests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Requests Module <!-- {docsify-ignore} -->

The `requests` module provides an interface for making asynchronous HTTP requests in AwesomeWM using the `lgi` bindings for `Soup`, `Gio`, and `GLib`.
It supports common HTTP methods like `GET`, `POST`, `PUT`, and more.

## Methods

### `requests.request(method, args, callback)`

#### Parameters:

- **method**: A string representing the HTTP method (`"GET"`, `"POST"`, etc.).
- **args**: A table or string. The table can include:
- **url**: The request URL (string).
- **params**: Query parameters (table).
- **headers**: HTTP headers (table).
- **body**: The request body (`GLib.Bytes` or string).
- **callback**: A function to handle the `Response` object.

#### Example:

```lua
requests.request("GET", { url = "https://api.example.com" }, function(response)
print(response.status_code, response.text)
end)
```

---

### `requests.get(args, callback)`

#### Description:

A shorthand for making `GET` requests.

#### Parameters:

- **args**: Similar to `requests.request` but defaults to `GET` method.
- **callback**: Function called with a `Response` object.

#### Example:

```lua
requests.get({ url = "https://api.example.com" }, function(response)
print(response.status_code, response.text)
end)
```

---

### `requests.post(args, callback)`

#### Description:

A shorthand for making `POST` requests.

#### Parameters:

- **args**: Similar to `requests.request` but defaults to `POST` method.
- **callback**: Function called with a `Response` object.

#### Example:

```lua
requests.post({
url = "https://api.example.com",
headers = { Authorization = "Bearer token" },
body = '{"key": "value"}'
}, function(response)
print(response.status_code, response.text)
end)
```

---

## Response Object

The `Response` object encapsulates the result of a request.

### Fields:

- **url**: The final URL after redirections.
- **status_code**: The HTTP status code (number).
- **ok**: A boolean indicating if the request was successful.
- **reason_phrase**: A string with the status reason.
- **text**: The response body as a string.
- **bytes**: The response body as `GLib.Bytes`.

### Example Usage:

```lua
print(response.url) -- "https://api.example.com"
print(response.status_code) -- 200
print(response.ok) -- true
print(response.text) -- Response body as string
```
126 changes: 112 additions & 14 deletions helpers/filesystem.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
local Gio = require("lgi").Gio
local lgi = require("lgi")
local Gio = lgi.require("Gio", "2.0")
local GLib = lgi.require("GLib", "2.0")
local awful = require("awful")
local string = string
local gears = require("gears")

local _filesystem = {}

Expand All @@ -22,14 +24,14 @@ function _filesystem.list_directory_files(path, exts, recursive)
end

-- Build a table of files from the path with the required extensions
local file_list = Gio.File.new_for_path(path):enumerate_children(
"standard::*",
0
)
local file_list =
Gio.File.new_for_path(path):enumerate_children("standard::*", 0)
if file_list then
for file in function()
return file_list:next_file()
end do
for file in
function()
return file_list:next_file()
end
do
local file_type = file:get_file_type()
if file_type == "REGULAR" then
local file_name = file:get_display_name()
Expand All @@ -43,7 +45,7 @@ function _filesystem.list_directory_files(path, exts, recursive)
local file_name = file:get_display_name()
files = gears.table.join(
files,
list_directory_files(file_name, exts, recursive)
_filesystem.list_directory_files(file_name, exts, recursive)
)
end
end
Expand All @@ -53,10 +55,106 @@ function _filesystem.list_directory_files(path, exts, recursive)
end

function _filesystem.save_image_async_curl(url, filepath, callback)
awful.spawn.with_line_callback(string.format("curl -L -s %s -o %s", url, filepath),
{
exit=callback
})
awful.spawn.with_line_callback(
string.format("curl -L -s %s -o %s", url, filepath),
{
exit = callback,
}
)
end

---@param filepath string | Gio.File
---@param callback fun(content: string)
---@return nil
function _filesystem.read_file_async(filepath, callback)
if type(filepath) == "string" then
return _filesystem.read_file_async(
Gio.File.new_for_path(filepath),
callback
)
elseif type(filepath) == "userdata" then
filepath:load_contents_async(nil, function(_, task)
local _, content, _ = filepath:load_contents_finish(task)
return callback(content)
end)
end
end

---@param filepath string | Gio.File
---@return string?
function _filesystem.read_file_sync(filepath)
if type(filepath) == "string" then
return _filesystem.read_file_sync(Gio.File.new_for_path(filepath))
elseif type(filepath) == "userdata" then
local _, content, _ = filepath:load_contents()
return content
end
end

---@param str string
local function tobytes(str)
local bytes = {}

for i = 1, #str do
table.insert(bytes, string.byte(str, i))
end

return bytes
end

---@param filepath string | Gio.File
---@param content string | string[] | GLib.Bytes
---@param callback? fun(file: Gio.File | userdata | nil): nil
function _filesystem.write_file_async(filepath, content, callback)
if type(filepath) == "string" then
return _filesystem.write_file_async(
Gio.File.new_for_path(filepath),
content,
callback
)
elseif type(content) == "string" then
return _filesystem.write_file_async(
filepath,
GLib.Bytes.new(tobytes(content)),
callback
)
elseif type(content) == "table" then
return _filesystem.write_file(
filepath,
table.concat(content, "\n"),
callback
)
elseif type(filepath) == "userdata" and type(content) == "userdata" then
callback = callback or function() end

return filepath:replace_contents_bytes_async(
content,
nil,
false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
nil,
function(_, task)
filepath:replace_contents_finish(task)
return callback(filepath)
end
)
end
end

function _filesystem.file_exists(filepath)
if filepath then
return GLib.file_test(filepath, GLib.FileTest.EXISTS)
else
return false
end
end

function _filesystem.dir_exists(filepath)
if filepath then
return GLib.file_test(filepath, GLib.FileTest.IS_DIR)
else
return false
end
end

return _filesystem
1 change: 1 addition & 0 deletions helpers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ return {
filesystem = require(... .. ".filesystem"),
shape = require(... .. ".shape"),
time = require(... .. ".time"),
requests = require(... .. ".requests"),
}
Loading