github_api/
github_api.rs

1#![allow(dead_code, unused)]
2
3use fetch_happen::{get, Client, Result};
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7#[derive(Debug, Deserialize)]
8struct GitHubBranch {
9    name: String,
10    commit: Commit,
11}
12
13#[derive(Debug, Deserialize)]
14struct Commit {
15    sha: String,
16    url: String,
17}
18
19/// Simple GET request using convenience function
20async fn get_github_branch(repo: String) -> Result<GitHubBranch> {
21    let url = format!("https://api.github.com/repos/{}/branches/master", repo);
22
23    let response = get(&url).await?.error_for_status()?;
24
25    response.json().await
26}
27
28/// Advanced GET request with custom headers
29async fn get_github_branch_advanced(repo: String) -> Result<GitHubBranch> {
30    let client = Client;
31    let url = format!("https://api.github.com/repos/{}/branches/master", repo);
32
33    let response = client
34        .get(url)
35        .header("Accept", "application/vnd.github.v3+json")
36        .header("User-Agent", "rust-wasm-fetch")
37        .send()
38        .await?
39        .error_for_status()?;
40
41    response.json().await
42}
43
44#[derive(Serialize)]
45struct CreateIssue {
46    title: String,
47    body: String,
48}
49
50/// POST request with JSON body
51async fn create_github_issue(repo: String, title: String, body: String) -> Result<Value> {
52    let client = Client;
53    let url = format!("https://api.github.com/repos/{}/issues", repo);
54
55    let issue = CreateIssue { title, body };
56
57    let response = client
58        .post(url)
59        .header("Accept", "application/vnd.github.v3+json")
60        .header("Authorization", "token YOUR_GITHUB_TOKEN")
61        .json(&issue)?
62        .send()
63        .await?
64        .error_for_status()?;
65
66    response.json_value().await
67}
68
69fn main() {}