-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Program upload refactorization (#1112)
* created exceptions class * created base program service class * use new service in the upload method * services now are versioned * applied services versioned logic * Applied black format * Remove request from the business logic * created a save program service test * fixed black format * removed self from static methods * fixed pylint messages * removed commented line * rename services test * fix returned program without serialize * test for program service * added some logs for test * configure a logger for the gateway services
- Loading branch information
Showing
8 changed files
with
182 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
""" | ||
Custom exceptions for the gateway application | ||
""" | ||
|
||
from rest_framework import status | ||
|
||
|
||
class GatewayException(Exception): | ||
""" | ||
Generic custom exception for our application | ||
""" | ||
|
||
def __init__(self, message): | ||
super().__init__(message) | ||
|
||
|
||
class GatewayHttpException(GatewayException): | ||
""" | ||
Generic http custom exception for our application | ||
""" | ||
|
||
def __init__(self, message, http_code): | ||
super().__init__(message) | ||
self.http_code = http_code | ||
|
||
|
||
class InternalServerErrorException(GatewayHttpException): | ||
""" | ||
A wrapper for when we want to raise an internal server error | ||
""" | ||
|
||
def __init__(self, message, http_code=status.HTTP_500_INTERNAL_SERVER_ERROR): | ||
super().__init__(message, http_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
Services for api application: | ||
- Program Service | ||
- Job Service | ||
Version services inherit from the different services. | ||
""" | ||
|
||
# pylint: disable=too-few-public-methods | ||
|
||
import logging | ||
|
||
from .models import Program | ||
from .exceptions import InternalServerErrorException | ||
|
||
logger = logging.getLogger("gateway.services") | ||
|
||
|
||
class ProgramService: | ||
""" | ||
Program service allocate the logic related with programs | ||
""" | ||
|
||
@staticmethod | ||
def save(serializer, author, artifact) -> Program: | ||
""" | ||
Save method gets a program serializer and creates or updates a program | ||
""" | ||
|
||
title = serializer.data.get("title") | ||
existing_program = ( | ||
Program.objects.filter(title=title, author=author) | ||
.order_by("-created") | ||
.first() | ||
) | ||
|
||
if existing_program is not None: | ||
program = existing_program | ||
program.arguments = serializer.data.get("arguments") | ||
program.entrypoint = serializer.data.get("entrypoint") | ||
program.dependencies = serializer.data.get("dependencies", "[]") | ||
program.env_vars = serializer.data.get("env_vars", "{}") | ||
logger.debug("Program [%s] will be updated by [%s]", title, author) | ||
else: | ||
program = Program(**serializer.data) | ||
logger.debug("Program [%s] will be created by [%s]", title, author) | ||
program.artifact = artifact | ||
program.author = author | ||
|
||
# It would be nice if we could unify all the saves logic in one unique entry-point | ||
try: | ||
program.save() | ||
except (Exception) as save_program_exception: | ||
logger.error( | ||
"Exception was caught saving the program [%s] by [%s] \n" | ||
"Error trace: %s", | ||
title, | ||
author, | ||
save_program_exception, | ||
) | ||
raise InternalServerErrorException( | ||
"Unexpected error saving the program" | ||
) from save_program_exception | ||
|
||
logger.debug("Program [%s] saved", title) | ||
|
||
return program |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
""" | ||
Services api for V1. | ||
""" | ||
|
||
# pylint: disable=too-few-public-methods | ||
|
||
from api import services | ||
|
||
|
||
class ProgramService(services.ProgramService): | ||
""" | ||
Program service first version. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import json | ||
from rest_framework.test import APITestCase | ||
from api.v1.services import ProgramService | ||
from api.v1.serializers import ProgramSerializer | ||
from api.models import Program | ||
from django.contrib.auth.models import User | ||
|
||
|
||
class ServicesTest(APITestCase): | ||
"""Tests for V1 services.""" | ||
|
||
fixtures = ["tests/fixtures/fixtures.json"] | ||
|
||
def test_save_program(self): | ||
"""Test to verify that the service creates correctly an entry with its serializer.""" | ||
|
||
user = User.objects.get(id=1) | ||
data = '{"title": "My Qiskit Pattern", "entrypoint": "pattern.py"}' | ||
program_serializer = ProgramSerializer(data=json.loads(data)) | ||
program_serializer.is_valid() | ||
|
||
program = ProgramService.save(program_serializer, user, "path") | ||
entry = Program.objects.get(id=program.id) | ||
|
||
self.assertIsNotNone(entry) | ||
self.assertEqual(program.title, entry.title) |