Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Option wrapper in successful responses #20

Merged
merged 6 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions .generator/src/generator/templates/api.j2
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,19 @@ impl {{ structName }} {
{% if operation.description is defined %}
{{ operation.description | block_comment }}
{%- endif %}
pub async fn {{operation.operationId | snake_case}}(&self{% for name, parameter in requiredParams %}, {{name|variable_name}}: {{ get_type_for_parameter(parameter, version) }}{% endfor %}{% if operation|has_optional_parameter %}, params: {{operation.operationId}}OptionalParams{% endif %}) -> Result<Option<{% if returnType %}{{returnType}}{% else %}(){% endif %}>, Error<{{operation.operationId}}Error>> {
pub async fn {{operation.operationId | snake_case}}(&self{% for name, parameter in requiredParams %}, {{name|variable_name}}: {{ get_type_for_parameter(parameter, version) }}{% endfor %}{% if operation|has_optional_parameter %}, params: {{operation.operationId}}OptionalParams{% endif %}) -> Result<{% if returnType %}{{returnType}}{% else %}(){% endif %}, Error<{{operation.operationId}}Error>> {
match self.{{operation.operationId | snake_case}}_with_http_info({% for name, parameter in requiredParams %}{{name|variable_name}}{% if loop.last %}{% if operation|has_optional_parameter %}, {% endif %}{% else %}, {% endif %}{% endfor %}{% if operation|has_optional_parameter %} params{% endif %}).await {
Ok(response_content) => Ok(response_content.entity),
{%- if returnType %}
Ok(response_content) => {
if let Some(e) = response_content.entity {
Ok(e)
} else {
Err(Error::Serde(serde::de::Error::custom("response content was None")))
}
},
{%- else%}
Ok(_) => Ok(()),
{%- endif%}
Err(err) => Err(err),
}
}
Expand Down Expand Up @@ -220,12 +230,14 @@ impl {{ structName }} {

if !local_status.is_client_error() && !local_status.is_server_error() {
{%- if returnType %}
let local_entity: Option<{{returnType}}> = serde_json::from_str(&local_content).ok();
Ok(ResponseContent {
status: local_status,
content: local_content,
entity: local_entity,
})
match serde_json::from_str::<{{ returnType }}>(&local_content) {
Ok(e) => return Ok(ResponseContent {
status: local_status,
content: local_content,
entity: Some(e),
}),
Err(e) => return Err(crate::datadog::Error::Serde(e)),
};
{%- else %}
Ok(ResponseContent {
status: local_status,
Expand Down
35 changes: 23 additions & 12 deletions src/datadogV1/api/api_authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ impl AuthenticationAPI {
/// Check if the API key (not the APP key) is valid. If invalid, a 403 is returned.
pub async fn validate(
&self,
) -> Result<
Option<crate::datadogV1::model::AuthenticationValidationResponse>,
Error<ValidateError>,
> {
) -> Result<crate::datadogV1::model::AuthenticationValidationResponse, Error<ValidateError>>
{
match self.validate_with_http_info().await {
Ok(response_content) => Ok(response_content.entity),
Ok(response_content) => {
if let Some(e) = response_content.entity {
Ok(e)
} else {
Err(Error::Serde(serde::de::Error::custom(
"response content was None",
)))
}
}
Err(err) => Err(err),
}
}
Expand Down Expand Up @@ -85,13 +91,18 @@ impl AuthenticationAPI {
let local_content = local_resp.text().await?;

if !local_status.is_client_error() && !local_status.is_server_error() {
let local_entity: Option<crate::datadogV1::model::AuthenticationValidationResponse> =
serde_json::from_str(&local_content).ok();
Ok(ResponseContent {
status: local_status,
content: local_content,
entity: local_entity,
})
match serde_json::from_str::<crate::datadogV1::model::AuthenticationValidationResponse>(
&local_content,
) {
Ok(e) => {
return Ok(ResponseContent {
status: local_status,
content: local_content,
entity: Some(e),
})
}
Err(e) => return Err(crate::datadog::Error::Serde(e)),
};
} else {
let local_entity: Option<ValidateError> = serde_json::from_str(&local_content).ok();
let local_error = ResponseContent {
Expand Down
Loading
Loading