use crate::types::*;
pub struct OperationBuilder {
operation: Operation,
}
impl OperationBuilder {
pub fn new() -> Self {
Self {
operation: Operation::new(Responses::new()),
}
}
pub fn summary(mut self, summary: impl Into<String>) -> Self {
self.operation.summary = Some(summary.into());
self
}
pub fn description(mut self, description: impl Into<String>) -> Self {
self.operation.description = Some(description.into());
self
}
pub fn operation_id(mut self, operation_id: impl Into<String>) -> Self {
self.operation.operation_id = Some(operation_id.into());
self
}
pub fn tag(mut self, tag: impl Into<String>) -> Self {
self.operation
.tags
.get_or_insert_with(Vec::new)
.push(tag.into());
self
}
pub fn tags(mut self, tags: Vec<String>) -> Self {
self.operation.tags = Some(tags);
self
}
pub fn parameter(mut self, parameter: Referenceable<Parameter>) -> Self {
self.operation
.parameters
.get_or_insert_with(Vec::new)
.push(parameter);
self
}
pub fn parameters(mut self, parameters: Vec<Referenceable<Parameter>>) -> Self {
self.operation.parameters = Some(parameters);
self
}
pub fn request_body(mut self, request_body: Referenceable<RequestBody>) -> Self {
self.operation.request_body = Some(request_body);
self
}
pub fn response(
mut self,
status: impl Into<String>,
response: Referenceable<Response>,
) -> Self {
self.operation
.responses
.data
.insert(status.into(), response);
self
}
pub fn default_response(mut self, response: Referenceable<Response>) -> Self {
self.operation.responses.default = Some(response);
self
}
pub fn deprecated(mut self) -> Self {
self.operation.deprecated = Some(true);
self
}
pub fn build(self) -> Operation {
self.operation
}
}
impl Default for OperationBuilder {
fn default() -> Self {
Self::new()
}
}
pub fn api(title: impl Into<String>, version: impl Into<String>) -> OpenAPIV3 {
OpenAPIV3::new(Info::new(title, version))
}
pub fn operation() -> OperationBuilder {
OperationBuilder::new()
}
pub fn get(summary: impl Into<String>) -> OperationBuilder {
OperationBuilder::new()
.summary(summary)
.response("200", Referenceable::ok("Success"))
}
pub fn post(summary: impl Into<String>) -> OperationBuilder {
OperationBuilder::new()
.summary(summary)
.response("201", Referenceable::ok("Created"))
.response("400", Referenceable::error("Bad Request"))
}
pub fn put(summary: impl Into<String>) -> OperationBuilder {
OperationBuilder::new()
.summary(summary)
.response("200", Referenceable::ok("Updated"))
.response("404", Referenceable::error("Not Found"))
}
pub fn delete(summary: impl Into<String>) -> OperationBuilder {
OperationBuilder::new()
.summary(summary)
.response("204", Referenceable::ok("Deleted"))
.response("404", Referenceable::error("Not Found"))
}