prototype
This commit is contained in:
parent
fbd7453551
commit
3175040e3a
12 changed files with 189 additions and 4 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,4 +1,5 @@
|
|||
*.beam
|
||||
*.ez
|
||||
/build
|
||||
/examples/*/build
|
||||
erl_crash.dump
|
||||
|
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"cSpell.words": ["apkallone", "httpc"]
|
||||
}
|
24
examples/info/README.md
Normal file
24
examples/info/README.md
Normal file
|
@ -0,0 +1,24 @@
|
|||
# info
|
||||
|
||||
[![Package Version](https://img.shields.io/hexpm/v/info)](https://hex.pm/packages/info)
|
||||
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/info/)
|
||||
|
||||
```sh
|
||||
gleam add info@1
|
||||
```
|
||||
```gleam
|
||||
import info
|
||||
|
||||
pub fn main() {
|
||||
// TODO: An example of the project in use
|
||||
}
|
||||
```
|
||||
|
||||
Further documentation can be found at <https://hexdocs.pm/info>.
|
||||
|
||||
## Development
|
||||
|
||||
```sh
|
||||
gleam run # Run the project
|
||||
gleam test # Run the tests
|
||||
```
|
8
examples/info/gleam.toml
Normal file
8
examples/info/gleam.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
name = "info"
|
||||
version = "1.0.0"
|
||||
|
||||
[dependencies]
|
||||
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
|
||||
apkallone = { path = "../.." }
|
||||
|
||||
[dev-dependencies]
|
15
examples/info/manifest.toml
Normal file
15
examples/info/manifest.toml
Normal file
|
@ -0,0 +1,15 @@
|
|||
# This file was generated by Gleam
|
||||
# You typically do not need to edit this file
|
||||
|
||||
packages = [
|
||||
{ name = "apkallone", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_httpc", "gleam_json", "gleam_stdlib"], source = "local", path = "../.." },
|
||||
{ name = "gleam_erlang", version = "0.27.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "DE468F676D71B313C6C8C5334425CFCF827837333F8AB47B64D8A6D7AA40185D" },
|
||||
{ name = "gleam_http", version = "3.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "EA66440C2269F7CED0F6845E5BD0DB68095775D627FA709A841CA78A398D6D56" },
|
||||
{ name = "gleam_httpc", version = "3.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "091CDD2BEC8092E82707BEA03FB5205A2BBBDE4A2F551E3C069E13B8BC0C428E" },
|
||||
{ name = "gleam_json", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "CB10B0E7BF44282FB25162F1A24C1A025F6B93E777CCF238C4017E4EEF2CDE97" },
|
||||
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
|
||||
]
|
||||
|
||||
[requirements]
|
||||
apkallone = { path = "../.." }
|
||||
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
|
13
examples/info/src/info.gleam
Normal file
13
examples/info/src/info.gleam
Normal file
|
@ -0,0 +1,13 @@
|
|||
import apkallone
|
||||
import gleam/io
|
||||
import mastodon/instance
|
||||
|
||||
pub fn main() {
|
||||
io.println_error("Fetching...")
|
||||
|
||||
let assert Ok(instance) =
|
||||
apkallone.Client("https://tech.lgbt")
|
||||
|> instance.get_instance
|
||||
|
||||
io.debug(instance)
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
name = "apkallone"
|
||||
version = "1.0.0"
|
||||
version = "0.1.0"
|
||||
|
||||
# Fill out these fields if you intend to generate HTML documentation or publish
|
||||
# your project to the Hex package manager.
|
||||
|
@ -14,6 +14,9 @@ version = "1.0.0"
|
|||
|
||||
[dependencies]
|
||||
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
|
||||
gleam_httpc = ">= 3.0.0 and < 4.0.0"
|
||||
gleam_json = ">= 2.0.0 and < 3.0.0"
|
||||
gleam_http = ">= 3.7.0 and < 4.0.0"
|
||||
|
||||
[dev-dependencies]
|
||||
gleeunit = ">= 1.0.0 and < 2.0.0"
|
||||
|
|
18
manifest.toml
Normal file
18
manifest.toml
Normal file
|
@ -0,0 +1,18 @@
|
|||
# This file was generated by Gleam
|
||||
# You typically do not need to edit this file
|
||||
|
||||
packages = [
|
||||
{ name = "gleam_erlang", version = "0.27.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "DE468F676D71B313C6C8C5334425CFCF827837333F8AB47B64D8A6D7AA40185D" },
|
||||
{ name = "gleam_http", version = "3.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "EA66440C2269F7CED0F6845E5BD0DB68095775D627FA709A841CA78A398D6D56" },
|
||||
{ name = "gleam_httpc", version = "3.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "091CDD2BEC8092E82707BEA03FB5205A2BBBDE4A2F551E3C069E13B8BC0C428E" },
|
||||
{ name = "gleam_json", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "CB10B0E7BF44282FB25162F1A24C1A025F6B93E777CCF238C4017E4EEF2CDE97" },
|
||||
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
|
||||
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
|
||||
]
|
||||
|
||||
[requirements]
|
||||
gleam_http = { version = ">= 3.7.0 and < 4.0.0" }
|
||||
gleam_httpc = { version = ">= 3.0.0 and < 4.0.0" }
|
||||
gleam_json = { version = ">= 2.0.0 and < 3.0.0" }
|
||||
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
|
||||
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
|
|
@ -1,5 +1,14 @@
|
|||
import gleam/io
|
||||
import gleam/dynamic.{type Dynamic}
|
||||
import gleam/json.{type DecodeError}
|
||||
|
||||
pub fn main() {
|
||||
io.println("Hello from apkallone!")
|
||||
pub const user_agent = "Apkallone/0.1.0"
|
||||
|
||||
pub type Client {
|
||||
Client(instance: String)
|
||||
}
|
||||
|
||||
pub type Error {
|
||||
BadInstance
|
||||
FetchError(error: Dynamic)
|
||||
DecodeError(error: DecodeError)
|
||||
}
|
||||
|
|
36
src/apkallone/internal.gleam
Normal file
36
src/apkallone/internal.gleam
Normal file
|
@ -0,0 +1,36 @@
|
|||
import gleam/dynamic.{type Decoder}
|
||||
import gleam/http/request.{type Request}
|
||||
import gleam/http/response.{type Response}
|
||||
import gleam/httpc
|
||||
import gleam/json
|
||||
import gleam/result
|
||||
|
||||
import apkallone.{type Client, type Error}
|
||||
|
||||
pub fn prepare_request(
|
||||
client: Client,
|
||||
endpoint: String,
|
||||
) -> Result(Request(String), Error) {
|
||||
use req <- result.try(
|
||||
request.to(client.instance <> endpoint)
|
||||
|> result.replace_error(apkallone.BadInstance),
|
||||
)
|
||||
|
||||
req
|
||||
|> request.prepend_header("user-agent", apkallone.user_agent)
|
||||
|> Ok
|
||||
}
|
||||
|
||||
pub fn send_request(req: Request(String)) -> Result(Response(String), Error) {
|
||||
httpc.send(req)
|
||||
|> result.map_error(apkallone.FetchError)
|
||||
}
|
||||
|
||||
pub fn decode_response(
|
||||
res: Response(String),
|
||||
decoder: Decoder(a),
|
||||
) -> Result(a, Error) {
|
||||
res.body
|
||||
|> json.decode(decoder)
|
||||
|> result.map_error(apkallone.DecodeError)
|
||||
}
|
13
src/mastodon/instance.gleam
Normal file
13
src/mastodon/instance.gleam
Normal file
|
@ -0,0 +1,13 @@
|
|||
import gleam/result
|
||||
|
||||
import apkallone
|
||||
import apkallone/internal
|
||||
import mastodon/model/instance.{type Instance}
|
||||
|
||||
pub fn get_instance(
|
||||
client: apkallone.Client,
|
||||
) -> Result(Instance, apkallone.Error) {
|
||||
use req <- result.try(internal.prepare_request(client, "/api/v2/instance"))
|
||||
use res <- result.try(internal.send_request(req))
|
||||
internal.decode_response(res, instance.decode)
|
||||
}
|
42
src/mastodon/model/instance.gleam
Normal file
42
src/mastodon/model/instance.gleam
Normal file
|
@ -0,0 +1,42 @@
|
|||
import gleam/dynamic.{type DecodeError, type Dynamic}
|
||||
|
||||
// TODO
|
||||
pub type Instance {
|
||||
Instance(
|
||||
domain: String,
|
||||
title: String,
|
||||
version: String,
|
||||
description: String,
|
||||
usage: Usage,
|
||||
)
|
||||
}
|
||||
|
||||
pub type Usage {
|
||||
Usage(users: Users)
|
||||
}
|
||||
|
||||
pub type Users {
|
||||
Users(active_month: Int)
|
||||
}
|
||||
|
||||
pub fn decode(dynamic: Dynamic) -> Result(Instance, List(DecodeError)) {
|
||||
dynamic
|
||||
|> dynamic.decode5(
|
||||
Instance,
|
||||
dynamic.field("domain", dynamic.string),
|
||||
dynamic.field("title", dynamic.string),
|
||||
dynamic.field("version", dynamic.string),
|
||||
dynamic.field("description", dynamic.string),
|
||||
dynamic.field("usage", decode_usage),
|
||||
)
|
||||
}
|
||||
|
||||
fn decode_usage(dynamic: Dynamic) -> Result(Usage, List(DecodeError)) {
|
||||
dynamic
|
||||
|> dynamic.decode1(Usage, dynamic.field("users", decode_users))
|
||||
}
|
||||
|
||||
fn decode_users(dynamic: Dynamic) -> Result(Users, List(DecodeError)) {
|
||||
dynamic
|
||||
|> dynamic.decode1(Users, dynamic.field("active_month", dynamic.int))
|
||||
}
|
Loading…
Reference in a new issue