aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/build/packages/adglent/src/priv/aoc_client.gleam
blob: e18bafac3b840dbb039755af16817585dcb0c938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import gleam/result.{try}
import gleam/httpc
import gleam/http/request
import gleam/int
import gleam/string

pub fn get_input(
  year: String,
  day: String,
  session: String,
) -> Result(String, String) {
  let url = "https://adventofcode.com/" <> year <> "/day/" <> day <> "/input"
  use request <- try(
    request.to(url)
    |> result.map_error(fn(error) {
      "Could not create request for \"" <> url <> "\": " <> string.inspect(
        error,
      )
    }),
  )

  // Send the HTTP request to the server
  use response <- try(
    request
    |> request.prepend_header("Accept", "application/json")
    |> request.prepend_header("Cookie", "session=" <> session <> ";")
    |> httpc.send
    |> result.map_error(fn(error) {
      "Error when requesting \"" <> url <> "\": " <> string.inspect(error)
    }),
  )

  case response.status {
    status if status >= 200 && status < 300 -> Ok(response.body)
    status -> Error(int.to_string(status) <> " - " <> response.body)
  }
}