diff --git a/README.md b/README.md index e8bd829..c38d38c 100644 --- a/README.md +++ b/README.md @@ -71,3 +71,4 @@ pets: list[Pet] | EmptyBody = await client.findPetsByStatus(status="available") ``` - Make changes - Execute `make clients-for-tests && make test-clients` +- Execute `make clients-for-examples` diff --git a/examples/petstore/client_async.py b/examples/petstore/client_async.py index 2d90a3f..0865f9b 100644 --- a/examples/petstore/client_async.py +++ b/examples/petstore/client_async.py @@ -364,8 +364,9 @@ def __init__( self, base_url: str, timeout: int = 5, + client_name: str = "", client: Optional[httpx.AsyncClient] = None, - headers: Dict[str, str] = None, + headers: Optional[Dict[str, str]] = None, tracer_integration: Optional[BaseTracerIntegration] = None, metrics_integration: Optional[BaseMetricsIntegration] = None, ): @@ -374,6 +375,7 @@ def __init__( self.headers = headers or {} self.tracer_integration = tracer_integration self.metrics_integration = metrics_integration + self.client_name = client_name @tracing async def findPetsByStatus( @@ -403,24 +405,23 @@ async def findPetsByStatus( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/pet/findByStatus") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/pet/findByStatus") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/pet/findByStatus") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/pet/findByStatus") if response.status_code == 200: return [Pet.parse_obj(item) for item in response.json()] if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -452,24 +453,23 @@ async def findPetsByTags( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/pet/findByTags") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/pet/findByTags") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/pet/findByTags") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/pet/findByTags") if response.status_code == 200: return [Pet.parse_obj(item) for item in response.json()] if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -499,36 +499,34 @@ async def getPetById( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/pet/:petId") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/pet/:petId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/pet/:petId") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/pet/:petId") if response.status_code == 200: return Pet.parse_obj(response.json()) if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -557,11 +555,11 @@ async def getInventory( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/store/inventory") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/store/inventory") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/store/inventory") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/store/inventory") if response.status_code == 200: return ReturnspetinventoriesbystatusResponse200.parse_obj(response.json()) @@ -592,36 +590,34 @@ async def getOrderById( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/store/order/:orderId") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/store/order/:orderId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/store/order/:orderId") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/store/order/:orderId") if response.status_code == 200: return Order.parse_obj(response.json()) if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -656,24 +652,23 @@ async def loginUser( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/user/login") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/user/login") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/user/login") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/user/login") if response.status_code == 200: return LogsuserintothesystemResponse200(text=response.text) if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -702,11 +697,11 @@ async def logoutUser( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/user/logout") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/user/logout") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/user/logout") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/user/logout") @tracing async def getUserByName( @@ -734,36 +729,34 @@ async def getUserByName( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/user/:username") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/user/:username") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/user/:username") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/user/:username") if response.status_code == 200: return User.parse_obj(response.json()) if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -800,24 +793,23 @@ async def addPet( response = await self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/pet") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/pet") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/pet") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/pet") if response.status_code == 200: return Pet.parse_obj(response.json()) if response.status_code == 405: - client_name = "" method = "post" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -853,21 +845,20 @@ async def updatePetWithForm( response = await self.client.post(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/pet/:petId") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/pet/:petId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/pet/:petId") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/pet/:petId") if response.status_code == 405: - client_name = "" method = "post" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -908,11 +899,11 @@ async def uploadFile( response = await self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/pet/:petId/uploadImage") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/pet/:petId/uploadImage") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/pet/:petId/uploadImage") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/pet/:petId/uploadImage") if response.status_code == 200: return ApiResponse.parse_obj(response.json()) @@ -950,24 +941,23 @@ async def placeOrder( response = await self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/store/order") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/store/order") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/store/order") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/store/order") if response.status_code == 200: return Order.parse_obj(response.json()) if response.status_code == 405: - client_name = "" method = "post" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -1004,11 +994,11 @@ async def createUser( response = await self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/user") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/user") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/user") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/user") @tracing async def createUsersWithListInput( @@ -1043,11 +1033,11 @@ async def createUsersWithListInput( response = await self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/user/createWithList") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/user/createWithList") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/user/createWithList") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/user/createWithList") if response.status_code == 200: return User.parse_obj(response.json()) @@ -1085,48 +1075,45 @@ async def updatePet( response = await self.client.put(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "put", "/pet") + self.metrics_integration.on_request_error(self.client_name, exc, "put", "/pet") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "put", "/pet") + self.metrics_integration.on_request_success(self.client_name, response, "put", "/pet") if response.status_code == 200: return Pet.parse_obj(response.json()) if response.status_code == 400: - client_name = "" method = "put" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "put" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 405: - client_name = "" method = "put" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -1164,11 +1151,11 @@ async def updateUser( response = await self.client.put(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "put", "/user/:username") + self.metrics_integration.on_request_error(self.client_name, exc, "put", "/user/:username") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "put", "/user/:username") + self.metrics_integration.on_request_success(self.client_name, response, "put", "/user/:username") @tracing async def deletePet( @@ -1199,21 +1186,20 @@ async def deletePet( response = await self.client.delete(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "delete", "/pet/:petId") + self.metrics_integration.on_request_error(self.client_name, exc, "delete", "/pet/:petId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "delete", "/pet/:petId") + self.metrics_integration.on_request_success(self.client_name, response, "delete", "/pet/:petId") if response.status_code == 400: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -1243,33 +1229,31 @@ async def deleteOrder( response = await self.client.delete(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "delete", "/store/order/:orderId") + self.metrics_integration.on_request_error(self.client_name, exc, "delete", "/store/order/:orderId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "delete", "/store/order/:orderId") + self.metrics_integration.on_request_success(self.client_name, response, "delete", "/store/order/:orderId") if response.status_code == 400: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -1299,33 +1283,31 @@ async def deleteUser( response = await self.client.delete(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "delete", "/user/:username") + self.metrics_integration.on_request_error(self.client_name, exc, "delete", "/user/:username") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "delete", "/user/:username") + self.metrics_integration.on_request_success(self.client_name, response, "delete", "/user/:username") if response.status_code == 400: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) diff --git a/examples/petstore/client_sync.py b/examples/petstore/client_sync.py index 75c0ace..aeaaa99 100644 --- a/examples/petstore/client_sync.py +++ b/examples/petstore/client_sync.py @@ -364,8 +364,9 @@ def __init__( self, base_url: str, timeout: int = 5, + client_name: str = "", client: Optional[httpx.Client] = None, - headers: Dict[str, str] = None, + headers: Optional[Dict[str, str]] = None, tracer_integration: Optional[BaseTracerIntegration] = None, metrics_integration: Optional[BaseMetricsIntegration] = None, ): @@ -374,6 +375,7 @@ def __init__( self.headers = headers or {} self.tracer_integration = tracer_integration self.metrics_integration = metrics_integration + self.client_name = client_name @tracing def findPetsByStatus( @@ -403,24 +405,23 @@ def findPetsByStatus( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/pet/findByStatus") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/pet/findByStatus") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/pet/findByStatus") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/pet/findByStatus") if response.status_code == 200: return [Pet.parse_obj(item) for item in response.json()] if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -452,24 +453,23 @@ def findPetsByTags( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/pet/findByTags") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/pet/findByTags") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/pet/findByTags") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/pet/findByTags") if response.status_code == 200: return [Pet.parse_obj(item) for item in response.json()] if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -499,36 +499,34 @@ def getPetById( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/pet/:petId") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/pet/:petId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/pet/:petId") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/pet/:petId") if response.status_code == 200: return Pet.parse_obj(response.json()) if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -557,11 +555,11 @@ def getInventory( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/store/inventory") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/store/inventory") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/store/inventory") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/store/inventory") if response.status_code == 200: return ReturnspetinventoriesbystatusResponse200.parse_obj(response.json()) @@ -592,36 +590,34 @@ def getOrderById( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/store/order/:orderId") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/store/order/:orderId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/store/order/:orderId") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/store/order/:orderId") if response.status_code == 200: return Order.parse_obj(response.json()) if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -656,24 +652,23 @@ def loginUser( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/user/login") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/user/login") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/user/login") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/user/login") if response.status_code == 200: return LogsuserintothesystemResponse200(text=response.text) if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -702,11 +697,11 @@ def logoutUser( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/user/logout") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/user/logout") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/user/logout") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/user/logout") @tracing def getUserByName( @@ -734,36 +729,34 @@ def getUserByName( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/user/:username") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/user/:username") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/user/:username") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/user/:username") if response.status_code == 200: return User.parse_obj(response.json()) if response.status_code == 400: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -800,24 +793,23 @@ def addPet( response = self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/pet") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/pet") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/pet") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/pet") if response.status_code == 200: return Pet.parse_obj(response.json()) if response.status_code == 405: - client_name = "" method = "post" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -853,21 +845,20 @@ def updatePetWithForm( response = self.client.post(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/pet/:petId") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/pet/:petId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/pet/:petId") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/pet/:petId") if response.status_code == 405: - client_name = "" method = "post" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -908,11 +899,11 @@ def uploadFile( response = self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/pet/:petId/uploadImage") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/pet/:petId/uploadImage") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/pet/:petId/uploadImage") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/pet/:petId/uploadImage") if response.status_code == 200: return ApiResponse.parse_obj(response.json()) @@ -950,24 +941,23 @@ def placeOrder( response = self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/store/order") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/store/order") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/store/order") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/store/order") if response.status_code == 200: return Order.parse_obj(response.json()) if response.status_code == 405: - client_name = "" method = "post" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -1004,11 +994,11 @@ def createUser( response = self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/user") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/user") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/user") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/user") @tracing def createUsersWithListInput( @@ -1043,11 +1033,11 @@ def createUsersWithListInput( response = self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/user/createWithList") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/user/createWithList") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/user/createWithList") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/user/createWithList") if response.status_code == 200: return User.parse_obj(response.json()) @@ -1085,48 +1075,45 @@ def updatePet( response = self.client.put(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "put", "/pet") + self.metrics_integration.on_request_error(self.client_name, exc, "put", "/pet") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "put", "/pet") + self.metrics_integration.on_request_success(self.client_name, response, "put", "/pet") if response.status_code == 200: return Pet.parse_obj(response.json()) if response.status_code == 400: - client_name = "" method = "put" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "put" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 405: - client_name = "" method = "put" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -1164,11 +1151,11 @@ def updateUser( response = self.client.put(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "put", "/user/:username") + self.metrics_integration.on_request_error(self.client_name, exc, "put", "/user/:username") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "put", "/user/:username") + self.metrics_integration.on_request_success(self.client_name, response, "put", "/user/:username") @tracing def deletePet( @@ -1199,21 +1186,20 @@ def deletePet( response = self.client.delete(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "delete", "/pet/:petId") + self.metrics_integration.on_request_error(self.client_name, exc, "delete", "/pet/:petId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "delete", "/pet/:petId") + self.metrics_integration.on_request_success(self.client_name, response, "delete", "/pet/:petId") if response.status_code == 400: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -1243,33 +1229,31 @@ def deleteOrder( response = self.client.delete(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "delete", "/store/order/:orderId") + self.metrics_integration.on_request_error(self.client_name, exc, "delete", "/store/order/:orderId") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "delete", "/store/order/:orderId") + self.metrics_integration.on_request_success(self.client_name, response, "delete", "/store/order/:orderId") if response.status_code == 400: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) @@ -1299,33 +1283,31 @@ def deleteUser( response = self.client.delete(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "delete", "/user/:username") + self.metrics_integration.on_request_error(self.client_name, exc, "delete", "/user/:username") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "delete", "/user/:username") + self.metrics_integration.on_request_success(self.client_name, response, "delete", "/user/:username") if response.status_code == 400: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) if response.status_code == 404: - client_name = "" method = "delete" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return EmptyBody(status_code=response.status_code, text=response.text) diff --git a/pyproject.toml b/pyproject.toml index 3bc829c..5d0565c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pythogen" -version = "0.0.27" +version = "0.0.28" description = "Generator of python HTTP-clients from OpenApi specification." homepage = "https://github.com/artsmolin/pythogen" repository = "https://github.com/artsmolin/pythogen" diff --git a/pythogen/templates/client/httpx-method.j2 b/pythogen/templates/client/httpx-method.j2 index 10d311f..53012d9 100644 --- a/pythogen/templates/client/httpx-method.j2 +++ b/pythogen/templates/client/httpx-method.j2 @@ -111,14 +111,13 @@ if response.status_code == {{ code }}: {% if code | int >= 400 -%} - client_name = "{{ name | replace('Client', '') | lower }}" method = "{{ method }}" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) {% endif -%} return {{ mapper }} diff --git a/pythogen/templates/client/httpx-request-metrics.j2 b/pythogen/templates/client/httpx-request-metrics.j2 index 1b2df80..33fb9b9 100644 --- a/pythogen/templates/client/httpx-request-metrics.j2 +++ b/pythogen/templates/client/httpx-request-metrics.j2 @@ -7,7 +7,7 @@ {%- endif %} except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("{{ name | replace('Client', '') | lower }}", exc, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}") + self.metrics_integration.on_request_error(self.client_name, exc, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}") raise exc {# https://www.python-httpx.org/api/#response @@ -16,4 +16,4 @@ total_seconds() to correctly get the total elapsed seconds. #} if self.metrics_integration: - self.metrics_integration.on_request_success("{{ name | replace('Client', '') | lower }}", response, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}") + self.metrics_integration.on_request_success(self.client_name, response, "{{ method }}", "{{ path | replace('{', ':') | replace('}', '') }}") diff --git a/pythogen/templates/httpx.j2 b/pythogen/templates/httpx.j2 index 1a8655e..8b9d8a1 100644 --- a/pythogen/templates/httpx.j2 +++ b/pythogen/templates/httpx.j2 @@ -284,8 +284,8 @@ class {{ classname(model.id) }}(BaseModel): class BasicAuth(BaseModel): - username: str - password: str + username: str + password: str class {{ name }}: @@ -293,12 +293,13 @@ class {{ name }}: self, base_url: str, timeout: int = 5, + client_name: str = "{{ name | replace('Client', '') | lower }}", {%- if sync %} client: Optional[httpx.Client] = None, {%- else %} client: Optional[httpx.AsyncClient] = None, {%- endif %} - headers: Dict[str, str] = None, + headers: Optional[Dict[str, str]] = None, tracer_integration: Optional[BaseTracerIntegration] = None, metrics_integration: Optional[BaseMetricsIntegration] = None, ): @@ -311,6 +312,7 @@ class {{ name }}: self.headers = headers or {} self.tracer_integration = tracer_integration self.metrics_integration=metrics_integration + self.client_name = client_name {#- get items begin #} {% with items=get.items(), method='get'%} diff --git a/tests/clients/async_client.py b/tests/clients/async_client.py index 74ba07c..d10de2b 100644 --- a/tests/clients/async_client.py +++ b/tests/clients/async_client.py @@ -553,8 +553,8 @@ class AllOfResp(BaseModel): class BasicAuth(BaseModel): - username: str - password: str + username: str + password: str class Client: @@ -562,8 +562,9 @@ def __init__( self, base_url: str, timeout: int = 5, + client_name: str = "", client: Optional[httpx.AsyncClient] = None, - headers: Dict[str, str] = None, + headers: Optional[Dict[str, str]] = None, tracer_integration: Optional[BaseTracerIntegration] = None, metrics_integration: Optional[BaseMetricsIntegration] = None, ): @@ -572,6 +573,7 @@ def __init__( self.headers = headers or {} self.tracer_integration = tracer_integration self.metrics_integration=metrics_integration + self.client_name = client_name @tracing async def get_object_no_ref_schema( @@ -605,11 +607,11 @@ async def get_object_no_ref_schema( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/objects/no-ref-schema/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/objects/no-ref-schema/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/objects/no-ref-schema/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/objects/no-ref-schema/:object_id") if response.status_code == 200: return GetObjectNoRefSchemaResponse200.parse_obj(response.json()) @@ -646,24 +648,23 @@ async def get_object( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/objects/:object_id") if response.status_code == 200: return GetObjectResp.parse_obj(response.json()) if response.status_code == 500: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return UnknownError.parse_obj(response.json()) @@ -693,11 +694,11 @@ async def get_object_with_inline_array( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/object-with-array-response") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/object-with-array-response") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/object-with-array-response") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/object-with-array-response") if response.status_code == 200: return [GetObjectWithInlineArrayResponse200Item.parse_obj(item) for item in response.json()] @@ -728,11 +729,11 @@ async def get_object_with_inline_array( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/object-with-inline-array") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/object-with-inline-array") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/object-with-inline-array") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/object-with-inline-array") if response.status_code == 200: return GetObjectWithInlineArrayResponse200.parse_obj(response.json()) @@ -763,11 +764,11 @@ async def get_list_objects( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/objects") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/objects") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/objects") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/objects") if response.status_code == 200: return [GetObjectResp.parse_obj(item) for item in response.json()] @@ -798,11 +799,11 @@ async def get_text( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/text") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/text") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/text") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/text") if response.status_code == 200: return GetTextResponse200(text=response.text) @@ -833,11 +834,11 @@ async def get_empty( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/empty") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/empty") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/empty") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/empty") if response.status_code == 200: return EmptyBody(status_code=response.status_code, text=response.text) @@ -868,11 +869,11 @@ async def get_binary( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/binary") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/binary") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/binary") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/binary") if response.status_code == 200: return GetBinaryResponse200(content=response.content) @@ -903,11 +904,11 @@ async def get_allof( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/allof") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/allof") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/allof") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/allof") if response.status_code == 200: return AllOfResp.parse_obj(response.json()) @@ -942,24 +943,23 @@ async def get_object_slow( response = await self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/slow/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/slow/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/slow/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/slow/objects/:object_id") if response.status_code == 200: return GetObjectResp.parse_obj(response.json()) if response.status_code == 500: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return UnknownError.parse_obj(response.json()) @@ -989,11 +989,11 @@ async def post_object_without_body( response = await self.client.post(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/post-without-body") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/post-without-body") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/post-without-body") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/post-without-body") if response.status_code == 200: return PostObjectResp.parse_obj(response.json()) @@ -1032,11 +1032,11 @@ async def post_object( response = await self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/objects") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/objects") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/objects") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/objects") if response.status_code == 200: return PostObjectResp.parse_obj(response.json()) @@ -1076,11 +1076,11 @@ async def post_form_object( response = await self.client.post(url, data=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/objects-form-data") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/objects-form-data") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/objects-form-data") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/objects-form-data") if response.status_code == 200: return PostObjectResp.parse_obj(response.json()) @@ -1124,11 +1124,11 @@ async def post_multipart_form_data( response = await self.client.post(url, data=json, headers=headers_, params=params, auth=auth_, files=files) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/multipart-form-data") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/multipart-form-data") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/multipart-form-data") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/multipart-form-data") if response.status_code == 200: return PostObjectResp.parse_obj(response.json()) @@ -1168,11 +1168,11 @@ async def patch_object( response = await self.client.patch(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "patch", "/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "patch", "/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "patch", "/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "patch", "/objects/:object_id") if response.status_code == 200: return PatchObjectResp.parse_obj(response.json()) @@ -1212,11 +1212,11 @@ async def put_object( response = await self.client.put(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "put", "/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "put", "/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "put", "/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "put", "/objects/:object_id") if response.status_code == 200: return PutObjectResp.parse_obj(response.json()) @@ -1256,11 +1256,11 @@ async def put_object_slow( response = await self.client.put(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "put", "/slow/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "put", "/slow/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "put", "/slow/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "put", "/slow/objects/:object_id") if response.status_code == 200: return PutObjectResp.parse_obj(response.json()) @@ -1292,11 +1292,11 @@ async def delete_object( response = await self.client.delete(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "delete", "/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "delete", "/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "delete", "/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "delete", "/objects/:object_id") if response.status_code == 200: return DeleteObjectResp.parse_obj(response.json()) diff --git a/tests/clients/sync_client.py b/tests/clients/sync_client.py index 7bd15d9..bab76d5 100644 --- a/tests/clients/sync_client.py +++ b/tests/clients/sync_client.py @@ -553,8 +553,8 @@ class AllOfResp(BaseModel): class BasicAuth(BaseModel): - username: str - password: str + username: str + password: str class Client: @@ -562,8 +562,9 @@ def __init__( self, base_url: str, timeout: int = 5, + client_name: str = "", client: Optional[httpx.Client] = None, - headers: Dict[str, str] = None, + headers: Optional[Dict[str, str]] = None, tracer_integration: Optional[BaseTracerIntegration] = None, metrics_integration: Optional[BaseMetricsIntegration] = None, ): @@ -572,6 +573,7 @@ def __init__( self.headers = headers or {} self.tracer_integration = tracer_integration self.metrics_integration=metrics_integration + self.client_name = client_name @tracing def get_object_no_ref_schema( @@ -605,11 +607,11 @@ def get_object_no_ref_schema( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/objects/no-ref-schema/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/objects/no-ref-schema/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/objects/no-ref-schema/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/objects/no-ref-schema/:object_id") if response.status_code == 200: return GetObjectNoRefSchemaResponse200.parse_obj(response.json()) @@ -646,24 +648,23 @@ def get_object( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/objects/:object_id") if response.status_code == 200: return GetObjectResp.parse_obj(response.json()) if response.status_code == 500: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return UnknownError.parse_obj(response.json()) @@ -693,11 +694,11 @@ def get_object_with_inline_array( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/object-with-array-response") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/object-with-array-response") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/object-with-array-response") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/object-with-array-response") if response.status_code == 200: return [GetObjectWithInlineArrayResponse200Item.parse_obj(item) for item in response.json()] @@ -728,11 +729,11 @@ def get_object_with_inline_array( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/object-with-inline-array") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/object-with-inline-array") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/object-with-inline-array") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/object-with-inline-array") if response.status_code == 200: return GetObjectWithInlineArrayResponse200.parse_obj(response.json()) @@ -763,11 +764,11 @@ def get_list_objects( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/objects") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/objects") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/objects") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/objects") if response.status_code == 200: return [GetObjectResp.parse_obj(item) for item in response.json()] @@ -798,11 +799,11 @@ def get_text( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/text") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/text") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/text") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/text") if response.status_code == 200: return GetTextResponse200(text=response.text) @@ -833,11 +834,11 @@ def get_empty( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/empty") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/empty") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/empty") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/empty") if response.status_code == 200: return EmptyBody(status_code=response.status_code, text=response.text) @@ -868,11 +869,11 @@ def get_binary( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/binary") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/binary") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/binary") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/binary") if response.status_code == 200: return GetBinaryResponse200(content=response.content) @@ -903,11 +904,11 @@ def get_allof( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/allof") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/allof") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/allof") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/allof") if response.status_code == 200: return AllOfResp.parse_obj(response.json()) @@ -942,24 +943,23 @@ def get_object_slow( response = self.client.get(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "get", "/slow/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "get", "/slow/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "get", "/slow/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "get", "/slow/objects/:object_id") if response.status_code == 200: return GetObjectResp.parse_obj(response.json()) if response.status_code == 500: - client_name = "" method = "get" if response.content is None: content = None else: content = response.content[:500] - self.log_error(client_name, method, url, params, content, headers_) + self.log_error(self.client_name, method, url, params, content, headers_) return UnknownError.parse_obj(response.json()) @@ -989,11 +989,11 @@ def post_object_without_body( response = self.client.post(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/post-without-body") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/post-without-body") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/post-without-body") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/post-without-body") if response.status_code == 200: return PostObjectResp.parse_obj(response.json()) @@ -1032,11 +1032,11 @@ def post_object( response = self.client.post(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/objects") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/objects") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/objects") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/objects") if response.status_code == 200: return PostObjectResp.parse_obj(response.json()) @@ -1076,11 +1076,11 @@ def post_form_object( response = self.client.post(url, data=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/objects-form-data") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/objects-form-data") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/objects-form-data") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/objects-form-data") if response.status_code == 200: return PostObjectResp.parse_obj(response.json()) @@ -1124,11 +1124,11 @@ def post_multipart_form_data( response = self.client.post(url, data=json, headers=headers_, params=params, auth=auth_, files=files) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "post", "/multipart-form-data") + self.metrics_integration.on_request_error(self.client_name, exc, "post", "/multipart-form-data") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "post", "/multipart-form-data") + self.metrics_integration.on_request_success(self.client_name, response, "post", "/multipart-form-data") if response.status_code == 200: return PostObjectResp.parse_obj(response.json()) @@ -1168,11 +1168,11 @@ def patch_object( response = self.client.patch(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "patch", "/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "patch", "/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "patch", "/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "patch", "/objects/:object_id") if response.status_code == 200: return PatchObjectResp.parse_obj(response.json()) @@ -1212,11 +1212,11 @@ def put_object( response = self.client.put(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "put", "/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "put", "/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "put", "/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "put", "/objects/:object_id") if response.status_code == 200: return PutObjectResp.parse_obj(response.json()) @@ -1256,11 +1256,11 @@ def put_object_slow( response = self.client.put(url, json=json, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "put", "/slow/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "put", "/slow/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "put", "/slow/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "put", "/slow/objects/:object_id") if response.status_code == 200: return PutObjectResp.parse_obj(response.json()) @@ -1292,11 +1292,11 @@ def delete_object( response = self.client.delete(url, headers=headers_, params=params, auth=auth_) except Exception as exc: if self.metrics_integration: - self.metrics_integration.on_request_error("", exc, "delete", "/objects/:object_id") + self.metrics_integration.on_request_error(self.client_name, exc, "delete", "/objects/:object_id") raise exc if self.metrics_integration: - self.metrics_integration.on_request_success("", response, "delete", "/objects/:object_id") + self.metrics_integration.on_request_success(self.client_name, response, "delete", "/objects/:object_id") if response.status_code == 200: return DeleteObjectResp.parse_obj(response.json())