Skip to main content

Client

Struct Client 

Source
pub struct Client { /* private fields */ }
Expand description

An HTTP client for making authenticated requests to the GitHub API.

Wraps a reqwest::blocking::Client and automatically attaches the appropriate Authorization header and Accept header.

Implementations§

Source§

impl Client

Source

pub fn new(hostname: &str) -> Result<Self, GorError>

Create a new Client for the given host.

Attempts to load a stored token from the OS keyring. If no token is found, the client is created in an unauthenticated state.

§Examples
use gor::client::Client;

let client = Client::new("github.com").unwrap();
§Errors

Returns an error if the reqwest client cannot be created.

Source

pub fn with_token(hostname: &str, token: &str) -> Result<Self, GorError>

Create a new Client with an explicit token (bypasses keyring).

§Examples
use gor::client::Client;

let client = Client::with_token("github.com", "gho_abc123").unwrap();
§Errors

Returns an error if the reqwest client cannot be created.

Source

pub const fn host(&self) -> &Host

Returns the host this client is configured for.

Source

pub const fn is_authenticated(&self) -> bool

Returns true if the client has an auth token.

Source

pub fn token(&self) -> Option<&str>

Returns the auth token, if available.

Source

pub fn get(&self, path: &str) -> Result<Response, GorError>

Make a GET request to the given API path.

The path should start with /, e.g. /user.

§Errors

Returns an error if the HTTP request fails.

Source

pub fn get_absolute(&self, url: &str) -> Result<Response, GorError>

Make a GET request to an absolute URL (not API-path-based).

Used for downloading release assets from the GitHub CDN.

§Errors

Returns an error if the HTTP request fails.

Source

pub fn request( &self, method: &str, path: &str, headers: &[String], body: Option<Vec<u8>>, ) -> Result<Response, GorError>

Make a request with an arbitrary HTTP method, headers, and optional body.

The path should start with /, e.g. /repos/owner/repo. headers is a slice of “Key: Value” strings. body is an optional raw byte vector for the request body.

§Errors

Returns an error if the HTTP request fails.

Source

pub fn post<T: Serialize>( &self, path: &str, body: &T, ) -> Result<Response, GorError>

Make a POST request to the given API path with a JSON body.

§Errors

Returns an error if the HTTP request fails.

Source

pub fn post_form_url( &self, url: &str, form: &HashMap<&str, &str>, ) -> Result<Response, GorError>

Make a POST request to an absolute URL (not API-path-based) with form data.

Used for OAuth device flow endpoints which live on the main host, not the API subdomain.

§Errors

Returns an error if the HTTP request fails.

Source

pub fn graphql( &self, query: &str, variables: Option<Value>, ) -> Result<Value, GorError>

Make a GraphQL API request.

Sends a POST request to the GraphQL endpoint with the given query and optional variables.

§Errors

Returns an error if the HTTP request fails.

Source

pub fn upload_asset( &self, upload_url: &str, data: &[u8], content_type: &str, ) -> Result<Response, GorError>

Upload a release asset to the given upload URL.

GitHub release asset uploads use a separate domain (uploads.github.com) and require the content type to be set explicitly.

§Errors

Returns an error if the HTTP request fails.

Source

pub fn save_token(&self) -> Result<(), GorError>

Store the current token in the OS keyring.

§Errors

Returns an error if the keyring operation fails.

Source

pub fn set_token(&mut self, token: String) -> Result<(), GorError>

Update the client’s token and persist it.

§Errors

Returns an error if the storage operation fails.

Source

pub fn save_user(&self, user: &str) -> Result<(), GorError>

Store the authenticated user’s login name.

§Errors

Returns an error if the storage operation fails.

Source

pub fn stored_user(&self) -> Result<Option<String>, GorError>

Retrieve the stored user login, if available.

§Errors

Returns an error if the storage read fails.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more