Skip to content

Commit

Permalink
feat chaotic: work on openapi
Browse files Browse the repository at this point in the history
commit_hash:f6118b107181229f425f06f173fad4d7fd74e2c1
  • Loading branch information
segoon committed Dec 20, 2024
1 parent 0cea353 commit 6a47139
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def make_env() -> jinja2.Environment:

def render(spec: types.ClientSpec, context: Context) -> List[CppOutput]:
env = {
'spec': spec,
'namespace': spec.cpp_namespace,
'name': spec.client_name,
'base_url': 'http://example.com', # TODO
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ void SerializeRequest(const Request& request, USERVER_NAMESPACE::clients::http::
// http_request.data(ToJsonString(request.body));
http_request.data(ToString(USERVER_NAMESPACE::formats::json::ValueBuilder(request.body).ExtractValue()));
{% elif len(op.request_bodies) > 1 %}
{# TODO #}
{%- for body in op.request_bodies -%}
switch (request.body.index()) {
{%- for num, body in enumerate(op.request_bodies) -%}
case {{ num }}:
http_request.headers({kContentType, "{{ body.content_type }}");
{% if body.content_type == 'application/json' %}
// http_request.data(ToJsonString(std::get<{{ num }}>(request.body)));
http_request.data(ToString(USERVER_NAMESPACE::formats::json::ValueBuilder(std::get<{{ num }}>(request.body)).ExtractValue()));
{% else %}
http_request.data(std::get<{{ num }}>(request.body).data);
{% endif %}
{% endfor %}
UASSERT(false);
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#pragma once

#include <string>
{# TODO: variant #}
{% if spec.has_multiple_content_type_request() %}
#include <variant>
{% endif %}

#include <userver/clients/http/request.hpp>
{% for include in spec.requests_declaration_includes() %}
#include <{{ include }}>
{% endfor %}

{# TODO: request_bodies includes #}

namespace {{ namespace }} {

Expand All @@ -16,9 +20,14 @@ namespace {{ namespace }} {
{% if len(op.request_bodies) == 1 %}
using Body = {{ op.request_bodies[0].schema.cpp_user_name() }};
{% elif len(op.request_bodies) > 1 %}
{%- for body in op.request_bodies -%}
struct {{ op.request_bodies[0].cpp_body_type() }} {
std::string data;
};
{% endfor %}
using Body = std::variant<
{%- for body in op.request_bodies -%}
{{ op.request_bodies[0].schema.cpp_user_name() }}
{{ op.request_bodies[0].cpp_body_type() }}
{%- if not loop.last %},{% endif %}
{% endfor %}
>
Expand Down
28 changes: 28 additions & 0 deletions chaotic-openapi/chaotic_openapi/back/cpp_client/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,27 @@ class Parameter:
cpp_type: str
parser: str

def declaration_includes(self) -> str:
# TODO
return []


@dataclasses.dataclass
class RequestBody:
content_type: str
schema: Optional[cpp_types.CppType]

def cpp_body_type(self) -> str:
name = cpp_names.camel_case(
cpp_names.cpp_identifier(self.content_type),
)
return f'Body{name}'


@dataclasses.dataclass
class Response:
status: int
# TODO

def is_error(self) -> bool:
return self.status >= 400
Expand Down Expand Up @@ -62,3 +73,20 @@ class ClientSpec:
client_name: str
cpp_namespace: str
operations: List[Operation]

def has_multiple_content_type_request(self) -> bool:
for op in self.operations:
if len(op.request_bodies) > 1:
return True
return False

def requests_declaration_includes(self) -> List[str]:
includes = set()
for op in self.operations:
# TODO: for body in op.request_bodies:
# TODO: if body.schema:
# TODO: includes.update(body.schema.declaration_includes())
for param in op.parameters:
includes.update(param.declaration_includes())

return list(includes)

0 comments on commit 6a47139

Please sign in to comment.