mardi 6 août 2019

Combining Context and Token Use Cases into Generic ABC

I have a basic ABC here:

from abc import ABCMeta, abstractmethod

class StreamingService(object, metaclass=ABCMeta):
    @abstractmethod
    def search_one(q):
        pass

    @abstractmethod
    def supports_url(url):
        pass

    @abstractmethod
    def get_title_from_url(url):
        pass

Problem is some of the classes that extend StreamingService use a context for their calls i.e.

class Context:
  def __enter__():
    ...

  def __exit__():
    ...

def search_one(context, q):
  ...

context = Context()
search_one(context, "query")

And other classes only require a string token.

def search_one(token, q):
  ...

token = "abcdef012345"
search_one(token, "query")

I could have each function in the ABC require a "context" var that leaves it up to the subclass owner to interpret. However, this feels like sloppy code design that isn't clear on its intention and breaks the standard definition of a "context".

What are some design patterns I could use to fuse these two use cases and make it clear to anyone else using the code?

The ultimate goal would be to homogenize how developers interact with each streaming service to reduce confusion and the need to look up documentation for each individual service.

Suggestions outside the realm of code design and into how I'm interacting with the APIs themselves is also appreciated.

Aucun commentaire:

Enregistrer un commentaire