zino_http::response

Struct Response

Source
pub struct Response<S: ResponseCode> { /* private fields */ }
Expand description

An HTTP response.

Implementations§

Source§

impl<S: ResponseCode> Response<S>

Source

pub fn new(code: S) -> Self

Creates a new instance.

Source

pub fn with_context<Ctx: RequestContext>(code: S, ctx: &Ctx) -> Self

Creates a new instance with the request context.

Source

pub fn context<Ctx: RequestContext>(self, ctx: &Ctx) -> Self

Provides the request context for the response.

Source

pub fn render<T: Serialize>(self, template_name: &str, data: T) -> Self

Available on crate feature view only.

Renders a template with the data and sets it as the reponse.

Source

pub fn set_code(&mut self, code: S)

Sets the response code.

Source

pub fn set_status_code(&mut self, status_code: impl Into<u16>)

Sets the status code.

Source

pub fn set_error_code(&mut self, error_code: impl Into<S::ErrorCode>)

Sets the error code.

Source

pub fn set_business_code(&mut self, business_code: impl Into<S::BusinessCode>)

Sets the bussiness code.

Source

pub fn set_instance(&mut self, instance: impl Into<SharedString>)

Sets a URI reference that identifies the specific occurrence of the problem.

Source

pub fn set_message(&mut self, message: impl Into<SharedString>)

Sets the message. If the response is not successful, it should be a human-readable explanation specific to this occurrence of the problem.

Source

pub fn set_error_message(&mut self, error: impl Into<Error>)

Sets the error message.

Source

pub fn set_data<T: Serialize>(&mut self, data: &T)

Sets the response data.

Source

pub fn set_json_data(&mut self, data: impl Into<JsonValue>)

Sets the JSON data.

Source

pub fn set_bytes_data(&mut self, data: impl Into<Bytes>)

Sets the bytes data.

Source

pub fn set_validation_data(&mut self, validation: Validation)

Sets the response data for the validation.

Source

pub fn set_data_transformer(&mut self, transformer: DataTransformer)

Sets a transformer for the response data.

Source

pub fn set_content_type(&mut self, content_type: impl Into<SharedString>)

Sets the content type.

§Note

Currently, we have built-in support for the following values:

  • application/json
  • application/jsonlines
  • application/octet-stream
  • application/problem+json
  • application/x-www-form-urlencoded
  • text/csv
  • text/html
  • text/plain
Source

pub fn set_form_response(&mut self, data: impl Into<JsonValue>)

Sets the form data as the response body.

Source

pub fn set_json_response(&mut self, data: impl Into<JsonValue>)

Sets the JSON data as the response body.

Source

pub fn set_jsonlines_response(&mut self, data: impl Into<JsonValue>)

Sets the JSON Lines data as the response body.

Source

pub fn set_csv_response(&mut self, data: impl Into<JsonValue>)

Sets the CSV data as the response body.

Source

pub fn set_text_response(&mut self, data: impl Into<String>)

Sets the plain text as the response body.

Source

pub fn set_bytes_response(&mut self, data: impl Into<Bytes>)

Sets the bytes data as the response body.

Available on crate feature cookie only.

Sends a cookie to the user agent.

Source

pub fn record_server_timing( &mut self, name: impl Into<SharedString>, description: impl Into<Option<SharedString>>, duration: impl Into<Option<Duration>>, )

Records a server timing metric entry.

Source

pub fn insert_header( &mut self, name: impl Into<SharedString>, value: impl ToString, )

Inserts a custom header.

Source

pub fn get_header(&self, name: &str) -> Option<&str>

Gets a custome header with the given name.

Source

pub fn status_code(&self) -> u16

Returns the status code as u16.

Source

pub fn error_code(&self) -> Option<&S::ErrorCode>

Returns the error code.

Source

pub fn business_code(&self) -> Option<&S::BusinessCode>

Returns the business code.

Source

pub fn is_success(&self) -> bool

Returns true if the response is successful or false otherwise.

Source

pub fn has_context(&self) -> bool

Returns true if the response has a request context.

Source

pub fn message(&self) -> Option<&str>

Returns the message.

Source

pub fn request_id(&self) -> Uuid

Returns the request ID.

Source

pub fn trace_id(&self) -> Uuid

Returns the trace ID.

Source

pub fn content_type(&self) -> &str

Returns the content type.

Source

pub fn headers(&self) -> &[(SharedString, String)]

Returns the custom headers.

Source

pub fn trace_context(&self) -> (String, String)

Returns the trace context in the form (traceparent, tracestate).

Source

pub fn server_timing(&self) -> String

Returns the server timing.

Source

pub fn read_bytes(&mut self) -> Result<Bytes, Error>

Reads the response into a byte buffer.

Source

pub fn response_time(&self) -> Duration

Gets the response time.

§Note

It should only be called when the response will finish.

Source

pub fn send_file(&mut self, file: NamedFile)

Sends a file to the client.

Source

pub fn finalize(self) -> impl Iterator<Item = (SharedString, String)>

Consumes self and returns the custom headers.

Source§

impl Response<StatusCode>

Source

pub fn ok() -> Self

Constructs a new response with status 200 OK.

Source

pub fn created() -> Self

Constructs a new response with status 201 Created.

Source

pub fn bad_request() -> Self

Constructs a new response with status 400 Bad Request.

Source

pub fn not_found() -> Self

Constructs a new response with status 404 Not Found.

Source

pub fn internal_server_error() -> Self

Constructs a new response with status 500 Internal Server Error.

Trait Implementations§

Source§

impl<S: Clone + ResponseCode> Clone for Response<S>

Source§

fn clone(&self) -> Response<S>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<S: Debug + ResponseCode> Debug for Response<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: ResponseCode> Default for Response<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Rejection> for Response<StatusCode>

Source§

fn from(rejection: Rejection) -> Self

Converts to this type from the input type.
Source§

impl<S: ResponseCode> From<Validation> for Response<S>

Source§

fn from(validation: Validation) -> Self

Converts to this type from the input type.
Source§

impl<S: ResponseCode> Serialize for Response<S>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T