MikeWooster / api-client

Separate the high level client implementation from the underlying CRUD.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Extended Example's import models lost

o1dcat opened this issue · comments

Refer to:https://github.com/MikeWooster/api-client

from apiclient import APIClient, endpoint, paginated, retry_request

import models lost [HeaderAuthentication,JsonResponseHandler,JsonRequestFormatter] three modles that below script used in:

client = JSONPlaceholderClient(
authentication_method=HeaderAuthentication(token="<secret_value>"),
response_handler=JsonResponseHandler,
request_formatter=JsonRequestFormatter,
)

Define endpoints, using the provided decorator.

@endpoint(base_url="https://jsonplaceholder.typicode.com")
class Endpoint:
todos = "todos"
todo = "todos/{id}"

def get_next_page(response):
return {
"limit": response["limit"],
"offset": response["offset"] + response["limit"],
}

Extend the client for your API integration.

class JSONPlaceholderClient(APIClient):

@paginated(by_query_params=get_next_page)
def get_all_todos(self) -> dict:
    return self.get(Endpoint.todos)

@retry_request
def get_todo(self, todo_id: int) -> dict:
    url = Endpoint.todo.format(id=todo_id)
    return self.get(url)

Initialize the client with the correct authentication method,

response handler and request formatter.

client = JSONPlaceholderClient(
authentication_method=HeaderAuthentication(token="<secret_value>"),
response_handler=JsonResponseHandler,
request_formatter=JsonRequestFormatter,
)

commented

Would it be possible to include a problem statement too? I'm having difficulty understanding the problem. Is this a request to update the documentation (feel free to submit a PR to do this if you want)?

Yes,the documentation need update.

from apiclient import APIClient, endpoint, paginated, retry_request,HeaderAuthentication,JsonResponseHandler,JsonRequestFormatter