Overview
This task involves adding core functionality for two key methods within a Backend abstract class:
connect: Establishes a connection to our external backend service.
make_request: Sends requests to the connected service and handles the response accordingly.
Description and outcomes
Acceptance Criteria
- Initialization
- The
Backend class constructor accepts a url_prefix argument for dynamic assignment of url prefixes during instantiation.
- A
session variable of type SessionWithMaxConnectionAge is declared within the Backend class constructor.
- The
BackendResponse class constructor accepts the kwargs argument.
- A private
_construct_full_url method in the Backend class constructs the full url by combining base_url, url_prefix, and path.
- Connect to the backend:
- The
connect method uses the session variable and _construct_full_url to attempt a connection to the backend using the connect_endpoint.
- Appropriate error handling is implemented based on the supported exceptions.
- Make requests
- The
make_request method uses the session variable and _construct_full_url to make requests to the backend using the specified request object.
- The method returns a
BackendResponse object constructed from the JSON response of the session.request() call.
- Appropriate error handling is implemented based on the supported exceptions.
- The
BackendResponse class constructor accepts keyword arguments and dynamically sets corresponding attributes.
- Tests are written to verify correctness of added code
Assumptions and Dependencies
body from the request object is of type JSON, thus should be handled appropriately.
- Whereas some variable and method names already exist in the code, others are presented as suggestions. Feel free to adapt these names as necessary, for clarity.
Scope
The scope of this task is limited to;
- Implementing the core functionality of the
connect and make_request methods
Accessibility Requirements
NA
Resources
Overview
This task involves adding core functionality for two key methods within a Backend abstract class:
connect: Establishes a connection to our external backend service.make_request: Sends requests to the connected service and handles the response accordingly.Description and outcomes
Locate the
BackendandBackendResponseclasses. They reside incontentcuration/automation/utils/appnexus/base.py.Define variables:
base_url: Store the specific backend instance's base url.connect_endpoint: Use this path to check backend availability.Sessions
BackendclassSessionWithMaxConnectionAgethat extendsrequests.Sessionincontentcuration/automation/utils/appnexus/base.pyageargument for dynamic assignment of the maximum connection age. It has a default time of10s.__init__, declare and initialize a variablelast_usedwith the current time, to keep track of when the session was last used.request()and implement the logic below;current_time - last_used > age, close any open connections and reinitialize sessionlast_usedvariable to the current time.super().request(...)Initialization
Backendurl_prefixargument for dynamic assignment of url prefixes during instantiation(eg, version)__init__, declare and initialize asessionvariable (of typeSessionWithMaxConnectionAge()) that will be used to make requests.BackendResponsekwargs__init__, dynamically set the class attributes usingkwargsConstruct full path
_construct_full_url.base_url,url_prefix, andpathin that order, removing any trailing slashes beforehand.Connect to the backend
connectmethod using the definedsessionvariable, utilizing both theconnect_endpointand_construct_full_urlas needed.connectmethod as inspiration.sessionwhenRequestException, orConnectionErrorare initially encountered and retry the request, else raise an exception.Make requests
make_requestmethod should use thesession,connect_endpoint,_construct_full_url, and the providedrequestobject to make requests to the backend.requestobject carries information likepath,method,headers,params, andbody, essential for invokingsession.request().BackendResponseobject, which is then returned.requestmethod as inspiration.sessionwhenRequestException, orConnectionErrorare initially encountered and retry the request, else raise an exception.Error handling
requestspackage raises these exceptions, that can be categorized as below;RequestExceptionConnectionErrorTimeoutHTTPErrorTooManyRedirectsURLRequiredJSONDecodeErrorcontentcuration/automation/utils/appnexus/base.pyAcceptance Criteria
Backendclass constructor accepts aurl_prefixargument for dynamic assignment of url prefixes during instantiation.sessionvariable of typeSessionWithMaxConnectionAgeis declared within theBackendclass constructor.BackendResponseclass constructor accepts thekwargsargument._construct_full_urlmethod in theBackendclass constructs the full url by combiningbase_url,url_prefix, andpath.connectmethod uses thesessionvariable and_construct_full_urlto attempt a connection to the backend using theconnect_endpoint.make_requestmethod uses thesessionvariable and_construct_full_urlto make requests to the backend using the specifiedrequestobject.BackendResponseobject constructed from the JSON response of thesession.request()call.BackendResponseclass constructor accepts keyword arguments and dynamically sets corresponding attributes.Assumptions and Dependencies
bodyfrom therequestobject is of typeJSON, thus should be handled appropriately.Scope
The scope of this task is limited to;
connectandmake_requestmethodsAccessibility Requirements
NA
Resources
requestsNetworkClientclass