Skip to content

Commit cbb8d70

Browse files
committed
Add read function
1 parent 869aca2 commit cbb8d70

File tree

6 files changed

+107
-2
lines changed

6 files changed

+107
-2
lines changed

.example.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
GITHUB_OWNER='foo'
2+
GITHUB_REPO='bar'
3+
GITHUB_TOKEN='baz'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.ez
33
/build
44
erl_crash.dump
5+
.env

gleam.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ repository = { type = "github", user = "jmcharter", repo = "https://github.com/j
1414

1515
[dependencies]
1616
gleam_stdlib = ">= 0.44.0 and < 2.0.0"
17+
envoy = ">= 1.0.2 and < 2.0.0"
18+
gleam_http = ">= 4.1.1 and < 5.0.0"
19+
gleam_httpc = ">= 5.0.0 and < 6.0.0"
20+
logging = ">= 1.3.0 and < 2.0.0"
1721

1822
[dev-dependencies]
1923
gleeunit = ">= 1.0.0 and < 2.0.0"

manifest.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This file was generated by Gleam
2+
# You typically do not need to edit this file
3+
4+
packages = [
5+
{ name = "envoy", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "95FD059345AA982E89A0B6E2A3BF1CF43E17A7048DCD85B5B65D3B9E4E39D359" },
6+
{ name = "gleam_erlang", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "1124AD3AA21143E5AF0FC5CF3D9529F6DB8CA03E43A55711B60B6B7B3874375C" },
7+
{ name = "gleam_http", version = "4.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "DD0271B32C356FB684EC7E9F48B1E835D0480168848581F68983C0CC371405D4" },
8+
{ name = "gleam_httpc", version = "5.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gleam_httpc", source = "hex", outer_checksum = "C545172618D07811494E97AAA4A0FB34DA6F6D0061FDC8041C2F8E3BE2B2E48F" },
9+
{ name = "gleam_stdlib", version = "0.62.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "0080706D3A5A9A36C40C68481D1D231D243AF602E6D2A2BE67BA8F8F4DFF45EC" },
10+
{ name = "gleeunit", version = "1.6.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "FDC68A8C492B1E9B429249062CD9BAC9B5538C6FBF584817205D0998C42E1DAC" },
11+
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
12+
]
13+
14+
[requirements]
15+
envoy = { version = ">= 1.0.2 and < 2.0.0" }
16+
gleam_http = { version = ">= 4.1.1 and < 5.0.0" }
17+
gleam_httpc = { version = ">= 5.0.0 and < 6.0.0" }
18+
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
19+
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
20+
logging = { version = ">= 1.3.0 and < 2.0.0" }

src/git_store.gleam

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,80 @@
1+
import gleam/http
2+
import gleam/http/request
3+
import gleam/http/response
4+
import gleam/httpc
15
import gleam/io
6+
import gleam/result
7+
import gleam/string
28

3-
pub type GithubConfig {
4-
GithubConfig(owner: String, repo: String, token: String)
9+
import envoy
10+
import logging
11+
12+
import git_store/utils
13+
14+
pub type GitStoreError {
15+
ParsingError(String)
16+
HTTPError(String)
17+
GitHubError
18+
}
19+
20+
pub type GitHubConfig {
21+
GithubConfig(owner: String, repo: String, token: String, base_url: String)
22+
}
23+
24+
pub type GitHubFileResponse {
25+
GitHubFileResponse(content: String, encoding: String, sha: String, size: Int)
26+
}
27+
28+
pub fn repos_url(config: GitHubConfig) -> String {
29+
config.base_url <> "/repos"
30+
}
31+
32+
pub fn read_file(
33+
config: GitHubConfig,
34+
path: String,
35+
) -> Result(response.Response(String), GitStoreError) {
36+
let url =
37+
utils.build_path(config.base_url, [
38+
"repos",
39+
config.owner,
40+
config.repo,
41+
"contents",
42+
])
43+
<> "/"
44+
<> path
45+
46+
request(config, http.Get, url)
47+
}
48+
49+
fn request(config: GitHubConfig, method: http.Method, endpoint: String) {
50+
use req <- result.try(
51+
request.to(endpoint)
52+
|> result.map_error(fn(_) {
53+
logging.log(logging.Error, "Unable to parse URL")
54+
ParsingError("Unable to parse URL: " <> endpoint)
55+
}),
56+
)
57+
let res =
58+
req
59+
|> request.set_header("Authorization", "Bearer " <> config.token)
60+
|> request.set_header("Accept", "application/vnd.github+json")
61+
|> request.set_header("User-Agent", "GitStore-Gleam")
62+
|> request.set_method(method)
63+
|> httpc.send()
64+
65+
res
66+
|> result.map_error(fn(error) {
67+
logging.log(logging.Error, string.inspect(error))
68+
HTTPError(string.inspect(error))
69+
})
570
}
671

772
pub fn main() -> Nil {
73+
logging.configure()
74+
let owner = envoy.get("GITHUB_OWNER") |> result.unwrap("")
75+
let repo = envoy.get("GITHUB_REPO") |> result.unwrap("")
76+
let token = envoy.get("GITHUB_TOKEN") |> result.unwrap("")
77+
let config = GithubConfig(owner, repo, token, "https://api.github.com")
78+
let file = read_file(config, "README.md")
879
io.println("Hello from git_store!")
980
}

src/git_store/utils.gleam

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import gleam/list
2+
3+
pub fn build_path(base: String, segments: List(String)) -> String {
4+
segments
5+
|> list.fold(base, fn(path, segment) { path <> "/" <> segment })
6+
}

0 commit comments

Comments
 (0)