Skip to content

Commit

Permalink
Created evaluate endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
edudouglas committed May 2, 2019
1 parent 36ba529 commit 9714237
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions bothub/api/v1/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
RepositorySerializer,
RepositoryAuthorizationSerializer,
AnalyzeTextSerializer,
EvaluateSerializer,
EditRepositorySerializer,
VoteSerializer,
RepositoryAuthorizationRoleSerializer,
Expand Down
4 changes: 4 additions & 0 deletions bothub/api/v1/serializers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ class AnalyzeTextSerializer(serializers.Serializer):
text = serializers.CharField(allow_blank=False)


class EvaluateSerializer(serializers.Serializer):
language = serializers.ChoiceField(LANGUAGE_CHOICES, required=True)


class VoteSerializer(serializers.ModelSerializer):
class Meta:
model = RepositoryVote
Expand Down
23 changes: 23 additions & 0 deletions bothub/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
from .serializers import RepositoryCategorySerializer
from .serializers import NewRepositoryExampleSerializer
from .serializers import AnalyzeTextSerializer
from .serializers import EvaluateSerializer
from .serializers import EditRepositorySerializer
from .serializers import NewRepositoryTranslatedExampleSerializer
from .serializers import VoteSerializer
Expand Down Expand Up @@ -523,6 +524,28 @@ def analyze(self, request, **kwargs):
message = error.get('message') # pragma: no cover
raise APIException(detail=message) # pragma: no cover

@detail_route(
methods=['POST'],
url_name='repository-evaluate')
def evaluate(self, request, **kwargs):
"""
Evaluate repository using Bothub NLP service
"""
repository = self.get_object()
user_authorization = repository.get_user_authorization(request.user)
if not user_authorization.can_write:
raise PermissionDenied()
serializer = EvaluateSerializer(
data=request.data) # pragma: no cover
serializer.is_valid(raise_exception=True) # pragma: no cover
request = Repository.request_nlp_evaluate( # pragma: no cover
user_authorization, serializer.data)
if request.status_code != status.HTTP_200_OK: # pragma: no cover
raise APIException( # pragma: no cover
{'status_code': request.status_code},
code=request.status_code)
return Response(request.json()) # pragma: no cover

@detail_route(
methods=['POST'],
url_name='repository-vote',
Expand Down
12 changes: 12 additions & 0 deletions bothub/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class Meta:

nlp_train_url = '{}train/'.format(settings.BOTHUB_NLP_BASE_URL)
nlp_analyze_url = '{}parse/'.format(settings.BOTHUB_NLP_BASE_URL)
nlp_evaluate_url = '{}evaluate/'.format(settings.BOTHUB_NLP_BASE_URL)

@classmethod
def request_nlp_train(cls, user_authorization):
Expand All @@ -201,6 +202,17 @@ def request_nlp_analyze(cls, user_authorization, data):
user_authorization.uuid)})
return r # pragma: no cover

@classmethod
def request_nlp_evaluate(cls, user_authorization, data):
r = requests.post( # pragma: no cover
cls.nlp_evaluate_url,
data={
'language': data.get('language'),
},
headers={'Authorization': 'Bearer {}'.format(
user_authorization.uuid)})
return r # pragma: no cover

@property
def available_languages(self):
examples = self.examples()
Expand Down

0 comments on commit 9714237

Please sign in to comment.