slackline/httpc

Send Web API calls with gleam_httpc on the Erlang target.

let client = slackline.new(token)
auth.test_() |> httpc.send(client)

Values

pub fn fold_pages(
  call: slackline.Call(m, a),
  client: slackline.Client,
  from initial: b,
  with fold: fn(b, a) -> b,
  metadata metadata: fn(a) -> option.Option(
    slackline.ResponseMetadata,
  ),
) -> Result(b, slackline.Error)

Sends a call and every following page, folding each response into an accumulator. metadata finds a response’s response_metadata, which holds the cursor of the next page. Stops at the first error.

conversations.list()
|> httpc.fold_pages(
  client,
  from: [],
  with: fn(channels, page) { list.append(channels, option.unwrap(page.channels, [])) },
  metadata: fn(page) { page.response_metadata },
)
pub fn retrying(
  attempts attempts: Int,
  run run: fn() -> Result(a, slackline.Error),
) -> Result(a, slackline.Error)

Runs run again while Slack answers RateLimited, waiting the retry_after seconds Slack asks for each time. Gives up after attempts tries in total and returns the last result. Other errors return at once.

use <- httpc.retrying(attempts: 3)
chat.post_message(channel:, text: "hi") |> httpc.send(client)
pub fn send(
  call: slackline.Call(m, a),
  client: slackline.Client,
) -> Result(a, slackline.Error)

Sends a call and decodes its response.

pub fn send_with(
  call: slackline.Call(m, a),
  client: slackline.Client,
  config: httpc.Configuration,
) -> Result(a, slackline.Error)

Sends a call with a custom httpc configuration, such as a longer timeout.

pub fn upload(
  upload_url: String,
  contents: BitArray,
) -> Result(Nil, slackline.Error)

Uploads file contents to an upload_url from files.getUploadURLExternal.

pub fn upload_files(
  client: slackline.Client,
  uploads: List(slackline.FileUpload),
  share share: fn(
    slackline.Call(
      files.CompleteUploadExternal,
      files.CompleteUploadExternalResponse,
    ),
  ) -> slackline.Call(
    files.CompleteUploadExternal,
    files.CompleteUploadExternalResponse,
  ),
) -> Result(files.CompleteUploadExternalResponse, slackline.Error)

Uploads files and shares them in one message: the three steps of files.getUploadURLExternal, upload and files.completeUploadExternal. share adjusts the last call, for example to set the channel:

httpc.upload_files(client, [report], share: fn(call) {
  call
  |> complete_upload_external.channel_id(channel)
  |> complete_upload_external.initial_comment("this week's report")
})

Without a channel the files are uploaded but not shared. Each file’s content_type is not sent; Slack detects the type. Run the three steps yourself for alt_txt, snippet_type, or to retry only the upload.

pub fn webhook(
  url: String,
  body: json.Json,
) -> Result(Nil, slackline.Error)

Posts a message to an incoming webhook or response_url.

✨ Search Document