Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialize a page from a search #681

Open
dfberry opened this issue Aug 23, 2024 · 0 comments
Open

Serialize a page from a search #681

dfberry opened this issue Aug 23, 2024 · 0 comments

Comments

@dfberry
Copy link

dfberry commented Aug 23, 2024

I'm new to Rust. When I tried to serialize a page from search, I ran into issues with the Page itself. Here is what finally compiled but is it the right way to serialize an Octocrab Page?

use anyhow::Result;
use octocrab::models::Repository;
use octocrab::models::UserProfile as User;
use octocrab::models::issues::Issue;
use octocrab::Page;
use octocrab::Octocrab;
use http::*;
use serde::{Serialize, Deserialize};
use std::option::Option; 

#[derive(Serialize, Deserialize)]
pub struct IssueQueryDef {
    #[serde(flatten)]
    inner: octocrab::models::issues::Issue,
}

#[derive(Serialize, Deserialize)]
pub struct PageDef<T>{
    pub items: Vec<T>,
    pub incomplete_results: Option<bool>,
    pub total_count: Option<u64>,
    #[serde(with = "http_serde_ext::uri::option", default)]
    pub next: Option<Uri>,
    #[serde(with = "http_serde_ext::uri::option", default)]
    pub prev: Option<Uri>,
    #[serde(with = "http_serde_ext::uri::option", default)]
    pub first: Option<Uri>,
    #[serde(with = "http_serde_ext::uri::option", default)]
    pub last: Option<Uri>,
}

#[derive(Debug)]
pub struct GitHub {
    pub name: String,
}

// Conversion function
fn convert_page_to_page_def(page: Page<Issue>) -> PageDef<IssueQueryDef> {
    let items = page.items.into_iter().map(|issue| IssueQueryDef { inner: issue }).collect();
    PageDef {
        items,
        incomplete_results: page.incomplete_results,
        total_count: page.total_count,
        next: page.next,
        prev: page.prev,
        first: page.first,
        last: page.last,
    }
}

impl GitHub {
    pub async fn repo(token: &str, org_or_user: &str, repo_name: &str) -> Result<Repository> {
        let octocrab = Octocrab::builder()
            .personal_token(token.to_string())
            .build()?;

        let repo = octocrab.repos(org_or_user, repo_name).get().await?;

        Ok(repo)
    }
    pub async fn user_profile(token: &str, user: &str) -> Result<User> {
        let octocrab = Octocrab::builder()
            .personal_token(token.to_string())
            .build()?;

        let repo = octocrab.users(user).profile().await?;

        Ok(repo)
    }
    pub async fn query(token: &str, query: &str) -> Result<PageDef<IssueQueryDef>> {
        let octocrab = Octocrab::builder()
            .personal_token(token.to_string())
            .build()?;

        let query_result = octocrab
            .search()
            .issues_and_pull_requests(query) //"tokei is:pr"
            .send()
            .await?;

        let page_def = convert_page_to_page_def(query_result);

        Ok(page_def)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant