Packages

A tool to audit Erlang & Elixir dependencies, to make sure your Gleam ✨ projects really sparkle!

Retired package: Security issue - Vulnerability warnings were never emitted due to an inverted advisory filter. Upgrade to 3.3.1 or 4.0.0-rc2+.

Current section

Files

Jump to
go_over src go_over hex puller.gleam
Raw

src/go_over/hex/puller.gleam

@target(erlang)
import gleam/http/request
@target(erlang)
import gleam/httpc
import gleam/result
@target(erlang)
import gleam/string
import go_over/util/util
import simplifile
@target(erlang)
pub const default = Native
@target(erlang)
pub const default_string = "native"
@target(javascript)
pub const default = CURL
@target(javascript)
pub const default_string = "curl"
pub type Puller {
Native
CURL
WGET
HTTPIE
Mock(result_filepath: String)
}
@target(erlang)
fn native_get(url: String) -> Result(String, #(Int, String)) {
url
|> request.to()
|> result.replace_error(httpc.InvalidUtf8Response)
|> result.try(httpc.send)
|> result.map(fn(resp) { resp.body })
|> result.map_error(fn(err) { #(1, string.inspect(err)) })
}
@target(javascript)
fn native_get(_: String) -> Result(String, #(Int, String)) {
Error(#(1, "Native puller is only supported on the Erlang target"))
}
pub fn run(puller: Puller, url: String) -> Result(String, #(Int, String)) {
case puller {
Native -> native_get(url)
CURL -> util.retry_cmd("curl", ["-sf", url])
WGET -> util.retry_cmd("wget", ["-qO-", url])
HTTPIE -> util.retry_cmd("https", ["--body", url])
Mock(result_filepath: result_filepath) ->
simplifile.read(result_filepath)
|> result.replace_error(#(1, "Mock Failure"))
}
}