pub struct HttpConnector { /* private fields */ }
Available on crate feature
connector-http
only.Expand description
A connector to HTTP services.
§Examples
ⓘ
use zino_connector::HttpConnector;
use zino_core::{error::Error, state::State, LazyLock, Map};
static AMAP_GEOCODE_CONNECTOR: LazyLock<HttpConnector> = LazyLock::new(|| {
let config = State::shared()
.get_config("amap")
.expect("the `amap` field should be a table");
let base_url = "https://restapi.amap.com/v3/geocode/geo";
connector = HttpConnector::try_new("GET", base_url)
.expect("fail to construct AMap Geocode connector")
.query("output", "JSON")
.query("key", config.get_str("key"))
.query_param("address", None)
.query_param("city", None)
.build_query()
.expect("fail to build a query template for the connector")
});
async fn get_lng_lat(city: &str, address: &str) -> Result<(f32, f32), Error> {
let params = json!({
"city": city,
"address": address,
});
let data: Map = AMAP_GEOCODE_CONNECTOR
.fetch_json(None, params.as_object())
.await?;
if let Some(Ok(postions)) = data
.pointer("/geocodes/0/location")
.and_then(|v| v.parse_array())
{
Ok((postions[0], postions[1]))
} else {
bail!("fail to parse the location");
}
}
Implementations§
Source§impl HttpConnector
impl HttpConnector
Sourcepub fn try_new(method: &str, base_url: &str) -> Result<Self, Error>
pub fn try_new(method: &str, base_url: &str) -> Result<Self, Error>
Constructs a new instance, returning an error if it fails.
Sourcepub fn try_from_config(config: &Table) -> Result<Self, Error>
pub fn try_from_config(config: &Table) -> Result<Self, Error>
Attempts to construct a new instance from the config.
Sourcepub fn query(self, key: &str, value: impl Into<JsonValue>) -> Self
pub fn query(self, key: &str, value: impl Into<JsonValue>) -> Self
Adds a key/value pair for the request query.
Sourcepub fn query_param(self, key: &str, param: Option<&str>) -> Self
pub fn query_param(self, key: &str, param: Option<&str>) -> Self
Adds a parameter for the request query.
Sourcepub fn build_query(self) -> Result<Self, Error>
pub fn build_query(self) -> Result<Self, Error>
Builds the request query.
Sourcepub fn header(self, key: &str, value: impl Into<JsonValue>) -> Self
pub fn header(self, key: &str, value: impl Into<JsonValue>) -> Self
Adds a key/value pair for the request headers.
Sourcepub fn set_json_pointer(&mut self, pointer: impl Into<String>)
pub fn set_json_pointer(&mut self, pointer: impl Into<String>)
Sets a JSON Pointer for looking up a value from the response data. It only applies when the response data is a JSON object.
Sourcepub async fn fetch(
&self,
query: Option<&str>,
params: Option<&Map>,
) -> Result<Response, Error>
pub async fn fetch( &self, query: Option<&str>, params: Option<&Map>, ) -> Result<Response, Error>
Makes an HTTP request with the given query and params.
Sourcepub async fn fetch_json<T: DeserializeOwned>(
&self,
query: Option<&str>,
params: Option<&Map>,
) -> Result<T, Error>
pub async fn fetch_json<T: DeserializeOwned>( &self, query: Option<&str>, params: Option<&Map>, ) -> Result<T, Error>
Makes an HTTP request with the given query and params, and deserializes the response body via JSON.
Trait Implementations§
Source§impl Clone for HttpConnector
impl Clone for HttpConnector
Source§fn clone(&self) -> HttpConnector
fn clone(&self) -> HttpConnector
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Connector for HttpConnector
impl Connector for HttpConnector
Source§fn try_new_data_source(config: &Table) -> Result<DataSource, Error>
fn try_new_data_source(config: &Table) -> Result<DataSource, Error>
Constructs a new data source with the configuration,
returning an error if it fails.
Source§async fn execute(
&self,
query: &str,
params: Option<&Map>,
) -> Result<Option<u64>, Error>
async fn execute( &self, query: &str, params: Option<&Map>, ) -> Result<Option<u64>, Error>
Executes the query and returns the total number of rows affected.
Source§async fn query(
&self,
query: &str,
params: Option<&Map>,
) -> Result<Vec<Record>, Error>
async fn query( &self, query: &str, params: Option<&Map>, ) -> Result<Vec<Record>, Error>
Executes the query and parses it as
Vec<Record>
.Source§async fn query_as<T: DeserializeOwned>(
&self,
query: &str,
params: Option<&Map>,
) -> Result<Vec<T>, Error>
async fn query_as<T: DeserializeOwned>( &self, query: &str, params: Option<&Map>, ) -> Result<Vec<T>, Error>
Executes the query and parses it as
Vec<T>
.Source§async fn query_one(
&self,
query: &str,
params: Option<&Map>,
) -> Result<Option<Record>, Error>
async fn query_one( &self, query: &str, params: Option<&Map>, ) -> Result<Option<Record>, Error>
Executes the query and parses it as a
Record
.Source§async fn query_one_as<T: DeserializeOwned>(
&self,
query: &str,
params: Option<&Map>,
) -> Result<Option<T>, Error>
async fn query_one_as<T: DeserializeOwned>( &self, query: &str, params: Option<&Map>, ) -> Result<Option<T>, Error>
Executes the query and parses it as an instance of type
T
.Auto Trait Implementations§
impl Freeze for HttpConnector
impl RefUnwindSafe for HttpConnector
impl Send for HttpConnector
impl Sync for HttpConnector
impl Unpin for HttpConnector
impl UnwindSafe for HttpConnector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more