Skip to content

Commit

Permalink
implement connect rest client
Browse files Browse the repository at this point in the history
  • Loading branch information
helllllllder committed May 23, 2022
1 parent ccb74aa commit a163c52
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ You can set environment variables in your OS, write on ```.env``` file or pass v
| OIDC_RP_SCOPES | ```string``` | ```openid email``` | The OpenID Connect scopes to request during login.
| CONNECT_GRPC_SERVER_URL | ```string``` | ```localhost:8002``` | Define grpc connect server url
| CONNECT_CERTIFICATE_GRPC_CRT | ```string``` | ```None``` | Absolute certificate path for secure grpc communication
| CONNECT_API_URL | ```string``` | ```None``` | Connect module api url
| USE_GRPC | ```bool``` | ```False``` | Use connect gRPC clients
| RECAPTCHA_SECRET_KEY | ```string``` | ```''``` | Token of the recaptcha used in the validation of a user's registration.
| REPOSITORY_NLP_LOG_LIMIT | ```int``` | ```10000``` | Limit of query size to repository log.
| REPOSITORY_RESTRICT_ACCESS_NLP_LOGS | ```list``` | ```[]``` | Restricts log access to a particular or multiple intelligences
Expand Down
78 changes: 78 additions & 0 deletions bothub/api/v2/internal/connect_rest_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import requests
import json
from typing import List, Dict

from django.conf import settings


class ConnectRESTClient:
def __init__(self):
self.base_url = settings.CONNECT_API_URL
self.headers = {
"Content-Type": "application/json; charset: utf-8",
"Authorization": self.get_auth_token(),
}

def get_auth_token(self) -> str:
request = requests.post(
url=settings.OIDC_OP_TOKEN_ENDPOINT,
data={
"client_id": settings.OIDC_RP_CLIENT_ID,
"client_secret": settings.OIDC_RP_CLIENT_SECRET,
"grant_type": "client_credentials",
},
)
token = request.json().get("access_token")
return f"Bearer {token}"

def list_classifiers(
self, project_uuid: str, user_email: str
) -> List[Dict[str, str]]:
request = requests.get(
url=f"{self.base_url}/v1/organization/project/list_classifier/",
headers=self.headers,
params={"project_uuid": project_uuid, "user_email": user_email},
)

return request.json()

def list_authorizations(self, project_uuid: str) -> List[str]:
classifiers = self.list_classifiers(project_uuid=project_uuid)

return [classifier.get("authorization_uuid") for classifier in classifiers]

def get_authorization_classifier(
self, project_uuid: str, authorization_uuid: str
) -> str:
"""
Recives a authorization UUID and returns the respective classifier UUID
"""
classifiers = self.list_classifiers(project_uuid)
classifier = filter(
lambda classifier: classifier["authorization_uuid"] == authorization_uuid,
classifiers,
)

return classifier.get("uuid")

def remove_authorization(
self, project_uuid: str, authorization_uuid: str, user_email: str
):
classifier_uuid = self.get_authorization_classifier(
project_uuid, authorization_uuid
)
request = requests.delete(
url=f"{self.base_url}/v1/organization/project/destroy_classifier/",
headers=self.headers,
json=json.dumps({"uuid": classifier_uuid, "user_email": user_email}),
)

return request.json()

def create_classifier(self, **kwargs):
request = requests.post(
url=f"{self.base_url}/v1/organization/project/create_classifier/",
headers=self.headers,
json=json.dumps({**kwargs, "classifier_type": "bothub"}),
)
return request.json()
2 changes: 1 addition & 1 deletion bothub/api/v2/repository/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ def list_project_organizatiton(self, request, **kwargs):
raise ValidationError(_("Need to pass 'project_uuid' in query params"))

task = celery_app.send_task(
name="get_project_organization", args=[project_uuid]
name="get_project_organization", args=[project_uuid, request.user.email]
)
task.wait()

Expand Down
26 changes: 20 additions & 6 deletions bothub/common/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from django_elasticsearch_dsl.registries import registry

from bothub import translate
from bothub.api.grpc.connect_grpc_client import ConnectGRPCClient
from bothub.celery import app
from bothub.common.models import (
RepositoryQueueTask,
Expand Down Expand Up @@ -40,6 +39,14 @@
)


if settings.USE_GRPC:
from bothub.api.grpc.connect_grpc_client import ConnectGRPCClient as ConnectClient
else:
from bothub.api.v2.internal.connect_rest_client import (
ConnectRESTClient as ConnectClient,
)


@app.task(name="es_handle_save")
def handle_save(pk, app_label, model_name):
sender = apps.get_model(app_label, model_name)
Expand Down Expand Up @@ -544,23 +551,30 @@ def evaluate_crossvalidation(data, authorization_token): # pragma: no cover


@app.task(name="get_project_organization")
def get_project_organization(project_uuid: str): # pragma: no cover
grpc_client = ConnectGRPCClient()
authorizations = grpc_client.list_authorizations(project_uuid=project_uuid)
def get_project_organization(
project_uuid: str, user_email: str = ""
): # pragma: no cover
grpc_client = ConnectClient()
if settings.USE_GRPC:
authorizations = grpc_client.list_authorizations(project_uuid=project_uuid)
else:
authorizations = grpc_client.list_authorizations(
project_uuid=project_uuid, user_email=user_email
)
return authorizations


@app.task(name="remove_authorizations_project")
def remove_authorizations_project(
project_uuid: str, authorizations_uuids: list, user_email: str
):
grpc_client = ConnectGRPCClient()
grpc_client = ConnectClient()
for authorization_uuid in authorizations_uuids:
grpc_client.remove_authorization(project_uuid, authorization_uuid, user_email)


@app.task(name="create_repository_project")
def create_repository_project(**kwargs):
grpc_client = ConnectGRPCClient()
grpc_client = ConnectClient()
grpc_client.create_classifier(**kwargs)
return kwargs
6 changes: 6 additions & 0 deletions bothub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
SECRET_KEY_CHECK_LEGACY_USER=(str, None),
CONNECT_GRPC_SERVER_URL=(str, "localhost:8002"),
CONNECT_CERTIFICATE_GRPC_CRT=(str, None),
USE_GRPC=(bool, False),
CONNECT_API_URL=(str, ""),
REPOSITORY_RESTRICT_ACCESS_NLP_LOGS=(list, []),
REPOSITORY_BLOCK_USER_LOGS=(list, []),
REPOSITORY_KNOWLEDGE_BASE_DESCRIPTION_LIMIT=(int, 450),
Expand Down Expand Up @@ -531,6 +533,10 @@

CONNECT_CERTIFICATE_GRPC_CRT = env.str("CONNECT_CERTIFICATE_GRPC_CRT")

USE_GRPC = env.bool("USE_GRPC", default=False)

CONNECT_API_URL = env.str("CONNECT_API_URL", default="https://api.dev.cloud.weni.ai")

# ElasticSearch
ELASTICSEARCH_DSL = {
"default": {"hosts": env.str("ELASTICSEARCH_DSL", default="es:9200")}
Expand Down

0 comments on commit a163c52

Please sign in to comment.