Expand description
GitHub API client for authenticated operations.
This module provides the primary interface for interacting with GitHub’s REST API as a GitHub App. It handles authentication, rate limiting, retries, pagination, and provides type-safe operations for repositories, issues, pull requests, projects, and more.
§Overview
The client operates at two levels:
- App-level (
GitHubClient) - Operations as the GitHub App itself (using JWT) - Installation-level (
InstallationClient) - Operations within a specific installation (using installation tokens)
Most operations happen at the installation level, where you interact with repositories, issues, pull requests, etc. on behalf of the installation.
§Features
- Automatic Authentication - Tokens are injected and refreshed automatically
- Rate Limit Handling - Detects rate limits and backs off appropriately
- Retry Logic - Exponential backoff for transient failures (network errors, 5xx responses)
- Pagination Support - Helper methods for paginated API responses
- Type Safety - Strongly-typed request and response models
- Configurable - Timeouts, retry behavior, rate limit margins
§Quick Start
use github_bot_sdk::{
auth::{AuthenticationProvider, InstallationId},
client::{GitHubClient, ClientConfig},
error::ApiError,
};
use std::time::Duration;
// Create client with custom configuration
let client = GitHubClient::builder(auth)
.config(ClientConfig::default()
.with_user_agent("my-bot/1.0")
.with_timeout(Duration::from_secs(30))
.with_max_retries(5))
.build()?;
// Get app information (app-level operation)
let app = client.get_app().await?;
println!("Running as: {}", app.name);
// Get installation information
let installation_id = InstallationId::new(12345);
let installation = client.get_installation(installation_id).await?;
println!("Installation: {}", installation.id.as_u64());§Configuration
Use ClientConfig to customize client behavior:
use github_bot_sdk::client::ClientConfig;
use std::time::Duration;
let config = ClientConfig::default()
.with_user_agent("my-github-bot/1.0") // Required by GitHub
.with_timeout(Duration::from_secs(60)) // Request timeout
.with_max_retries(5) // Max retry attempts
.with_rate_limit_margin(0.1) // Keep 10% rate limit buffer
.with_github_api_url("https://api.github.com"); // Override for GHE
// Or use the builder pattern
let config = ClientConfig::builder()
.user_agent("my-bot/2.0")
.timeout(Duration::from_secs(45))
.max_retries(3)
.build();§Operations by Category
§Repository Operations
let installation_id = InstallationId::new(12345);
let _installation = client.get_installation(installation_id).await?;
// Repository operations are available through client methods
// See InstallationClient and related modules for detailed API§Issue Operations
let installation_id = InstallationId::new(12345);
let _installation = client.get_installation(installation_id).await?;
// Issue operations through client methods
// Create, comment, label, and manage issues§Pull Request Operations
let installation_id = InstallationId::new(12345);
let _installation = client.get_installation(installation_id).await?;
// Pull request operations through client methods
// Create, review, merge, and manage PRs§Project Operations (GitHub Projects V2)
let installation_id = InstallationId::new(12345);
let _installation = client.get_installation(installation_id).await?;
// Project operations through client methods§Rate Limiting
The client automatically handles GitHub’s rate limits:
- Detection - Monitors
X-RateLimit-*headers in responses - Proactive Backoff - Slows down when approaching limit (configurable margin)
- Reactive Handling - Respects
Retry-Afterheaders on 429 responses - Secondary Rate Limits - Handles abuse detection (403 with retry-after)
// Configure rate limit behavior
let config = ClientConfig::default()
.with_rate_limit_margin(0.15); // Start backing off at 85% of limit
let client = GitHubClient::builder(auth)
.config(config)
.build()?;
// Client will automatically handle rate limits
// No manual rate limit checking needed§Retry Behavior
The client automatically retries transient failures with exponential backoff:
-
Retryable Errors:
- Network errors (timeouts, connection failures)
- 5xx server errors (GitHub is having issues)
- 429 rate limit exceeded (respects Retry-After)
- 403 with Retry-After (secondary rate limit)
-
Non-Retryable Errors:
- 4xx client errors (except 429 and some 403s)
- Authentication failures
- Validation errors
use github_bot_sdk::client::ClientConfig;
use std::time::Duration;
// Configure retry behavior
let config = ClientConfig::default()
.with_max_retries(5) // Max attempts
.with_timeout(Duration::from_secs(30)); // Request timeout
// Default exponential backoff is applied automatically§Pagination
GitHub’s API uses Link headers for pagination. See specific operation documentation for examples.
let installation_id = InstallationId::new(12345);
// Get installation
let installation = client.get_installation(installation_id).await?;
println!("Installation: {}", installation.id.as_u64());§Error Handling
All operations return Result<T, ApiError>:
let installation_id = InstallationId::new(12345);
// Error handling example
match client.get_installation(installation_id).await {
Ok(installation) => println!("Found installation: {}", installation.id.as_u64()),
Err(ApiError::NotFound { .. }) => {
println!("Installation doesn't exist or no access");
}
Err(ApiError::RateLimitExceeded { reset_at, .. }) => {
println!("Rate limited, resets at: {:?}", reset_at);
}
Err(ApiError::AuthorizationFailed) => {
println!("Insufficient permissions");
}
Err(e) => return Err(e),
}§GitHub Enterprise Support
To use with GitHub Enterprise Server, configure the API base URL:
use github_bot_sdk::client::ClientConfig;
let config = ClientConfig::default()
.with_github_api_url("https://github.example.com/api/v3");§See Also
crate::auth- Authentication types and providerscrate::webhook- Webhook validation for incoming events- GitHub REST API Documentation
Structs§
- AddProject
V2Item Request - Request to add an item to a project.
- App
- GitHub App metadata.
- Branch
- Git branch with commit information.
- Client
Config - Configuration for GitHub API client behavior.
- Client
Config Builder - Builder for constructing
ClientConfiginstances. - Comment
- Comment on an issue.
- Commit
- Commit reference (used in branches, tags, and pull requests).
- Commit
Details - Git-level commit metadata.
- Commit
Reference - A minimal reference to a Git object containing only its SHA and API URL.
- Comparison
- The result of comparing two Git refs (commits, branches, or tags).
- Create
Comment Request - Request to create a comment.
- Create
Issue Request - Request to create a new issue.
- Create
Label Request - Request to create a label.
- Create
Pull Request Comment Request - Request to create a pull request comment.
- Create
Pull Request Request - Request to create a new pull request.
- Create
Release Request - Request to create a release.
- Create
Review Request - Request to create a review.
- Dismiss
Review Request - Request to dismiss a review.
- File
Change - A single file change within a
Comparison. - Full
Commit - A complete GitHub commit with all metadata.
- GitHub
Client - GitHub API client for authenticated operations.
- GitHub
Client Builder - Builder for constructing
GitHubClientinstances. - GitRef
- Git reference (branch, tag, etc.).
- GitSignature
- An identity record stored in a Git commit object.
- Installation
Client - Installation-scoped GitHub API client.
- Issue
- GitHub issue.
- Issue
User - User associated with an issue.
- Label
- GitHub label.
- Merge
Pull Request Request - Request to merge a pull request.
- Merge
Result - Result of merging a pull request.
- Milestone
- Milestone associated with an issue or pull request.
- Paged
Response - Paginated response wrapper.
- Pagination
- Pagination metadata extracted from Link headers.
- Project
Owner - Project owner (organisation or user).
- Project
V2 - GitHub Projects v2 project.
- Project
V2Item - Item in a GitHub Projects v2 project.
- Pull
Request - GitHub pull request.
- Pull
Request Branch - Branch information in a pull request.
- Pull
Request Comment - Comment on a pull request (review comment on code).
- Pull
Request Repo - Repository information in a pull request branch.
- Rate
Limit - Rate limit information from GitHub API response headers.
- Rate
Limit Info - Rate limit information from GitHub API.
- Rate
Limiter - Thread-safe rate limit tracker for GitHub API operations.
- Release
- GitHub release.
- Release
Asset - Asset attached to a release.
- Repository
- GitHub repository with metadata.
- Repository
Owner - Repository owner (user or organization).
- Retry
Policy - Retry policy for transient errors.
- Review
- Pull request review.
- SetIssue
Milestone Request - Request to set milestone on an issue.
- SetPull
Request Milestone Request - Request to set milestone on a pull request.
- Tag
- Git tag information.
- Trigger
Workflow Request - Request to trigger a workflow.
- Update
Comment Request - Request to update a comment.
- Update
Issue Request - Request to update an existing issue.
- Update
Label Request - Request to update a label.
- Update
Pull Request Request - Request to update an existing pull request.
- Update
Release Request - Request to update a release.
- Update
Review Request - Request to update a review.
- Verification
- GPG or SSH signature verification status for a commit.
- Workflow
- GitHub Actions workflow.
- Workflow
Run - GitHub Actions workflow run.
Enums§
- Owner
Type - Owner type classification.
- Rate
Limit Context - Authentication context for rate limit tracking.
Functions§
- calculate_
rate_ limit_ delay - Calculate delay for rate limit exceeded (429) response.
- detect_
secondary_ rate_ limit - Detect secondary rate limit (abuse detection) from 403 response.
- extract_
page_ number - Extract page number from a URL.
- parse_
link_ header - Parse pagination metadata from Link header.
- parse_
rate_ limit_ from_ headers - Parse rate limit information from HTTP response headers.
- parse_
retry_ after - Parse Retry-After header from HTTP response.