mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 14:35:36 +01:00
Move url creation to separate function
This commit is contained in:
parent
72be7ee750
commit
1b86af3baa
1 changed files with 52 additions and 6 deletions
|
@ -8,6 +8,24 @@ use serde_json::Value;
|
||||||
|
|
||||||
const TELEGRAM_API_URL: &str = "https://api.telegram.org";
|
const TELEGRAM_API_URL: &str = "https://api.telegram.org";
|
||||||
|
|
||||||
|
fn method_url(base: &str, token: &str, method_name: &str) -> String {
|
||||||
|
format!(
|
||||||
|
"{url}/bot{token}/{method}",
|
||||||
|
url = base,
|
||||||
|
token = token,
|
||||||
|
method = method_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file_url(base: &str, token: &str, file_path: &str) -> String {
|
||||||
|
format!(
|
||||||
|
"{url}/file/bot{token}/{file}",
|
||||||
|
url = base,
|
||||||
|
token = token,
|
||||||
|
file = file_path,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum RequestError {
|
pub enum RequestError {
|
||||||
ApiError {
|
ApiError {
|
||||||
|
@ -25,12 +43,7 @@ pub async fn request<T: DeserializeOwned, R: Request<ReturnValue = T>>(
|
||||||
request: R,
|
request: R,
|
||||||
) -> ResponseResult<T> {
|
) -> ResponseResult<T> {
|
||||||
let mut response = client
|
let mut response = client
|
||||||
.post(&format!(
|
.post(&method_url(TELEGRAM_API_URL, request.token(), request.name()))
|
||||||
"{url}/bot{token}/{method}",
|
|
||||||
url = TELEGRAM_API_URL,
|
|
||||||
token = request.token(),
|
|
||||||
method = request.name(),
|
|
||||||
))
|
|
||||||
.apply(|request_builder| {
|
.apply(|request_builder| {
|
||||||
if let Some(params) = request.params() {
|
if let Some(params) = request.params() {
|
||||||
request_builder.multipart(params)
|
request_builder.multipart(params)
|
||||||
|
@ -61,3 +74,36 @@ pub async fn request<T: DeserializeOwned, R: Request<ReturnValue = T>>(
|
||||||
Ok(serde_json::from_value(response_json["result"].clone()).unwrap())
|
Ok(serde_json::from_value(response_json["result"].clone()).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn method_url_test() {
|
||||||
|
let url = method_url(
|
||||||
|
TELEGRAM_API_URL,
|
||||||
|
"535362388:AAF7-g0gYncWnm5IyfZlpPRqRRv6kNAGlao",
|
||||||
|
"methodName"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
url,
|
||||||
|
"https://api.telegram.org/bot535362388:AAF7-g0gYncWnm5IyfZlpPRqRRv6kNAGlao/methodName"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn file_url_test() {
|
||||||
|
let url = file_url(
|
||||||
|
TELEGRAM_API_URL,
|
||||||
|
"535362388:AAF7-g0gYncWnm5IyfZlpPRqRRv6kNAGlao",
|
||||||
|
"AgADAgADyqoxG2g8aEsu_KjjVsGF4-zetw8ABAEAAwIAA20AA_8QAwABFgQ"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
url,
|
||||||
|
"https://api.telegram.org/file/bot535362388:AAF7-g0gYncWnm5IyfZlpPRqRRv6kNAGlao/AgADAgADyqoxG2g8aEsu_KjjVsGF4-zetw8ABAEAAwIAA20AA_8QAwABFgQ"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue