Skip to content

Commit 3a2155e

Browse files
committed
Update py-ard REST Service
- add `validate` endpoint to validate a GL String
1 parent b32da35 commit 3a2155e

File tree

4 files changed

+95
-7
lines changed

4 files changed

+95
-7
lines changed

api-spec.yaml

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,66 @@ paths:
5050
type: string
5151
example: "A*01:01+A*01:01^B*07:02+B*08:01^C*02:02/C*02:10+C*07:01/C*07:150Q^DPB1*28:01+DPB1*28:01"
5252
400:
53-
description: Invalid GL String
53+
description: Invalid GL String Form
54+
content:
55+
application/json:
56+
schema:
57+
type: object
58+
properties:
59+
message:
60+
description: Describes what went wrong
61+
type: string
62+
example: "Invalid HLA locus"
63+
/validate:
64+
post:
65+
tags:
66+
- ARD Reduction
67+
operationId: api.validate_controller
68+
summary: Validate GL String
69+
description: |
70+
Given a GL String report whether it is valid or not
71+
requestBody:
72+
content:
73+
application/json:
74+
schema:
75+
properties:
76+
gl_string:
77+
description: GL String
78+
type: string
79+
example: "A*01:01+A*01:01^B*08:ASXJP+B*07:02^C*02:02+C*07:HTGM^DPB1*28:01:01G+DPB1*296:01"
80+
responses:
81+
200:
82+
description: Validation Result
83+
content:
84+
application/json:
85+
schema:
86+
type: object
87+
properties:
88+
valid:
89+
description: Is GL String valid
90+
type: boolean
91+
example: true
92+
404:
93+
description: GL String didn't validate
94+
content:
95+
application/json:
96+
schema:
97+
type: object
98+
properties:
99+
valid:
100+
description: Is GL String valid
101+
type: boolean
102+
example: false
103+
message:
104+
description: Describes what went wrong
105+
type: string
106+
example: "Provided GL String is invalid: HLA-A*01:BLAH"
107+
cause:
108+
description: Explanation of why the GL String is not valid
109+
type: string
110+
example: "HLA-A*01:BLAH is not a valid GL Allele"
111+
400:
112+
description: Invalid GL String Form
54113
content:
55114
application/json:
56115
schema:

api.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
from flask import request
22

33
from pyard import ARD
4-
from pyard.exceptions import PyArdError
4+
from pyard.exceptions import PyArdError, InvalidAlleleError
55

66
# Globally accessible for all endpoints
77
ard = ARD()
88

99

10+
def validate_controller():
11+
if request.json:
12+
# Check the request has required inputs
13+
try:
14+
gl_string = request.json["gl_string"]
15+
except KeyError:
16+
return {"message": "gl_string not provided"}, 404
17+
# Validate
18+
try:
19+
ard.isvalid_gl(gl_string)
20+
return {"valid": True}, 200
21+
except InvalidAlleleError as e:
22+
return {
23+
"valid": False,
24+
"message": f"Provided GL String is invalid: {gl_string}",
25+
"cause": e.message,
26+
}, 404
27+
except PyArdError as e:
28+
return {"message": e.message}, 400
29+
30+
1031
def redux_controller():
1132
if request.json:
1233
# Check the request has required inputs
1334
try:
1435
gl_string = request.json["gl_string"]
1536
reduction_method = request.json["reduction_method"]
1637
except KeyError:
17-
return {"message": "gl_string and reduction_method not provided"}, 400
38+
return {"message": "gl_string and reduction_method not provided"}, 404
1839
# Perform redux
1940
try:
2041
redux_gl_string = ard.redux_gl(gl_string, reduction_method)

pyard/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ def __str__(self) -> str:
2323

2424

2525
class InvalidTypingError(PyArdError):
26-
def __init__(self, message: str) -> None:
26+
def __init__(self, message: str, cause=None) -> None:
2727
self.message = message
28+
self.cause = cause
2829

2930
def __str__(self) -> str:
3031
return f"Invalid HLA Typing: {self.message}"

pyard/pyard.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,12 @@ def redux_gl(self, glstring: str, redux_type: VALID_REDUCTION_TYPES) -> str:
276276

277277
validate_reduction_type(redux_type)
278278

279-
if not self.isvalid_gl(glstring):
280-
raise InvalidTypingError(f"{glstring} is not a valid typing.")
279+
try:
280+
self.isvalid_gl(glstring)
281+
except InvalidAlleleError as e:
282+
raise InvalidTypingError(
283+
f"{glstring} is not valid GL String. \n {e.message}", e
284+
)
281285

282286
if re.search(r"\^", glstring):
283287
return self.sorted_unique_gl(
@@ -603,7 +607,10 @@ def isvalid_gl(self, glstring: str) -> bool:
603607
return all(map(self.isvalid_gl, glstring.split("/")))
604608

605609
# what falls through here is an allele
606-
return self.isvalid(glstring)
610+
is_valid_allele = self.isvalid(glstring)
611+
if not is_valid_allele:
612+
raise InvalidAlleleError(f"{glstring} is not a valid Allele")
613+
return is_valid_allele
607614

608615
def mac_toG(self, allele: str) -> str:
609616
"""

0 commit comments

Comments
 (0)