Skip to main content

gitlab/api/projects/repository/files/
file_raw.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use derive_builder::Builder;
8
9use crate::api::common::{self, NameOrId};
10use crate::api::endpoint_prelude::*;
11
12/// Get raw file from repository.
13///
14/// Note: This endpoint returns raw data, so [`crate::api::raw`] is recommended to avoid the normal
15/// JSON parsing present in the typical endpoint handling.
16#[derive(Debug, Builder, Clone)]
17#[builder(setter(strip_option))]
18pub struct FileRaw<'a> {
19    /// The project to get a file within.
20    #[builder(setter(into))]
21    project: NameOrId<'a>,
22    /// The path to the file in the repository.
23    ///
24    /// This is automatically escaped as needed.
25    #[builder(setter(into))]
26    file_path: Cow<'a, str>,
27    /// The ref to get a file from.
28    #[builder(setter(into), default)]
29    ref_: Option<Cow<'a, str>>,
30    /// If `true`, resolve LFS pointers to their backing data.
31    ///
32    /// If the file is not an LFS pointer it is ignored.
33    #[builder(default)]
34    lfs: Option<bool>,
35}
36
37impl<'a> FileRaw<'a> {
38    /// Create a builder for the endpoint.
39    pub fn builder() -> FileRawBuilder<'a> {
40        FileRawBuilder::default()
41    }
42}
43
44impl Endpoint for FileRaw<'_> {
45    fn method(&self) -> Method {
46        Method::GET
47    }
48
49    fn endpoint(&self) -> Cow<'static, str> {
50        format!(
51            "projects/{}/repository/files/{}/raw",
52            self.project,
53            common::path_escaped(&self.file_path),
54        )
55        .into()
56    }
57
58    fn parameters(&self) -> QueryParams<'_> {
59        let mut params = QueryParams::default();
60
61        params
62            .push_opt("ref", self.ref_.as_ref())
63            .push_opt("lfs", self.lfs);
64
65        params
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use http::Method;
72
73    use crate::api::projects::repository::files::{FileRaw, FileRawBuilderError};
74    use crate::api::{self, Query};
75    use crate::test::client::{ExpectedUrl, SingleTestClient};
76
77    #[test]
78    fn all_parameters_are_needed() {
79        let err = FileRaw::builder().build().unwrap_err();
80        crate::test::assert_missing_field!(err, FileRawBuilderError, "project");
81    }
82
83    #[test]
84    fn project_is_required() {
85        let err = FileRaw::builder()
86            .file_path("new/file")
87            .build()
88            .unwrap_err();
89        crate::test::assert_missing_field!(err, FileRawBuilderError, "project");
90    }
91
92    #[test]
93    fn file_path_is_required() {
94        let err = FileRaw::builder().project(1).build().unwrap_err();
95        crate::test::assert_missing_field!(err, FileRawBuilderError, "file_path");
96    }
97
98    #[test]
99    fn sufficient_parameters() {
100        FileRaw::builder()
101            .project(1)
102            .file_path("new/file")
103            .build()
104            .unwrap();
105    }
106
107    #[test]
108    fn endpoint() {
109        let endpoint = ExpectedUrl::builder()
110            .method(Method::GET)
111            .endpoint("projects/simple%2Fproject/repository/files/path%2Fto%2Ffile/raw")
112            .build()
113            .unwrap();
114        let client = SingleTestClient::new_raw(endpoint, "");
115
116        let endpoint = FileRaw::builder()
117            .project("simple/project")
118            .file_path("path/to/file")
119            .build()
120            .unwrap();
121        api::ignore(endpoint).query(&client).unwrap();
122    }
123
124    #[test]
125    fn endpoint_ref() {
126        let endpoint = ExpectedUrl::builder()
127            .method(Method::GET)
128            .endpoint("projects/simple%2Fproject/repository/files/path%2Fto%2Ffile/raw")
129            .add_query_params(&[("ref", "branch")])
130            .build()
131            .unwrap();
132        let client = SingleTestClient::new_raw(endpoint, "");
133
134        let endpoint = FileRaw::builder()
135            .project("simple/project")
136            .file_path("path/to/file")
137            .ref_("branch")
138            .build()
139            .unwrap();
140        api::ignore(endpoint).query(&client).unwrap();
141    }
142
143    #[test]
144    fn endpoint_lfs() {
145        let endpoint = ExpectedUrl::builder()
146            .method(Method::GET)
147            .endpoint("projects/simple%2Fproject/repository/files/path%2Fto%2Ffile/raw")
148            .add_query_params(&[("lfs", "true")])
149            .build()
150            .unwrap();
151        let client = SingleTestClient::new_raw(endpoint, "");
152
153        let endpoint = FileRaw::builder()
154            .project("simple/project")
155            .file_path("path/to/file")
156            .lfs(true)
157            .build()
158            .unwrap();
159        api::ignore(endpoint).query(&client).unwrap();
160    }
161}