GhEx.REST (gh_ex v0.3.0)

Copy Markdown View Source

Generic REST access. Any GitHub REST path is reachable through get/3, post/3, patch/3, put/3, and delete/3.

Every call returns {:ok, body, meta} on a 2xx or {:error, reason} otherwise, where reason is a GhEx.Error for an API error response or a Req exception for a transport failure. meta is a GhEx.REST.Meta struct carrying the status, response headers, parsed pagination links, and rate-limit snapshot.

Options

Per-call options are passed through to Req, so the useful ones are:

  • :params - query string parameters, e.g. params: [state: "open", per_page: 100]
  • :json - a body to JSON-encode for post/patch/put

Examples

GhEx.REST.get(client, "/repos/elixir-lang/elixir")
GhEx.REST.get(client, "/repos/o/r/issues", params: [state: "open"])
GhEx.REST.post(client, "/repos/o/r/issues", json: %{title: "Bug", body: "..."})

Summary

Functions

Runs a request and returns the raw Req.Response, without normalizing it.

Auto-paginates a list endpoint into a lazy Stream of individual items.

Types

meta()

@type meta() :: GhEx.REST.Meta.t()

result()

@type result() :: {:ok, term(), meta()} | {:error, GhEx.Error.t() | Exception.t()}

Functions

delete(client, path, opts \\ [])

@spec delete(GhEx.Client.t(), String.t(), keyword()) :: result()

Issues a DELETE.

get(client, path, opts \\ [])

@spec get(GhEx.Client.t(), String.t(), keyword()) :: result()

Issues a GET.

patch(client, path, opts \\ [])

@spec patch(GhEx.Client.t(), String.t(), keyword()) :: result()

Issues a PATCH.

post(client, path, opts \\ [])

@spec post(GhEx.Client.t(), String.t(), keyword()) :: result()

Issues a POST.

put(client, path, opts \\ [])

@spec put(GhEx.Client.t(), String.t(), keyword()) :: result()

Issues a PUT.

raw(client, method, path, opts \\ [])

@spec raw(GhEx.Client.t(), atom(), String.t(), keyword()) ::
  {:ok, Req.Response.t()} | {:error, term()}

Runs a request and returns the raw Req.Response, without normalizing it.

Unlike the verb functions, a non-2xx response comes back as {:ok, %Req.Response{}} (inspect resp.status yourself) rather than {:error, _}. Useful when a 404 means "absent" rather than an error, or when you need the raw status, headers, or body, or custom decoding. Auth resolution can still fail, returning {:error, reason}. Compose with GhEx.Pagination.links/1 and GhEx.RateLimit.from_response/1 for links or a rate-limit snapshot.

stream(client, path, opts \\ [])

@spec stream(GhEx.Client.t(), String.t(), keyword()) :: Enumerable.t()

Auto-paginates a list endpoint into a lazy Stream of individual items.

Follows the Link: rel="next" header until GitHub stops handing one back. The first page's :params are applied once; subsequent pages use the exact next URL GitHub returns, so cursors are never double-applied. A failed page raises GhEx.Error.

client
|> GhEx.REST.stream("/repos/elixir-lang/elixir/issues", params: [state: "all", per_page: 100])
|> Stream.map(& &1["number"])
|> Enum.take(250)