jinxapi_github/v1_1_4/request/
search_code.rs

1//! Search code
2//! 
3//! Searches for query terms inside of a file. This method returns up to 100 results [per page](https://docs.github.com/rest/overview/resources-in-the-rest-api#pagination).
4//! 
5//! When searching for code, you can get text match metadata for the file **content** and file **path** fields when you pass the `text-match` media type. For more details about how to receive highlighted search results, see [Text match metadata](https://docs.github.com/rest/reference/search#text-match-metadata).
6//! 
7//! For example, if you want to find the definition of the `addClass` function inside [jQuery](https://github.com/jquery/jquery) repository, your query would look something like this:
8//! 
9//! `q=addClass+in:file+language:js+repo:jquery/jquery`
10//! 
11//! This query searches for the keyword `addClass` within a file's contents. The query limits the search to files where the language is JavaScript in the `jquery/jquery` repository.
12//! 
13//! #### Considerations for code search
14//! 
15//! Due to the complexity of searching code, there are a few restrictions on how searches are performed:
16//! 
17//! *   Only the _default branch_ is considered. In most cases, this will be the `master` branch.
18//! *   Only files smaller than 384 KB are searchable.
19//! *   You must always include at least one search term when searching source code. For example, searching for [`language:go`](https://github.com/search?utf8=%E2%9C%93&q=language%3Ago&type=Code) is not valid, while [`amazing
20//! language:go`](https://github.com/search?utf8=%E2%9C%93&q=amazing+language%3Ago&type=Code) is.
21//! 
22//! [API method documentation](https://docs.github.com/rest/reference/search#search-code)
23
24
25fn url_string(
26    base_url: &str,
27    q_q: &str,
28    q_sort: ::std::option::Option<&str>,
29    q_order: ::std::option::Option<&str>,
30    q_per_page: ::std::option::Option<i64>,
31    q_page: ::std::option::Option<i64>,
32) -> Result<String, crate::v1_1_4::ApiError> {
33    let trimmed = if base_url.is_empty() {
34        "https://api.github.com"
35    } else {
36        base_url.trim_end_matches('/')
37    };
38    let mut url = String::with_capacity(trimmed.len() + 32);
39    url.push_str(trimmed);
40    url.push_str("/search/code");
41    url.push('?');
42    ::querylizer::Form::extend(&mut url, "q", &q_q, false, &::querylizer::encode_query)?;
43    if let Some(value) = &q_sort {
44        url.push('&');
45        ::querylizer::Form::extend(&mut url, "sort", value, false, &::querylizer::encode_query)?;
46    }
47    if let Some(value) = &q_order {
48        url.push('&');
49        ::querylizer::Form::extend(&mut url, "order", value, false, &::querylizer::encode_query)?;
50    }
51    if let Some(value) = &q_per_page {
52        url.push('&');
53        ::querylizer::Form::extend(&mut url, "per_page", value, false, &::querylizer::encode_query)?;
54    }
55    if let Some(value) = &q_page {
56        url.push('&');
57        ::querylizer::Form::extend(&mut url, "page", value, false, &::querylizer::encode_query)?;
58    }
59    Ok(url)
60}
61
62#[cfg(feature = "hyper")]
63#[allow(clippy::too_many_arguments)]
64pub fn http_builder(
65    base_url: &str,
66    q_q: &str,
67    q_sort: ::std::option::Option<&str>,
68    q_order: ::std::option::Option<&str>,
69    q_per_page: ::std::option::Option<i64>,
70    q_page: ::std::option::Option<i64>,
71    h_user_agent: &str,
72    h_accept: ::std::option::Option<&str>,
73) -> Result<::http::request::Builder, crate::v1_1_4::ApiError> {
74    let url = url_string(
75        base_url,
76        q_q,
77        q_sort,
78        q_order,
79        q_per_page,
80        q_page,
81    )?;
82    let mut builder = ::http::request::Request::get(url);
83    builder = builder.header(
84        "User-Agent",
85        &::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?
86    );
87    if let Some(value) = &h_accept {
88        builder = builder.header(
89            "Accept",
90            &::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?
91        );
92    }
93    Ok(builder)
94}
95
96#[cfg(feature = "hyper")]
97#[inline]
98pub fn hyper_request(
99    builder: ::http::request::Builder,
100) -> Result<::http::request::Request<::hyper::Body>, crate::v1_1_4::ApiError> {
101    Ok(builder.body(::hyper::Body::empty())?)
102}
103
104#[cfg(feature = "reqwest")]
105#[allow(clippy::too_many_arguments)]
106pub fn reqwest_builder(
107    base_url: &str,
108    q_q: &str,
109    q_sort: ::std::option::Option<&str>,
110    q_order: ::std::option::Option<&str>,
111    q_per_page: ::std::option::Option<i64>,
112    q_page: ::std::option::Option<i64>,
113    h_user_agent: &str,
114    h_accept: ::std::option::Option<&str>,
115) -> Result<::reqwest::Request, crate::v1_1_4::ApiError> {
116    let url = url_string(
117        base_url,
118        q_q,
119        q_sort,
120        q_order,
121        q_per_page,
122        q_page,
123    )?;
124    let reqwest_url = ::reqwest::Url::parse(&url)?;
125    let mut request = ::reqwest::Request::new(::reqwest::Method::GET, reqwest_url);
126    let headers = request.headers_mut();
127    headers.append(
128        "User-Agent",
129        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
130    );
131    if let Some(value) = &h_accept {
132        headers.append(
133            "Accept",
134            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
135        );
136    }
137    Ok(request)
138}
139
140#[cfg(feature = "reqwest")]
141#[inline(always)]
142pub fn reqwest_request(
143    builder: ::reqwest::Request,
144) -> Result<::reqwest::Request, crate::v1_1_4::ApiError>
145{
146    Ok(builder)
147}
148
149#[cfg(feature = "reqwest-blocking")]
150#[allow(clippy::too_many_arguments)]
151pub fn reqwest_blocking_builder(
152    base_url: &str,
153    q_q: &str,
154    q_sort: ::std::option::Option<&str>,
155    q_order: ::std::option::Option<&str>,
156    q_per_page: ::std::option::Option<i64>,
157    q_page: ::std::option::Option<i64>,
158    h_user_agent: &str,
159    h_accept: ::std::option::Option<&str>,
160) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError> {
161    let url = url_string(
162        base_url,
163        q_q,
164        q_sort,
165        q_order,
166        q_per_page,
167        q_page,
168    )?;
169    let reqwest_url = ::reqwest::Url::parse(&url)?;
170    let mut request = ::reqwest::blocking::Request::new(::reqwest::Method::GET, reqwest_url);
171    let headers = request.headers_mut();
172    headers.append(
173        "User-Agent",
174        ::querylizer::Simple::to_string(&h_user_agent, false, &::querylizer::passthrough)?.try_into()?
175    );
176    if let Some(value) = &h_accept {
177        headers.append(
178            "Accept",
179            ::querylizer::Simple::to_string(value, false, &::querylizer::passthrough)?.try_into()?
180        );
181    }
182    Ok(request)
183}
184
185#[cfg(feature = "reqwest-blocking")]
186#[inline(always)]
187pub fn reqwest_blocking_request(
188    builder: ::reqwest::blocking::Request,
189) -> Result<::reqwest::blocking::Request, crate::v1_1_4::ApiError>
190{
191    Ok(builder)
192}