Trait RestApiClient

Source
pub trait RestApiClient {
    // Required methods
    fn set_exit_code(
        &self,
        checks_failed: u64,
        format_checks_failed: Option<u64>,
        tidy_checks_failed: Option<u64>,
    ) -> u64;
    fn start_log_group(&self, name: String);
    fn end_log_group(&self);
    fn make_headers() -> Result<HeaderMap<HeaderValue>>;
    fn get_list_of_changed_files(
        &self,
        file_filter: &FileFilter,
    ) -> impl Future<Output = Result<Vec<FileObj>>>;
    fn get_changed_files_paginated(
        &self,
        url: Url,
        file_filter: &FileFilter,
    ) -> impl Future<Output = Result<Vec<FileObj>>>;
    fn post_feedback(
        &self,
        files: &[Arc<Mutex<FileObj>>],
        user_inputs: FeedbackInput,
        clang_versions: ClangVersions,
    ) -> impl Future<Output = Result<u64>>;

    // Provided methods
    fn make_api_request(
        client: &Client,
        url: impl IntoUrl,
        method: Method,
        data: Option<String>,
        headers: Option<HeaderMap>,
    ) -> Result<Request> { ... }
    fn send_api_request(
        client: Client,
        request: Request,
        rate_limit_headers: RestApiRateLimitHeaders,
        retries: u8,
    ) -> impl Future<Output = Result<Response>> + Send { ... }
    fn make_comment(
        &self,
        files: &[Arc<Mutex<FileObj>>],
        format_checks_failed: u64,
        tidy_checks_failed: u64,
        clang_versions: &ClangVersions,
        max_len: Option<u64>,
    ) -> String { ... }
    fn try_next_page(headers: &HeaderMap) -> Option<Url> { ... }
    fn log_response(
        response: Response,
        context: &str,
    ) -> impl Future<Output = ()> + Send { ... }
}
Expand description

A custom trait that templates necessary functionality with a Git server’s REST API.

Required Methods§

Source

fn set_exit_code( &self, checks_failed: u64, format_checks_failed: Option<u64>, tidy_checks_failed: Option<u64>, ) -> u64

A way to set output variables specific to cpp_linter executions in CI.

Source

fn start_log_group(&self, name: String)

This prints a line to indicate the beginning of a related group of log statements.

Source

fn end_log_group(&self)

This prints a line to indicate the ending of a related group of log statements.

Source

fn make_headers() -> Result<HeaderMap<HeaderValue>>

A convenience method to create the headers attached to all REST API calls.

If an authentication token is provided (via environment variable), this method shall include the relative information.

Source

fn get_list_of_changed_files( &self, file_filter: &FileFilter, ) -> impl Future<Output = Result<Vec<FileObj>>>

A way to get the list of changed files using REST API calls. It is this method’s job to parse diff blobs and return a list of changed files.

The context of the file changes are subject to the type of event in which cpp_linter package is used.

Source

fn get_changed_files_paginated( &self, url: Url, file_filter: &FileFilter, ) -> impl Future<Output = Result<Vec<FileObj>>>

A way to get the list of changed files using REST API calls that employ a paginated response.

This is a helper to RestApiClient::get_list_of_changed_files() but takes a formulated URL endpoint based on the context of the triggering CI event.

Source

fn post_feedback( &self, files: &[Arc<Mutex<FileObj>>], user_inputs: FeedbackInput, clang_versions: ClangVersions, ) -> impl Future<Output = Result<u64>>

A way to post feedback in the form of thread_comments, file_annotations, and step_summary.

The given files should’ve been gathered from get_list_of_changed_files() or list_source_files().

The format_advice and tidy_advice should be a result of parsing output from clang-format and clang-tidy (see capture_clang_tools_output()).

All other parameters correspond to CLI arguments.

Provided Methods§

Source

fn make_api_request( client: &Client, url: impl IntoUrl, method: Method, data: Option<String>, headers: Option<HeaderMap>, ) -> Result<Request>

Construct a HTTP request to be sent.

The idea here is that this method is called before RestApiClient::send_api_request().

let request = Self::make_api_request(
    &self.client,
    "https://example.com",
    Method::GET,
    None,
    None
);
let response = Self::send_api_request(
    self.client.clone(),
    request,
    self.rest_api_headers.clone(),
    0, // start recursion count at 0 (max iterations is 4)
);
match response.await {
    Some(res) => {/* handle response */}
    None => {/* handle failure */}
}
Source

fn send_api_request( client: Client, request: Request, rate_limit_headers: RestApiRateLimitHeaders, retries: u8, ) -> impl Future<Output = Result<Response>> + Send

A convenience function to send HTTP requests and respect a REST API rate limits.

This method must own all the data passed to it because asynchronous recursion is used. Recursion is needed when a secondary rate limit is hit. The server tells the client that it should back off and retry after a specified time interval.

Note, pass 0 to the retries parameter, which is used to count recursive iterations. This function will recur a maximum of 4 times, and this only happens when the response’s headers includes a retry interval.

Source

fn make_comment( &self, files: &[Arc<Mutex<FileObj>>], format_checks_failed: u64, tidy_checks_failed: u64, clang_versions: &ClangVersions, max_len: Option<u64>, ) -> String

Makes a comment in MarkDown syntax based on the concerns in format_advice and tidy_advice about the given set of files.

This method has a default definition and should not need to be redefined by implementors.

Returns the markdown comment as a string as well as the total count of format_checks_failed and tidy_checks_failed (in respective order).

Source

fn try_next_page(headers: &HeaderMap) -> Option<Url>

Gets the URL for the next page in a paginated response.

Returns None if current response is the last page.

Source

fn log_response( response: Response, context: &str, ) -> impl Future<Output = ()> + Send

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§