Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions contentcuration/automation/tests/appnexus/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@ class MockBackend(Backend):
def connect(self) -> None:
return super().connect()

def make_request(self, url: str, params=None):
return super().make_request(url, params)

def request(self) -> None:
return super().request()

def response(self) -> None:
return super().response()
def make_request(self, request):
return super().make_request(request)

@classmethod
def _create_instance(cls) -> 'MockBackend':
Expand Down
12 changes: 6 additions & 6 deletions contentcuration/automation/utils/appnexus/APILayer.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Different backends can now be created by implementing the base `Backend` class:
```python
# Implement CONCRETE CLASS using ABSTRACT Backend class
CLASS GCS IMPLEMENTS Backend:
METHOD make_request(params):
METHOD make_request(request):
# make request to Google Cloud Storage services

METHOD connect(params):
Expand All @@ -62,7 +62,7 @@ CLASS GCS IMPLEMENTS Backend:
# initialize a GCS Backend instance

CLASS ML IMPLEMENTS Backend:
METHOD make_request(params):
METHOD make_request(request):
# make request to DeepLearning models hosted as service

METHOD connect(params):
Expand Down Expand Up @@ -128,18 +128,18 @@ With this `Adapter` class in place, we can create Adapter that are able interact

```python
CLASS Recommendation INHERITS ADAPTER:
METHOD generateEmbeddings(self, params) -> Boolean
METHOD generateEmbeddings(self, request) -> Boolean
# [ Implementation ]

METHOD getRecommendation(self, params) -> Array
METHOD getRecommendation(self, request) -> Array
# [ Implementation ]

CLASS Transcription INHERITS ADAPTER:
METHOD generateCaption(self, params) -> Array
METHOD generateCaption(self, request) -> Array
# [ Implementation ]

CLASS OtherAdapter INHERITS ADAPTER:
METHOD someOperation(self, params) -> Any
METHOD someOperation(self, request) -> Any
# Operation that any backend wants
```

Expand Down
1 change: 0 additions & 1 deletion contentcuration/automation/utils/appnexus/adapters.py

This file was deleted.

1 change: 0 additions & 1 deletion contentcuration/automation/utils/appnexus/backends.py

This file was deleted.

35 changes: 13 additions & 22 deletions contentcuration/automation/utils/appnexus/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
from abc import ABC
from abc import abstractmethod
from builtins import NotImplementedError
from typing import Dict
from typing import Union


class BackendRequest(object):
""" Class that should be inherited by specific backend for its requests"""
pass


class BackendResponse(object):
""" Class that should be inherited by specific backend for its responses"""
pass


class Backend(ABC):
Expand All @@ -20,18 +28,8 @@ def connect(self) -> None:
pass

@abstractmethod
def make_request(self, url: str, params=None) -> Union[bytes, str, Dict]:
""" Makes an HTTP request to a given URL using the specified method. """
pass

@abstractmethod
def request(self) -> None:
""" Blueprint for the request object. """
pass

@abstractmethod
def response(self) -> None:
""" Blueprint for the response object. """
def make_request(self, request) -> BackendResponse:
""" Make a request based on "request" """
pass

@classmethod
Expand Down Expand Up @@ -59,13 +57,6 @@ class Adapter:
This class should be inherited by adapter classes that facilitate
interaction with different backend implementations.
"""

def __init__(self, backend: Backend) -> None:
self.backend = backend

def request(self):
""" Forward the request to the chosen Backend """
return self.backend.request()

def response(self):
""" Forward the response to the chosen Backend """
return self.backend.response()
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.test import TestCase

from contentcuration.utils.recommendations import Recommendations


class RecommendationsTestCase(TestCase):
def test_backend_initialization(self):
recomendations = Recommendations()
self.assertIsNotNone(recomendations)
self.assertIsInstance(recomendations.get_instance(), Recommendations)
65 changes: 65 additions & 0 deletions contentcuration/contentcuration/utils/recommendations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from typing import Union

from automation.utils.appnexus.base import Adapter
from automation.utils.appnexus.base import Backend
from automation.utils.appnexus.base import BackendFactory
from automation.utils.appnexus.base import BackendRequest
from automation.utils.appnexus.base import BackendResponse


class RecommendationsBackendRequest(BackendRequest):
pass


class RecommedationsRequest(RecommendationsBackendRequest):
def __init__(self) -> None:
super().__init__()


class EmbeddingsRequest(RecommendationsBackendRequest):
def __init__(self) -> None:
super().__init__()


class RecommendationsBackendResponse(BackendResponse):
pass


class RecommendationsResponse(RecommendationsBackendResponse):
def __init__(self) -> None:
pass


class EmbeddingsResponse(RecommendationsBackendResponse):
def __init__(self) -> None:
pass


class RecommendationsBackendFactory(BackendFactory):
def create_backend(self) -> Backend:
# Return backend based on some setting.
return super().create_backend()


class RecommendationsAdapter(Adapter):

def generate_embedding(self, text) -> EmbeddingsResponse:
request = EmbeddingsRequest()
return self.backend.make_request(request)

def get_recommendations(self) -> RecommendationsResponse:
request = RecommedationsRequest()
return self.backend.make_request(request)


class Recommendations(Backend):

def connect(self) -> None:
return super().connect()

def make_request(self, request) -> Union[EmbeddingsResponse, RecommendationsResponse]:
return super().make_request(request)

@classmethod
def _create_instance(cls) -> 'Recommendations':
return cls()