Module xmodel_rest.session
Expand source code
import requests
from xinject import DependencyPerThread
class Session(DependencyPerThread, attributes_to_skip_while_copying=['_requests_session']):
""" Simple resource to keep track of a common requests session.
For unit-tests, if they use the `xynlib.fixtures.context` fixture, they will get
a blank-context, and so a new `Session` object will be created each time;
there-by creating a new requests-session.
This is important, since when you mock requests library, you must create your
session object that you want to use for mock library while unit test is running.
But at the same time, we want the application to really share a session when using the
requests library so existing http connections can be reused (ie: keep connections open).
This helps facilitate using the requests mocking library while at the same time
preserving the ability to reused requests library connections like you would normally
want to do.
In normal situations, this resource will be kept around and re-used when a requests
session is needed. Therefore, requests is given an opportunity to reuse an
already open http-connection to an API.
"""
# So we know if we lazily created the session yet or not.
_requests_session = None
def reset(self):
""" Next time we are asked for the current requests Session, we will generate a new one.
"""
self._requests_session = None
@property
def requests_session(self) -> requests.Session:
session = self._requests_session
if not session:
session = requests.session()
self._requests_session = session
return session
Classes
class Session-
Simple resource to keep track of a common requests session. For unit-tests, if they use the
xynlib.fixtures.contextfixture, they will get a blank-context, and so a newSessionobject will be created each time; there-by creating a new requests-session.This is important, since when you mock requests library, you must create your session object that you want to use for mock library while unit test is running.
But at the same time, we want the application to really share a session when using the requests library so existing http connections can be reused (ie: keep connections open).
This helps facilitate using the requests mocking library while at the same time preserving the ability to reused requests library connections like you would normally want to do.
In normal situations, this resource will be kept around and re-used when a requests session is needed. Therefore, requests is given an opportunity to reuse an already open http-connection to an API.
Expand source code
class Session(DependencyPerThread, attributes_to_skip_while_copying=['_requests_session']): """ Simple resource to keep track of a common requests session. For unit-tests, if they use the `xynlib.fixtures.context` fixture, they will get a blank-context, and so a new `Session` object will be created each time; there-by creating a new requests-session. This is important, since when you mock requests library, you must create your session object that you want to use for mock library while unit test is running. But at the same time, we want the application to really share a session when using the requests library so existing http connections can be reused (ie: keep connections open). This helps facilitate using the requests mocking library while at the same time preserving the ability to reused requests library connections like you would normally want to do. In normal situations, this resource will be kept around and re-used when a requests session is needed. Therefore, requests is given an opportunity to reuse an already open http-connection to an API. """ # So we know if we lazily created the session yet or not. _requests_session = None def reset(self): """ Next time we are asked for the current requests Session, we will generate a new one. """ self._requests_session = None @property def requests_session(self) -> requests.Session: session = self._requests_session if not session: session = requests.session() self._requests_session = session return sessionAncestors
Class variables
var obj : Dependency-
Inherited from:
Dependency.objclass property/attribute that will return the current dependency for the subclass it's asked on by calling
Dependency.grab, passing no extra …
Static methods
def __init_subclass__(thread_sharable=Default, attributes_to_skip_while_copying: Optional[Iterable[str]] = Default, **kwargs)-
Inherited from:
DependencyPerThread.__init_subclass__Args
thread_sharable- If
False: While a dependency is lazily auto-created, we will ensure we do it per-thread, and not make it visible …
def grab() ‑> ~T-
Inherited from:
DependencyPerThread.grabGets a potentially shared dependency from the current
udpend.context.XContext… def proxy() ‑> ~R-
Inherited from:
DependencyPerThread.proxyReturns a proxy-object, that when and attribute is asked for, it will proxy it to the current object of
cls… def proxy_attribute(attribute_name: str) ‑> Any-
Inherited from:
DependencyPerThread.proxy_attributeReturns a proxy-object, that when and attribute is asked for, it will proxy it to the current attribute value on the current object of
cls…
Instance variables
var requests_session : requests.sessions.Session-
Expand source code
@property def requests_session(self) -> requests.Session: session = self._requests_session if not session: session = requests.session() self._requests_session = session return session
Methods
def __call__(self, func)-
Inherited from:
DependencyPerThread.__call__This makes Resource subclasses have an ability to be used as function decorators by default unless this method is overriden to provide some other …
def __copy__(self)-
Inherited from:
DependencyPerThread.__copy__Basic shallow copy protection (I am wondering if I should just remove this default copy code) …
def reset(self)-
Next time we are asked for the current requests Session, we will generate a new one.
Expand source code
def reset(self): """ Next time we are asked for the current requests Session, we will generate a new one. """ self._requests_session = None