pub struct ApiClient { /* private fields */ }Expand description
API client, used to query CDF.
Implementations§
Source§impl ApiClient
impl ApiClient
Sourcepub fn new(
api_base_url: &str,
app_name: &str,
client: ClientWithMiddleware,
) -> ApiClient
pub fn new( api_base_url: &str, app_name: &str, client: ClientWithMiddleware, ) -> ApiClient
Create a new api client.
§Arguments
api_base_url- Base URL for CDF. For examplehttps://api.cognitedata.comapp_name- App name added to thex-cdp-appheader.client- Underlying reqwest client.
Sourcepub fn clone_with_api_version(&self, api_version: &str) -> ApiClient
pub fn clone_with_api_version(&self, api_version: &str) -> ApiClient
Create a new api client with a custom API version.
This will set the cdf-version header to the given value.
§Arguments
api_version- API version to use.
Sourcepub async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T>
pub async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T>
Perform a get request to the given path, deserializing the result from JSON.
§Arguments
path- Request path, without leading slash.
Sourcepub async fn get_with_params<T: DeserializeOwned, R: IntoParams>(
&self,
path: &str,
params: Option<R>,
) -> Result<T>
pub async fn get_with_params<T: DeserializeOwned, R: IntoParams>( &self, path: &str, params: Option<R>, ) -> Result<T>
Perform a get request to the given path, with a query given by params,
then deserialize the result from JSON.
§Arguments
path- Request path, without leading slash.params- Optional object converted to query parameters.
Sourcepub async fn get_stream(
&self,
url: &str,
) -> Result<impl TryStream<Ok = Bytes, Error = Error>>
pub async fn get_stream( &self, url: &str, ) -> Result<impl TryStream<Ok = Bytes, Error = Error>>
Perform a get request to the given URL, returning a stream.
§Arguments
url- Full URL to get stream from.
Sourcepub async fn post<D, S>(&self, path: &str, object: &S) -> Result<D>where
D: DeserializeOwned,
S: Serialize,
pub async fn post<D, S>(&self, path: &str, object: &S) -> Result<D>where
D: DeserializeOwned,
S: Serialize,
Perform a post request to the given path, serializing object to JSON and sending it
as the body, then deserialize the response from JSON.
§Arguments
path- Request path, without leading slash.object- Object converted to JSON body.
Sourcepub fn get_request(&self, path: &str) -> RequestBuilder<'_, ()>
pub fn get_request(&self, path: &str) -> RequestBuilder<'_, ()>
Create a request builder for a GET request to path.
Sourcepub fn post_request(&self, path: &str) -> RequestBuilder<'_, ()>
pub fn post_request(&self, path: &str) -> RequestBuilder<'_, ()>
Create a request builder for a POST request to path.
Sourcepub fn put_request(&self, path: &str) -> RequestBuilder<'_, ()>
pub fn put_request(&self, path: &str) -> RequestBuilder<'_, ()>
Create a request builder for a PUT request to path.
Sourcepub fn delete_request(&self, path: &str) -> RequestBuilder<'_, ()>
pub fn delete_request(&self, path: &str) -> RequestBuilder<'_, ()>
Create a request builder for a Delete request to path.
Sourcepub async fn post_with_query<D: DeserializeOwned, S: Serialize, R: IntoParams>(
&self,
path: &str,
object: &S,
params: Option<R>,
) -> Result<D>
pub async fn post_with_query<D: DeserializeOwned, S: Serialize, R: IntoParams>( &self, path: &str, object: &S, params: Option<R>, ) -> Result<D>
Perform a post request to the given path, with query parameters given by params.
Deserialize the response from JSON.
path- Request path, without leading slash.object- Object converted to JSON body.params- Object converted to query parameters.
Sourcepub async fn post_protobuf<D: DeserializeOwned + Send + Sync, T: Message>(
&self,
path: &str,
value: &T,
) -> Result<D>
pub async fn post_protobuf<D: DeserializeOwned + Send + Sync, T: Message>( &self, path: &str, value: &T, ) -> Result<D>
Perform a post request to the given path, posting value as protobuf.
Expects JSON as response.
§Arguments
path- Request path without leading slash.value- Protobuf value to post.
Sourcepub async fn post_expect_protobuf<D: Message + Default, S: Serialize>(
&self,
path: &str,
object: &S,
) -> Result<D>
pub async fn post_expect_protobuf<D: Message + Default, S: Serialize>( &self, path: &str, object: &S, ) -> Result<D>
Perform a post request to the given path, send object as JSON in the body,
then expect protobuf as response.
§Arguments
path- Request path without leading slash.object- Object to convert to JSON and post.
Sourcepub async fn put_blob(
&self,
url: &str,
mime_type: &str,
data: impl Into<Bytes>,
) -> Result<()>
pub async fn put_blob( &self, url: &str, mime_type: &str, data: impl Into<Bytes>, ) -> Result<()>
Perform a put request with the data in data.
§Arguments
url- URL to stream blob to.mime_type- What to put in theX-Upload_Content-Typeheader.data- Data to upload.
Sourcepub async fn put_stream<S>(
&self,
url: &str,
mime_type: &str,
stream: S,
stream_chunked: bool,
known_size: Option<u64>,
) -> Result<()>
pub async fn put_stream<S>( &self, url: &str, mime_type: &str, stream: S, stream_chunked: bool, known_size: Option<u64>, ) -> Result<()>
Perform a put request, streaming data to url.
§Arguments
url- URL to stream blob to.mime_type- What to put in theX-Upload_Content-Typeheader.stream- Stream to upload.stream_chunked- Iftrue, use chunked streaming to upload the data.known_size- Set theContent-Lengthheader to this value.
If known_size is None and stream_chunked is true, the request will be uploaded using
special chunked streaming logic. Some backends do not support this.
If stream_chunked is true and known_size is Some, this will include a content length header,
it is highly recommended to set this whenever possible.
§Warning
If stream_chunked is false, this will collect the input stream into a memory, which can
be very expensive.
Sourcepub async fn put<D, S>(&self, path: &str, object: &S) -> Result<D>where
D: DeserializeOwned,
S: Serialize,
pub async fn put<D, S>(&self, path: &str, object: &S) -> Result<D>where
D: DeserializeOwned,
S: Serialize,
Perform a put request to path with object as JSON, expecting JSON in return.
§Arguments
path- Request path without leading slash.object- Object to send as JSON.
Sourcepub async fn delete<T: DeserializeOwned>(&self, path: &str) -> Result<T>
pub async fn delete<T: DeserializeOwned>(&self, path: &str) -> Result<T>
Perform a delete request to path, expecting JSON as response.
§Arguments
path- Request path without leading slash.
Sourcepub async fn delete_with_params<T: DeserializeOwned, R: IntoParams>(
&self,
path: &str,
params: Option<R>,
) -> Result<T>
pub async fn delete_with_params<T: DeserializeOwned, R: IntoParams>( &self, path: &str, params: Option<R>, ) -> Result<T>
Perform a delete request to path, with query parameters given by params.
§Arguments
path- Request path without leading slash.params- Object converted to query parameters.
Sourcepub async fn send_request(
&self,
request_builder: RequestBuilder,
) -> Result<Response>
pub async fn send_request( &self, request_builder: RequestBuilder, ) -> Result<Response>
Send an arbitrary HTTP request using the client. This will not parse the response, but will append authentication headers and retry with the same semantics as any normal API call.
§Arguments
request_builder- Request to send.
Sourcepub fn client(&self) -> &ClientWithMiddleware
pub fn client(&self) -> &ClientWithMiddleware
Get the inner HTTP client.
Sourcepub fn api_base_url(&self) -> &str
pub fn api_base_url(&self) -> &str
Get the configured API base URL.
Sourcepub fn api_version(&self) -> Option<&str>
pub fn api_version(&self) -> Option<&str>
Get the CDF API version this client will use. Any requests made with the request builder will append a header cdf-version with this value, if it is set.