• Stars
    star
    1,058
  • Rank 43,617 (Top 0.9 %)
  • Language
    Rust
  • License
    Other
  • Created over 4 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A modern, extensible GitHub API Client for Rust.

Octocrab: A modern, extensible GitHub API client.

Rust crates.io Help Wanted Lines Of Code Documentation Crates.io

Octocrab is a third party GitHub API client, allowing you to easily build your own GitHub integrations or bots in Rust. Octocrab comes with two primary sets of APIs for communicating with GitHub, a high level strongly typed semantic API, and a lower level HTTP API for extending behaviour.

Adding Octocrab

Run this command in your terminal to add the latest version of Octocrab.

cargo add octocrab

Semantic API

The semantic API provides strong typing around GitHub's API, a set of models that maps to GitHub's types, and auth functions that are useful for GitHub apps. Currently, the following modules are available as of version 0.17.

Getting a Pull Request

// Get pull request #5 from `XAMPPRocky/octocrab`.
let issue = octocrab::instance().pulls("XAMPPRocky", "octocrab").get(5).await?;

All methods with multiple optional parameters are built as Builder structs, allowing you to easily specify parameters.

Listing issues

let octocrab = octocrab::instance();
// Returns the first page of all issues.
let mut page = octocrab
    .issues("XAMPPRocky", "octocrab")
    .list()
    // Optional Parameters
    .creator("XAMPPRocky")
    .state(params::State::All)
    .per_page(50)
    .send()
    .await?;

// Go through every page of issues. Warning: There's no rate limiting so
// be careful.
loop {
    for issue in &page {
        println!("{}", issue.title);
    }
    page = match octocrab
        .get_page::<models::issues::Issue>(&page.next)
        .await?
    {
        Some(next_page) => next_page,
        None => break,
    }
}

HTTP API

The typed API currently doesn't cover all of GitHub's API at this time, and even if it did GitHub is in active development and this library will likely always be somewhat behind GitHub at some points in time. However that shouldn't mean that in order to use those features, you have to fork or replace octocrab with your own solution.

Instead octocrab exposes a suite of HTTP methods allowing you to easily extend Octocrab's existing behaviour. Using these HTTP methods allows you to keep using the same authentication and configuration, while having control over the request and response. There is a method for each HTTP method, get, post, patch, put, delete, all of which accept a relative route and a optional body.

let user: octocrab::models::User = octocrab::instance()
    .get("/user", None::<&()>)
    .await?;

Each of the HTTP methods expects a body, formats the URL with the base URL, and errors if GitHub doesn't return a successful status, but this isn't always desired when working with GitHub's API, sometimes you need to check the response status or headers. As such there are companion methods _get, _post, etc. that perform no additional pre or post-processing to the request.

let octocrab = octocrab::instance();
let response = octocrab
    ._get("https://api.github.com/organizations")
    .await?;

// You can also use `Uri::builder().authority("<my custom base>").path_and_query("<my custom path>")` if you want to customize the base uri and path.
let response =  octocrab
    ._get(Uri::builder().path_and_query("/organizations").build().expect("valid uri"))
    .await?;

You can use the those HTTP methods to easily create your own extensions to Octocrab's typed API. (Requires async_trait).

use octocrab::{Octocrab, Page, Result, models};

#[async_trait::async_trait]
trait OrganisationExt {
  async fn list_every_organisation(&self) -> Result<Page<models::Organization>>;
}

#[async_trait::async_trait]
impl OrganisationExt for Octocrab {
  async fn list_every_organisation(&self) -> Result<Page<models::Organization>> {
    self.get("/organizations", None::<&()>).await
  }
}

You can also easily access new properties that aren't available in the current models using serde.

#[derive(Deserialize)]
struct RepositoryWithVisibility {
    #[serde(flatten)]
    inner: octocrab::models::Repository,
    visibility: String,
}

let my_repo = octocrab::instance()
    .get::<RepositoryWithVisibility>("https://api.github.com/repos/XAMPPRocky/octocrab", None::<&()>)
    .await?;

Static API

Octocrab also provides a statically reference counted version of its API, allowing you to easily plug it into existing systems without worrying about having to integrate and pass around the client.

// Initialises the static instance with your configuration and returns an
// instance of the client.
octocrab::initialise(octocrab::Octocrab::builder());
// Gets a instance of `Octocrab` from the static API. If you call this
// without first calling `octocrab::initialise` a default client will be
// initialised and returned instead.
let octocrab = octocrab::instance();

More Repositories

1

tokei

Count your code, quickly.
Rust
10,875
star
2

rasn

A Safe #[no_std] ASN.1 Codec Framework
Rust
153
star
3

fluent-templates

Easily add Fluent to your Rust project.
Rust
136
star
4

mean-bean-ci-template

Build and Test Your Rust Projects with Zero Configuration
Shell
100
star
5

Mammut

A wrapper for the Mastodon API in Rust.
Rust
69
star
6

remove_dir_all

Reliable remove_dir_all implementation for Windows
Rust
59
star
7

tokei_rs

The tokei.rs server code.
Rust
55
star
8

eve

A utility to easily search and replace with environment variables.
Rust
34
star
9

deploy-mdbook

A GitHub Action to automatically build and deploy your mdbook project.
TypeScript
29
star
10

i8080

An intel 8080 emulator
Rust
28
star
11

timetill.rs

A community website for highlighting Rust conferences.
Vue
23
star
12

region_buffer

A growable array allowing for multiple mutable non-overlapping regions.
Rust
16
star
13

hera

A program for checking if there were code changes between git commits.
Rust
9
star
14

unwrap_to

A Rust utility macro to unwrap enums.
Rust
8
star
15

errln

Convenience macros for writing to stderr.
Rust
6
star
16

get-github-release

A GitHub Action for downloading any binary available through GitHub Releases.
JavaScript
6
star
17

Polly

A truly logic-less templating language for Rust servers.
Rust
5
star
18

gh-auditor

Rust
3
star
19

event_feedback

A proof of concept for an event feedback system.
Rust
2
star
20

csv-to-md

A small tool to convert a csv to markdown.
Rust
2
star
21

numcount

A small utility that counts up the numbers in a file.
Rust
2
star
22

tonic-ice

Starlark
1
star
23

keywords

Rust
1
star
24

cargo-raze-bug

Rust
1
star