Implement the core functionality for the connect
and make_request
methods of the Backend
abstract class.
#4447
Labels
Milestone
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
Backend
andBackendResponse
classes. 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
Backend
classSessionWithMaxConnectionAge
that extendsrequests.Session
incontentcuration/automation/utils/appnexus/base.py
age
argument for dynamic assignment of the maximum connection age. It has a default time of10s
.__init__
, declare and initialize a variablelast_used
with 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_used
variable to the current time.super().request(...)
Initialization
Backend
url_prefix
argument for dynamic assignment of url prefixes during instantiation(eg, version)__init__
, declare and initialize asession
variable (of typeSessionWithMaxConnectionAge()
) that will be used to make requests.BackendResponse
kwargs
__init__
, dynamically set the class attributes usingkwargs
Construct full path
_construct_full_url
.base_url
,url_prefix
, andpath
in that order, removing any trailing slashes beforehand.Connect to the backend
connect
method using the definedsession
variable, utilizing both theconnect_endpoint
and_construct_full_url
as needed.connect
method as inspiration.session
whenRequestException
, orConnectionError
are initially encountered and retry the request, else raise an exception.Make requests
make_request
method should use thesession
,connect_endpoint
,_construct_full_url
, and the providedrequest
object to make requests to the backend.request
object carries information likepath
,method
,headers
,params
, andbody
, essential for invokingsession.request()
.BackendResponse
object, which is then returned.request
method as inspiration.session
whenRequestException
, orConnectionError
are initially encountered and retry the request, else raise an exception.Error handling
requests
package raises these exceptions, that can be categorized as below;RequestException
ConnectionError
Timeout
HTTPError
TooManyRedirects
URLRequired
JSONDecodeError
contentcuration/automation/utils/appnexus/base.py
Acceptance Criteria
Backend
class constructor accepts aurl_prefix
argument for dynamic assignment of url prefixes during instantiation.session
variable of typeSessionWithMaxConnectionAge
is declared within theBackend
class constructor.BackendResponse
class constructor accepts thekwargs
argument._construct_full_url
method in theBackend
class constructs the full url by combiningbase_url
,url_prefix
, andpath
.connect
method uses thesession
variable and_construct_full_url
to attempt a connection to the backend using theconnect_endpoint
.make_request
method uses thesession
variable and_construct_full_url
to make requests to the backend using the specifiedrequest
object.BackendResponse
object constructed from the JSON response of thesession.request()
call.BackendResponse
class constructor accepts keyword arguments and dynamically sets corresponding attributes.Assumptions and Dependencies
body
from therequest
object is of typeJSON
, thus should be handled appropriately.Scope
The scope of this task is limited to;
connect
andmake_request
methodsAccessibility Requirements
NA
Resources
requests
NetworkClient
classThe text was updated successfully, but these errors were encountered: