1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use octocrab::models::User;
use reqwest::Url;
use serde::*;

use crate::*;

#[derive(Serialize, Debug)]
pub struct UserRec {
    pub login: String,
    pub id: i64,
    pub node_id: String,
    pub avatar_url: Url,
    pub gravatar_id: String,
    pub url: Url,
    pub html_url: Url,
    pub followers_url: Url,
    pub following_url: Url,
    pub gists_url: Url,
    pub starred_url: Url,
    pub subscriptions_url: Url,
    pub organizations_url: Url,
    pub repos_url: Url,
    pub events_url: Url,
    pub received_events_url: Url,
    pub r#type: String,
    pub site_admin: bool,
}

impl RepositryAware for UserRec {
    fn set_repository(&mut self, _: String) {}
}

impl From<User> for UserRec {
    fn from(from: User) -> UserRec {
        Self {
            login: from.login,
            id: from.id,
            node_id: from.node_id,
            avatar_url: from.avatar_url,
            gravatar_id: from.gravatar_id,
            url: from.url,
            html_url: from.html_url,
            followers_url: from.followers_url,
            following_url: from.following_url,
            gists_url: from.gists_url,
            starred_url: from.starred_url,
            subscriptions_url: from.subscriptions_url,
            organizations_url: from.organizations_url,
            repos_url: from.repos_url,
            events_url: from.events_url,
            received_events_url: from.received_events_url,
            r#type: from.r#type,
            site_admin: from.site_admin,
        }
    }
}

pub struct UserFetcher {
    octocrab: octocrab::Octocrab,
}

impl UserFetcher {
    pub fn new(octocrab: octocrab::Octocrab) -> Self {
        Self { octocrab }
    }
}

impl UrlConstructor for UserFetcher {
    fn reponame(&self) -> String {
        "".to_string()
    }

    fn entrypoint(&self) -> Option<Url> {
        let param = Params::default();

        let route = format!("users?{query}", query = param.to_query());
        self.octocrab.absolute_url(route).ok()
    }
}

impl LoopWriter for UserFetcher {
    type Model = User;
    type Record = UserRec;
}

impl UserFetcher {
    pub async fn fetch<T: std::io::Write>(&self, mut wtr: csv::Writer<T>) -> octocrab::Result<()> {
        let mut next: Option<Url> = self.entrypoint();

        while let Some(page) = self.octocrab.get_page(&next).await? {
            next = self.write_and_continue(page, &mut wtr);
        }

        Ok(())
    }
}