1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! 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()),
}
}
}
}