Skip to content

Commit

Permalink
Merge pull request #739 from Ilhasoft/feature/repository_token_api
Browse files Browse the repository at this point in the history
RepositoryTokenByUser API
  • Loading branch information
victor-salles committed Aug 12, 2022
2 parents 1c373b4 + 493b005 commit 21ba8e9
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
·
</p>
</p>

<br />
<p align="center">
<img src="https://user-images.githubusercontent.com/5360835/65427083-1af35900-de01-11e9-86ef-59f1eee79a68.png" width="230" height="70" alt="Bothub" />
Expand Down Expand Up @@ -146,7 +146,7 @@ You can set environment variables in your OS, write on ```.env``` file or pass v
| APM_SERVICE_ENVIRONMENT | ```string``` | ```''``` | Environment that APM is running on
| ENVIRONMENT | ```string``` | ```production``` | Specify the environment you are going to run, it is also used for sentry
| SUGGESTION_LANGUAGES | ```string``` | ```en|pt_br``` | Specify the the languages supported by environment for word and intent suggestions
| N_WORDS_TO_GENERATE | ```int``` | ```4``` | Specify the number of suggestions that will be returned for word suggestions
| N_WORDS_TO_GENERATE | ```int``` | ```4``` | Specify the number of suggestions that will be returned for word suggestions
| N_SENTENCES_TO_GENERATE | ```int``` | ```10``` | Specify the number of suggestions that will be returned for intent suggestions
| REDIS_TIMEOUT | ```int``` | ```3600``` | Specify a systemwide Redis keys life time
| SECRET_KEY_CHECK_LEGACY_USER | ```string``` | ```None``` | Enables and specifies the token to use for the legacy user endpoint.
Expand Down Expand Up @@ -185,6 +185,7 @@ You can set environment variables in your OS, write on ```.env``` file or pass v
| USE_ELASTICSEARCH | ```boolean``` | ```true``` | Change the logic in requirements_to_train to use either elasticsearch or postgres.
| REPOSITORY_BLOCK_USER_LOGS | ```list``` | ```[]``` | List of repository authorization(api bearer) that won't save logs
| RUN_AS_DEVELOPMENT_MODE | ```boolean``` | ```false``` | Specifies how to run the server, in production or development mode.
| TEST_REPOSITORY_ID | ```string``` | ```None``` | The repository from which the RepositoryTokenByUserViewSet will retrieve the logged user's access token.


## Roadmap
Expand All @@ -206,4 +207,3 @@ Contributions are what make the open source community such an amazing place to b
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request

28 changes: 28 additions & 0 deletions bothub/api/v2/repository/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,34 @@ def destroy(self, request, *args, **kwargs):
return super().destroy(request, *args, **kwargs)


class RepositoryTokenByUserViewSet(mixins.ListModelMixin, GenericViewSet):
def get_queryset(self):
user = self.request.user
if user.is_anonymous:
return RepositoryAuthorization.objects.none()
return RepositoryAuthorization.objects.filter(user=self.request.user)

def list(self, request, **kwargs):
"""
Get repository access token based on logged user
"""

repository_id = settings.TEST_REPOSITORY_ID
if not repository_id:
return Response(
{"TEST_REPOSITORY_ID": "Not set"}, status=status.HTTP_400_BAD_REQUEST
)
try:
repository = Repository.objects.get(pk=repository_id)
except Repository.DoesNotExist:
return Response(
{"Repository": "Does Not Exist"}, status=status.HTTP_404_NOT_FOUND
)

authorization = repository.get_user_authorization(request.user)
return Response({"access_token": str(authorization.uuid)})


class RepositoryExampleViewSet(
mixins.CreateModelMixin,
mixins.RetrieveModelMixin,
Expand Down
6 changes: 6 additions & 0 deletions bothub/api/v2/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from .repository.views import RepositoriesViewSet
from .repository.views import RepositoryAuthorizationRequestsViewSet
from .repository.views import RepositoryAuthorizationViewSet
from .repository.views import RepositoryTokenByUserViewSet
from .repository.views import RepositoryCategoriesView
from .repository.views import RepositoryExampleViewSet
from .repository.views import RepositoryMigrateViewSet
Expand Down Expand Up @@ -180,6 +181,11 @@ def get_lookup_regex(self, viewset, lookup_prefix=""):
router.register(
"repository/authorization-requests", RepositoryAuthorizationRequestsViewSet
)
router.register(
"repository/authorization-by-user",
RepositoryTokenByUserViewSet,
basename="authorization-by-user",
)
router.register("repository/example", RepositoryExampleViewSet)
router.register("repository/example-bulk", RepositoryExamplesBulkViewSet)
router.register("repository/intent", RepositoryIntentViewSet)
Expand Down
2 changes: 2 additions & 0 deletions bothub/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,5 @@
]

REPOSITORY_BLOCK_USER_LOGS = env.list("REPOSITORY_BLOCK_USER_LOGS", default=[])

TEST_REPOSITORY_ID = env("TEST_REPOSITORY_ID", default=None)
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ services:
- ELASTICSEARCH_REPOSITORYNLPLOG_INDEX=${ELASTICSEARCH_REPOSITORYQANLPLOG_INDEX:-ai_repositoryqanlplog}
- ELASTICSEARCH_SIGNAL_PROCESSOR=${ELASTICSEARCH_SIGNAL_PROCESSOR:-celery}
- RUN_AS_DEVELOPMENT_MODE=${RUN_AS_DEVELOPMENT_MODE:-false}
- TEST_REPOSITORY_ID=${TEST_REPOSITORY_ID}

celery:
build:
Expand Down Expand Up @@ -119,6 +120,7 @@ services:
- ELASTICSEARCH_REPOSITORYBASICEXAMPLE_INDEX=${ELASTICSEARCH_REPOSITORYBASICEXAMPLE_INDEX:-ai_repositorybasicexample}
- ELASTICSEARCH_SIGNAL_PROCESSOR=${ELASTICSEARCH_SIGNAL_PROCESSOR:-celery}
- USE_ELASTICSEARCH=${USE_ELASTICSEARCH:-true}
- TEST_REPOSITORY_ID=${TEST_REPOSITORY_ID}

bothub-engine-celery-redis:
image: redis
Expand Down

0 comments on commit 21ba8e9

Please sign in to comment.