Make the 'core.network' module private

This commit is contained in:
Temirkhan Myrzamadi 2019-09-03 20:24:52 +06:00
parent 0a6dc12f92
commit e9ebd62993
5 changed files with 33 additions and 32 deletions

View file

@ -1,3 +1,3 @@
pub mod network; mod network;
pub mod requests; pub mod requests;
pub mod types; pub mod types;

View file

@ -3,9 +3,9 @@ use serde::de::DeserializeOwned;
use serde_json::Value; use serde_json::Value;
use reqwest::{ use reqwest::{
r#async::{Client, multipart::Form}, r#async::{Client, multipart::Form},
StatusCode,
}; };
use apply::Apply; use apply::Apply;
use crate::core::requests::{RequestError, ResponseResult};
const TELEGRAM_API_URL: &str = "https://api.telegram.org"; const TELEGRAM_API_URL: &str = "https://api.telegram.org";
@ -31,32 +31,6 @@ fn file_url(base: &str, token: &str, file_path: &str) -> String {
) )
} }
#[derive(Debug, Display)]
pub enum RequestError {
#[display(fmt = "Telegram error #{}: {}", status_code, description)]
ApiError {
status_code: StatusCode,
description: String,
},
#[display(fmt = "Network error: {err}", err = _0)]
NetworkError(reqwest::Error),
#[display(fmt = "InvalidJson error caused by: {err}", err = _0)]
InvalidJson(serde_json::Error),
}
impl std::error::Error for RequestError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
RequestError::ApiError { .. } => None,
RequestError::NetworkError(err) => Some(err),
RequestError::InvalidJson(err) => Some(err),
}
}
}
pub type ResponseResult<T> = Result<T, RequestError>;
pub async fn request<T: DeserializeOwned>( pub async fn request<T: DeserializeOwned>(
client: &Client, client: &Client,

View file

@ -1,12 +1,13 @@
use crate::core::{ use crate::core::{
types::User, types::User,
network::{ network::{
request, ResponseResult, request,
}, },
requests::{ requests::{
Request, RequestInfo, RequestFuture, Request, RequestInfo, RequestFuture,
} }
}; };
use crate::core::requests::ResponseResult;
#[derive(Debug, Constructor)] #[derive(Debug, Constructor)]

View file

@ -1,13 +1,38 @@
use std::future::Future; use std::future::Future;
use crate::core::network::ResponseResult;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use reqwest::r#async::Client; use reqwest::r#async::Client;
use reqwest::StatusCode;
mod form_builder; mod form_builder;
#[derive(Debug, Display)]
pub enum RequestError {
#[display(fmt = "Telegram error #{}: {}", status_code, description)]
ApiError {
status_code: StatusCode,
description: String,
},
#[display(fmt = "Network error: {err}", err = _0)]
NetworkError(reqwest::Error),
#[display(fmt = "InvalidJson error caused by: {err}", err = _0)]
InvalidJson(serde_json::Error),
}
impl std::error::Error for RequestError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
RequestError::ApiError { .. } => None,
RequestError::NetworkError(err) => Some(err),
RequestError::InvalidJson(err) => Some(err),
}
}
}
pub type ResponseResult<T> = Result<T, RequestError>;
/// Request that can be sent to telegram. /// Request that can be sent to telegram.
/// `ReturnValue` - a type that will be returned from Telegram. /// `ReturnValue` - a type that will be returned from Telegram.

View file

@ -1,7 +1,7 @@
use crate::core::{ use crate::core::{
types::Message, types::Message,
network::{ network::{
request, ResponseResult, request,
}, },
requests::{ requests::{
form_builder::FormBuilder, form_builder::FormBuilder,
@ -11,6 +11,7 @@ use crate::core::{
RequestFuture, RequestFuture,
} }
}; };
use crate::core::requests::ResponseResult;
#[derive(Debug, TypedBuilder)] #[derive(Debug, TypedBuilder)]