22
33## Pre requisits
44
5- As already denoted in [ the quick start section] ( quick_start.md ) , the first thing
5+ As already denoted in [ the quick start section] ( ../ quick_start.md) , the first thing
66you need to do is to actually install the generator. You can do so via pip
77or any other package manager.
88
@@ -593,7 +593,7 @@ suite of the generator. It has the following structure:
593593!!! tip "OpenAPI specification"
594594
595595 Take a look at our short introduction to the
596- [OpenAPI specification](openapi-definition.md) if you need to look up
596+ [OpenAPI specification](../ openapi-definition.md) if you need to look up
597597 what the specific nodes mean, or if you just need a refresher or some
598598 links for further information.
599599
@@ -605,7 +605,7 @@ Lets run the generator on this file:
605605</div >
606606
607607This will result in the folder structure as denoted in the
608- [ quick start] ( quick_start.md ) section. Lets take a deep dive on what
608+ [ quick start] ( ../ quick_start.md) section. Lets take a deep dive on what
609609the generator created for us, starting with the models.
610610
611611## The models module
@@ -763,3 +763,108 @@ the additional two - four modules.
763763The next thing is async support: You may want (depending on your usecase)
764764bot async and sync services. The generator will create both (for __ httpx__ ),
765765only sync (for __ requests__ ) or only async (for __ aiohttp__ ) services.
766+
767+ === "async_general_service.py"
768+ ``` py
769+ ...
770+ async def async_root__ get() -> RootResponse:
771+ base_path = APIConfig.base_path
772+ path = f"/"
773+ headers = {
774+ "Content-Type": "application/json",
775+ "Accept": "application/json",
776+ "Authorization": f"Bearer { APIConfig.get_access_token() }",
777+ }
778+ query_params = {}
779+
780+ with httpx.AsyncClient(base_url=base_path) as client:
781+ response = await client.request(
782+ method="get",
783+ url=path,
784+ headers=headers,
785+ params=query_params,
786+ )
787+
788+ if response.status_code != 200:
789+ raise Exception(f" failed with status code: {response.status_code}")
790+ return RootResponse(**response.json())
791+ ...
792+ ```
793+
794+ === "general_service.py"
795+ ``` py
796+ ...
797+ def root__ get() -> RootResponse:
798+ base_path = APIConfig.base_path
799+ path = f"/"
800+ headers = {
801+ "Content-Type": "application/json",
802+ "Accept": "application/json",
803+ "Authorization": f"Bearer { APIConfig.get_access_token() }",
804+ }
805+ query_params = {}
806+
807+ with httpx.Client(base_url=base_path) as client:
808+ response = client.request(
809+ method="get",
810+ url=path,
811+ headers=headers,
812+ params=query_params,
813+ )
814+
815+ if response.status_code != 200:
816+ raise Exception(f" failed with status code: {response.status_code}")
817+ return RootResponse(**response.json())
818+ ...
819+ ```
820+
821+ While we are at the topic of looking at the individual functions, lets walk through the one above:
822+
823+ ``` py
824+ ...
825+ def root__get () -> RootResponse:
826+ ...
827+ ```
828+
829+ All functions are fully annotated with the proper types, which provides the inspection of your IDE better insight
830+ on what to provide to a given function and what to expect.
831+
832+ ``` py
833+ ...
834+ path = f " / "
835+ ...
836+ ```
837+
838+ Paths are automatically created from the specification. No need to worry about that.
839+
840+ ``` py
841+ ...
842+ headers = {
843+ " Content-Type" : " application/json" ,
844+ " Accept" : " application/json" ,
845+ " Authorization" : f " Bearer { APIConfig.get_access_token() } " ,
846+ }
847+ query_params = {}
848+ ...
849+ ```
850+
851+ Authorization token is always passed to the Rest API, you will not need to worry about differentiating between the
852+ calls. Query params are also automatically created, with the input parameters and depending on your spec (for
853+ this root call no params are necessary)
854+
855+ ``` py
856+ ...
857+ if response.status_code != 200 :
858+ raise Exception (f " failed with status code: { response.status_code} " )
859+ return RootResponse(** response.json())
860+ ...
861+ ```
862+
863+ The generator will automatically raise an exception if a non-good status code was returned by the API, for
864+ whatever reason. The "good" status code is also determined by the spec - and can be defined through your API.
865+ For a post call for example, the spec will define a 201 status code as a good status code.
866+
867+ Lastly the code will automatically type check and convert the response to the appropriate type (in this case
868+ ` RootResponse ` ). This is really neat, because without doing much in the code, it automatically validates that
869+ your API truly responds the way we expect it to respond, and gives you proper typing latter on in your code -
870+ all thanks to the magic of [ pydantic] ( https://pydantic-docs.helpmanual.io/ ) .
0 commit comments