@sigit / si Mirror · read-only
Fork 0

Mirror of github.com/getsigit/si.

Mirrored from https://github.com/getsigit/si.git

Synced Nov 24, 09:30 UTC

1 //! A repository's default branch, published as a static site.
2
3 use {
4 crate::{client::Client, error::Result, models::Site},
5 reqwest::{Method, RequestBuilder},
6 };
7
8 impl Client {
9 /// `GET /api/v1/repos/:owner/:repo/site` — `404` when the repository has no
10 /// site.
11 pub async fn site(&self, owner: &str, repo: &str) -> Result<Site> {
12 self.get(&site_path(owner, repo)).await
13 }
14
15 /// `POST /api/v1/repos/:owner/:repo/site` — publishes the site, or
16 /// publishes it again. Owner only. Publishing runs in the background, so
17 /// the answer has state `publishing`; poll [`Client::site`] for `live` or
18 /// `failed`. Publishing too often answers `429`.
19 pub async fn publish_site(&self, owner: &str, repo: &str) -> Result<Site> {
20 self.send(self.publish_site_request(owner, repo)).await
21 }
22
23 /// `DELETE /api/v1/repos/:owner/:repo/site` — takes the site offline. The
24 /// server does it in the background.
25 pub async fn unpublish_site(&self, owner: &str, repo: &str) -> Result<()> {
26 self.delete(&site_path(owner, repo)).await
27 }
28
29 fn publish_site_request(&self, owner: &str, repo: &str) -> RequestBuilder {
30 self.request(Method::POST, &site_path(owner, repo))
31 .json(&serde_json::json!({}))
32 }
33 }
34
35 fn site_path(owner: &str, repo: &str) -> String {
36 format!("repos/{owner}/{repo}/site")
37 }
38
39 #[cfg(test)]
40 mod tests {
41 use {super::*, crate::Environment};
42
43 #[test]
44 fn publishing_posts_to_the_site_path() {
45 let client = Client::builder(Environment::Production)
46 .base_url("https://sigit.si")
47 .token("tok")
48 .build()
49 .unwrap();
50 let request = client.publish_site_request("seto", "demo").build().unwrap();
51
52 assert_eq!(request.method(), Method::POST);
53 assert_eq!(
54 request.url().as_str(),
55 "https://sigit.si/api/v1/repos/seto/demo/site"
56 );
57 }
58
59 #[test]
60 fn a_site_parses() {
61 let body = r#"{"url":"https://demo.example","state":"live","error":null,
62 "last_published_at":"2026-10-01T10:00:00Z","files":4,"bytes":2048}"#;
63 let site: Site = serde_json::from_str(body).unwrap();
64 assert_eq!(site.state, "live");
65 assert_eq!(site.files, 4);
66 }
67
68 #[test]
69 fn a_site_still_publishing_has_no_timestamp_yet() {
70 let body = r#"{"url":"https://demo.example","state":"publishing","error":null,
71 "last_published_at":null,"files":0,"bytes":0}"#;
72 let site: Site = serde_json::from_str(body).unwrap();
73 assert!(site.last_published_at.is_none());
74 }
75 }