roctogen 0.50.0

Github API and models generated from the official swagger OpenAPI specification
Documentation
//! Method, error and parameter types for the Markdown endpoint.
#![allow(
    clippy::all
)]
/* 
 * GitHub v3 REST API
 *
 * GitHub's v3 REST API.
 *
 * OpenAPI spec version: 1.1.4
 * 
 * Generated by: https://github.com/swagger-api/swagger-codegen.git
 */

use serde::Deserialize;

use roctokit::adapters::{AdapterError, Client, GitHubRequest, GitHubResponseExt};
use crate::models::*;

use super::PerPage;

use std::collections::HashMap;
use serde_json::value::Value;

pub struct Markdown<'api, C: Client> where AdapterError: From<<C as Client>::Err> {
    client: &'api C
}

pub fn new<C: Client>(client: &C) -> Markdown<C> where AdapterError: From<<C as Client>::Err> {
    Markdown { client }
}

/// Errors for the [Render a Markdown document](Markdown::render_async()) endpoint.
#[derive(Debug, thiserror::Error)]
pub enum MarkdownRenderError {
    #[error("Not modified")]
    Status304,
    #[error("Status code: {}", code)]
    Generic { code: u16 },
}

impl From<MarkdownRenderError> for AdapterError {
    fn from(err: MarkdownRenderError) -> Self {
        let (description, status_code) = match err {
            MarkdownRenderError::Status304 => (String::from("Not modified"), 304),
            MarkdownRenderError::Generic { code } => (String::from("Generic"), code)
        };

        Self::Endpoint {
            description,
            status_code,
            source: Some(Box::new(err))
        }
    }
}

/// Errors for the [Render a Markdown document in raw mode](Markdown::render_raw_async()) endpoint.
#[derive(Debug, thiserror::Error)]
pub enum MarkdownRenderRawError {
    #[error("Not modified")]
    Status304,
    #[error("Status code: {}", code)]
    Generic { code: u16 },
}

impl From<MarkdownRenderRawError> for AdapterError {
    fn from(err: MarkdownRenderRawError) -> Self {
        let (description, status_code) = match err {
            MarkdownRenderRawError::Status304 => (String::from("Not modified"), 304),
            MarkdownRenderRawError::Generic { code } => (String::from("Generic"), code)
        };

        Self::Endpoint {
            description,
            status_code,
            source: Some(Box::new(err))
        }
    }
}



impl<'api, C: Client> Markdown<'api, C> where AdapterError: From<<C as Client>::Err> {
    /// ---
    ///
    /// # Render a Markdown document
    ///
    /// Depending on what is rendered in the Markdown, you may need to provide additional token scopes for labels, such as `issues:read` or `pull_requests:read`.
    ///
    /// [GitHub API docs for render](https://docs.github.com/rest/markdown/markdown#render-a-markdown-document)
    ///
    /// ---
    pub async fn render_async(&self, body: PostMarkdownRender) -> Result<String, AdapterError> {

        let request_uri = format!("{}/markdown", super::GITHUB_BASE_API_URL);


        let req = GitHubRequest {
            uri: request_uri,
            body: Some(C::from_json::<PostMarkdownRender>(body)?),
            method: "POST",
            headers: vec![]
        };

        let request = self.client.build(req)?;

        // --

        let github_response = self.client.fetch_async(request).await?;

        // --

        if github_response.is_success() {
            Ok(github_response.to_json_async().await?)
        } else {
            match github_response.status_code() {
                304 => Err(MarkdownRenderError::Status304.into()),
                code => Err(MarkdownRenderError::Generic { code }.into()),
            }
        }
    }

    /// ---
    ///
    /// # Render a Markdown document
    ///
    /// Depending on what is rendered in the Markdown, you may need to provide additional token scopes for labels, such as `issues:read` or `pull_requests:read`.
    ///
    /// [GitHub API docs for render](https://docs.github.com/rest/markdown/markdown#render-a-markdown-document)
    ///
    /// ---
    #[cfg(not(target_arch = "wasm32"))]
    pub fn render(&self, body: PostMarkdownRender) -> Result<String, AdapterError> {

        let request_uri = format!("{}/markdown", super::GITHUB_BASE_API_URL);


        let req = GitHubRequest {
            uri: request_uri,
            body: Some(C::from_json::<PostMarkdownRender>(body)?),
            method: "POST",
            headers: vec![]
        };

        let request = self.client.build(req)?;

        // --

        let github_response = self.client.fetch(request)?;

        // --

        if github_response.is_success() {
            Ok(github_response.to_json()?)
        } else {
            match github_response.status_code() {
                304 => Err(MarkdownRenderError::Status304.into()),
                code => Err(MarkdownRenderError::Generic { code }.into()),
            }
        }
    }

    /// ---
    ///
    /// # Render a Markdown document in raw mode
    ///
    /// You must send Markdown as plain text (using a `Content-Type` header of `text/plain` or `text/x-markdown`) to this endpoint, rather than using JSON format. In raw mode, [GitHub Flavored Markdown](https://github.github.com/gfm/) is not supported and Markdown will be rendered in plain format like a README.md file. Markdown content must be 400 KB or less.
    ///
    /// [GitHub API docs for render_raw](https://docs.github.com/rest/markdown/markdown#render-a-markdown-document-in-raw-mode)
    ///
    /// ---
    pub async fn render_raw_async(&self, body: String) -> Result<String, AdapterError> {

        let request_uri = format!("{}/markdown/raw", super::GITHUB_BASE_API_URL);


        let req = GitHubRequest {
            uri: request_uri,
            body: Some(C::from_json::<String>(body)?),
            method: "POST",
            headers: vec![]
        };

        let request = self.client.build(req)?;

        // --

        let github_response = self.client.fetch_async(request).await?;

        // --

        if github_response.is_success() {
            Ok(github_response.to_json_async().await?)
        } else {
            match github_response.status_code() {
                304 => Err(MarkdownRenderRawError::Status304.into()),
                code => Err(MarkdownRenderRawError::Generic { code }.into()),
            }
        }
    }

    /// ---
    ///
    /// # Render a Markdown document in raw mode
    ///
    /// You must send Markdown as plain text (using a `Content-Type` header of `text/plain` or `text/x-markdown`) to this endpoint, rather than using JSON format. In raw mode, [GitHub Flavored Markdown](https://github.github.com/gfm/) is not supported and Markdown will be rendered in plain format like a README.md file. Markdown content must be 400 KB or less.
    ///
    /// [GitHub API docs for render_raw](https://docs.github.com/rest/markdown/markdown#render-a-markdown-document-in-raw-mode)
    ///
    /// ---
    #[cfg(not(target_arch = "wasm32"))]
    pub fn render_raw(&self, body: String) -> Result<String, AdapterError> {

        let request_uri = format!("{}/markdown/raw", super::GITHUB_BASE_API_URL);


        let req = GitHubRequest {
            uri: request_uri,
            body: Some(C::from_json::<String>(body)?),
            method: "POST",
            headers: vec![]
        };

        let request = self.client.build(req)?;

        // --

        let github_response = self.client.fetch(request)?;

        // --

        if github_response.is_success() {
            Ok(github_response.to_json()?)
        } else {
            match github_response.status_code() {
                304 => Err(MarkdownRenderRawError::Status304.into()),
                code => Err(MarkdownRenderRawError::Generic { code }.into()),
            }
        }
    }

}