Module pyreddit.services.service

Abstract Base static Class for every service.

Classes

class Service

Abstract Base static Class for every service class.

Summary

This class contains all the common logic to the media retrieval process in all child services.

This consists of the get_media() method, which executes the following steps (in order):

  • URL preprocessing
  • Media information get
  • re-authentication if necessary
  • URL postprocessing and Media object creation

All the implementation logic is delegated to the child classes, since every service provider has custom logic, although the base class offers some basic implementation, which can be easily overwritten.

Notes

Authenticated services need to present an __init__ method in which the specific service authentication method is called, in order to authenticate for the first time on the Service creation.

Subclasses

Class variables

var has_external_request : bool

True if the service needs to reach out to an external http endpoint, False otherwise.

var is_authenticated : bool

True if the external request needs to be authenticated (i.e. with an Authorization header), False otherwise.

Note

This is taken into account only if has_external_request is set to True

var access_token : Union[str, NoneType]

Contains the access token for the OAuth authentication if present, None otherwise.

Note

This is taken into account only if is_authenticated is set to True

Static methods

def preprocess(url: str, data: Any) ‑> str

Preprocess the media URL coming from Reddit json.

Returned url should match the service provider API URL structure, from which to get the media information.

Parameters

url : str
Reddit media URL to preprocess.
data : json
Json from the Reddit API which contains the post data. Used to get fallback media urls for specific services.

Returns

str
Preprocessed url related to the service provider API.
def get(url: str) ‑> Union[requests.models.Response, str]

Get the media information.

This is usually through an http request to the service provider API.

Note

In case the request needs to be authenticated, this method assumes a valid access token is stored in access_token.

Parameters

url : str
URL related to the specific service provider API, on which an http request can be fired to get the media information.

Returns

requests.models.Response Response from the service provider API.

def postprocess(response: Union[requests.models.Response, str]) ‑> Media

From the service provider API response create the media object.

Warning

This is the only abstract method of the class. Thus it needs to be implemented in each child class. This is due to the intrinsic inexistence of a common pattern on which to create the media object.

Parameters

response : requests.models.Response
Response from the service provider API.

Returns

Media Media object related to the media retrieval process.

def authenticate() ‑> NoneType

Authenticate the service on the service provider API.

Update the access_code variable with the newly refreshed valid access token.

def get_media(url: str, data: Any) ‑> Media

Entrypoint of the class.

Takes care of the common logic of media information retrieval, which consists in executing the following steps (in order):

Caution

This method should not be overwritten by child classes.

Parameters

url : str
Media URL from the Reddit API json.
data : json
Json from the Reddit API which contains the post data. Used to get fallback media urls for specific services.

Returns

Media The media object accessible from the application.