github-rest: Query the GitHub REST API programmatically

[ bsd3, github, library ] [ Propose Tags ] [ Report a vulnerability ]

Query the GitHub REST API programmatically, which can provide a more flexible and clear interface than if all of the endpoints and their types were defined as Haskell values.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.2.0, 1.2.1
Change log CHANGELOG.md
Dependencies aeson (<3), base (<5), bytestring (<0.13), crypton (<1.1), http-client (>=0.5.13 && <0.8), http-client-tls (<0.4), http-types (>=0.11 && <0.13), jose-jwt (>=0.10.0 && <0.11), mtl (<2.4), scientific (<0.4), text (<2.2), time (<1.15), transformers (<0.7), unliftio (<0.3), unliftio-core (<0.3) [details]
License BSD-3-Clause
Author Brandon Chinn <brandonchinn178@gmail.com>
Maintainer Brandon Chinn <brandonchinn178@gmail.com>
Category GitHub
Home page https://github.com/brandonchinn178/github-rest#readme
Bug tracker https://github.com/brandonchinn178/github-rest/issues
Source repo head: git clone https://github.com/brandonchinn178/github-rest
Uploaded by brandonchinn178 at 2024-06-07T06:05:22Z
Distributions LTSHaskell:1.2.1, NixOS:1.1.4, Stackage:1.2.1
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 2366 total (34 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-06-07 [all 1 reports]

Readme for github-rest-1.2.1

[back to package description]

github-rest

GitHub Actions Codecov Hackage

A package providing a more flexible interface to accessing the GitHub API. Endpoints are created using the GHEndpoint constructor and are executed with the queryGitHub function in the GitHubT monad.

Quickstart

This quickstart will demonstrate querying endpoints in a hypothetical public GitHub repo alice/my-project.

{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -fno-warn-type-defaults #-}

import Data.Text (Text)
import GitHub.REST
import Network.HTTP.Types (StdMethod(..))

default (Text)

main = do
  let state = GitHubSettings
        { token = Nothing
          -- ^ An authentication token to use, if any.
        , userAgent = "alice/my-project"
          -- ^ GitHub requires this to be set to a User Agent specific to your
          -- application: https://developer.github.com/v3/#user-agent-required
        , apiVersion = "v3"
          -- ^ Specifies the API version to query: https://developer.github.com/v3/media/
        }

  runGitHubT state $ do

    -- Get information for the "main" branch
    -- https://developer.github.com/v3/git/refs/#get-a-single-reference
    ref <- queryGitHub GHEndpoint
      { method = GET
        -- Colon-prefixed components in the endpoint will be interpolated by
        -- the values in 'endpointVals'.
        -- In this case, "/repos/alice/my-project/git/refs/heads/main"
      , endpoint = "/repos/:owner/:repo/git/refs/:ref"
      , endpointVals =
        [ "owner" := "alice"
        , "repo" := "my-project"
        , "ref" := "heads/main"
        ]
      , ghData = []
      }

    -- 'github-rest' provides a '.:' helper for when the API guarantees that a
    -- key in a JSON object exists
    --
    -- The result of 'queryGitHub' is anything that's an instance of FromJSON,
    -- if using manually-defined data types is preferred over using '.:'. This
    -- package can be easily used with the aeson-schemas library, which
    -- provides a type-safe way to query JSON data.
    let sha :: Text
        sha = ref .: "object" .: "sha"

    -- Create a new branch called "foo"
    -- https://developer.github.com/v3/git/refs/#create-a-reference
    queryGitHub GHEndpoint
      { method = POST
      , endpoint = "/repos/:owner/:repo/git/refs"
      , endpointVals =
        [ "owner" := "alice"
        , "repo" := "my-project"
        ]
      , ghData =
        [ "ref" := "refs/heads/foo"
        , "sha" := sha
        ]
      }

Comparison to other libraries

The github package provides a decent API for querying the GitHub API, and it defines Haskell data types for each endpoint. These data types can be used as the result of queryGitHub.

This package provides a different interface for people with different tastes:

  • github-rest informs the user exactly which GitHub endpoint is being hit (e.g. /repos/:owner/:repo). Users no longer need to spend time trying to scour documentation to find the corresponding function for an endpoint.

  • github-rest passes authentication once, with requests executed in a single monadic context. The github package requires passing in an authentication token every time a request is executed

  • In the same vein, github-rest provides a monad transformer that handles all GitHub state needed to execute queryGitHub. github runs everything in IO, expecting the caller to keep track of GitHub state manually.

  • github-rest allows usage with aeson-schemas