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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate::{
    builders::{
        builder, builder_nested_setters_non_optional, builder_nested_string_setters, builder_string_setters, Builder,
    },
    methods::{create_gist, get_user_gists, patch_gist, CreateGistBody, FileContents, Pagination, PatchGistBody},
    model::gists::{Gist, SimpleGist},
    GithubRestError, Requester,
};
use async_trait::async_trait;
use std::fmt::Display;

builder!(
    /// * tags gists
    /// * get `/users/{username}/gists`
    /// * docs <https://docs.github.com/rest/reference/gists#list-gists-for-a-user>
    ///
    /// List gists for a user
    /// Lists public gists for the specified user:
    GetGistsBuilder {
        owner: String,
        options: Pagination
    }
);

builder_string_setters!(GetGistsBuilder { owner });
builder_nested_string_setters!(GetGistsBuilder { options { per_page, page } });

#[async_trait]
impl Builder for GetGistsBuilder {
    type Response = Vec<SimpleGist>;

    async fn execute<T>(self, client: &T) -> Result<Self::Response, GithubRestError>
    where
        T: Requester,
    {
        get_user_gists(client, self.owner, Some(&self.options)).await
    }
}

builder!(
    /// * tags gists
    /// * post `/gists`
    /// * docs <https://docs.github.com/rest/reference/gists#create-a-gist>
    ///
    /// Create a gist
    /// Allows you to add a new gist with one or more files.
    ///
    /// **Note:** Don't name your files "gistfile" with a numerical suffix. This
    /// is the format of the automatic naming scheme that Gist uses
    /// internally.
    CreateGistBuilder { body: CreateGistBody }
);

builder_nested_string_setters!(CreateGistBuilder { body { description } });
builder_nested_setters_non_optional!(CreateGistBuilder { body { public: bool } });

impl CreateGistBuilder {
    pub fn file<A, B>(mut self, name: A, contents: B) -> Self
    where
        A: Into<String>,
        B: Display,
    {
        self.body.files.insert(name.into(), FileContents::from(contents));
        self
    }
}

#[async_trait]
impl Builder for CreateGistBuilder {
    type Response = Gist;

    async fn execute<T>(self, client: &T) -> Result<Self::Response, GithubRestError>
    where
        T: Requester,
    {
        create_gist(client, &self.body).await
    }
}

builder!(
    /// * tags gists
    /// * patch `/gists/{gist_id}`
    /// * docs <https://docs.github.com/rest/reference/gists/#update-a-gist>
    ///
    /// Update a gist
    /// Allows you to update or delete a gist file and rename gist files. Files
    /// from the previous version of the gist that aren't explicitly changed
    /// during an edit are unchanged.
    PatchGistBuilder {
        gist_id: String,
        body: PatchGistBody
    }
);

builder_string_setters!(PatchGistBuilder { gist_id });
builder_nested_string_setters!(PatchGistBuilder { body { description } });

impl PatchGistBuilder {
    pub fn file<A, B>(mut self, name: A, contents: B) -> Self
    where
        A: Into<String>,
        B: Display,
    {
        self.body.files.insert(name.into(), FileContents::from(contents));
        self
    }
}

#[async_trait]
impl Builder for PatchGistBuilder {
    type Response = Gist;

    async fn execute<T>(self, client: &T) -> Result<Self::Response, GithubRestError>
    where
        T: Requester,
    {
        patch_gist(client, self.gist_id, &self.body).await
    }
}

#[cfg(all(feature = "builders", feature = "client"))]
#[cfg(test)]
mod tests {
    use crate::{
        builders::{Builder, CreateGistBuilder, GetGistsBuilder, PatchGistBuilder},
        client::DefaultRequester,
        methods::util,
    };

    // You will need to change this to your own thing. The gist method tests have
    // the same issue. TODO: Work out a better system
    const GIST_ID: &str = "c2196b06d002d4ee278175bb82454a95";

    #[tokio::test]
    async fn test_get_gists_builder() {
        let req = GetGistsBuilder::new().owner("proudmuslim-dev");
        let requester = DefaultRequester::new_none();

        let res = req.execute(&requester).await.unwrap();

        dbg!(res);
    }

    #[tokio::test]
    async fn test_patch_gist_builder() {
        let req = PatchGistBuilder::new().gist_id(GIST_ID).description("Test description");

        let res = req.execute(&util::github_auth()).await.unwrap();

        dbg!(res);
    }

    #[tokio::test]
    async fn test_create_gist_builder() {
        let req = CreateGistBuilder::new()
            .description("Test description")
            .file("hello.rs", r#"fn main() { println!("Hello, world!") }"#)
            .file("goodbye.rs", r#"fn main() { println!("Goodbye, world!") }"#);

        let res = req.execute(&util::github_auth()).await.unwrap();

        dbg!(res);
    }
}