Skip to content

Commit 1c45611

Browse files
authored
Add "query-path" API endpoint
Merges #34 Closes #31 Rename querypair endpoint to `query-metapaths` Update continuous deployment conda env update Refs #35
2 parents 6198c5f + dfa653e commit 1c45611

File tree

4 files changed

+69
-4
lines changed

4 files changed

+69
-4
lines changed

.circleci/deploy.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ git pull
1010

1111
# Update conda env if needed
1212
if [ -n "$ENV_DIFF" ]; then
13-
conda update --file $ENV_FILE
13+
source ~/miniconda/etc/profile.d/conda.sh
14+
conda env update --file $ENV_FILE
1415
fi
1516

1617
# Restart Gunicorn daemon

dj_hetmech/urls.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@
2323
urlpatterns = [
2424
path('v1/', views.api_root),
2525
path('v1/', include(router.urls)),
26-
path('v1/querypair/', views.QueryPairView.as_view(), name="query-pair"),
26+
path('v1/query-metapaths/', views.QueryMetapathsView.as_view(), name="query-metapaths"),
27+
path('v1/query-paths/', views.QueryPathsView.as_view(), name="query-paths"),
2728
]

dj_hetmech_app/models.py

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class Meta:
4141
# https://stackoverflow.com/a/42676612/4651668
4242
unique_together = ('metanode', 'identifier')
4343

44+
def get_cast_identifier(self):
45+
import builtins
46+
caster = getattr(builtins, self.identifier_type)
47+
return caster(self.identifier)
4448

4549
class Metapath(models.Model):
4650
abbreviation = models.CharField(primary_key=True, max_length=20)

dj_hetmech_app/views.py

+61-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
def api_root(request):
1515
return Response({
1616
'nodes': reverse('node-list', request=request),
17-
'querypair': reverse('query-pair', request=request),
17+
'query-metapaths': reverse('query-metapaths', request=request),
18+
'query-paths': reverse('query-paths', request=request),
1819
})
1920

2021

@@ -42,7 +43,7 @@ def get_queryset(self):
4243
return queryset
4344

4445

45-
class QueryPairView(APIView):
46+
class QueryMetapathsView(APIView):
4647
http_method_names = ['get']
4748

4849
def polish_pathcounts(self, source_id, target_id, pathcounts_data):
@@ -130,3 +131,61 @@ def get(self, request):
130131
)
131132

132133
return Response(data)
134+
135+
136+
class QueryPathsView(APIView):
137+
http_method_names = ['get']
138+
139+
def get(self, request):
140+
# Validate "source" parameter
141+
source_id = request.query_params.get('source', None)
142+
if source_id is None:
143+
return Response(
144+
{'error': 'source parameter not found in URL'},
145+
status=status.HTTP_400_BAD_REQUEST
146+
)
147+
try:
148+
source_node = Node.objects.get(pk=source_id)
149+
except:
150+
return Response(
151+
{'error': 'source node not found in database'},
152+
status=status.HTTP_404_NOT_FOUND
153+
)
154+
source_identifier = source_node.get_cast_identifier()
155+
156+
# Validate "target" parameter
157+
target_id = request.query_params.get('target', None)
158+
if target_id is None:
159+
return Response(
160+
{'error': 'target parameter not found in URL'},
161+
status=status.HTTP_400_BAD_REQUEST
162+
)
163+
try:
164+
target_node = Node.objects.get(pk=target_id)
165+
except:
166+
return Response(
167+
{'error': 'target node not found in database'},
168+
status=status.HTTP_404_NOT_FOUND
169+
)
170+
target_identifier = target_node.get_cast_identifier()
171+
172+
# Validate "metapath"
173+
metapath = request.query_params.get('metapath', None)
174+
if metapath is None:
175+
return Response(
176+
{'error': 'metapth parameter not found in URL'},
177+
status=status.HTTP_400_BAD_REQUEST
178+
)
179+
180+
# TODO: test metapath is a valid metapath abbreviation
181+
182+
max_paths = request.query_params.get('max-paths', '100')
183+
max_paths = int(max_paths)
184+
if max_paths < 0:
185+
max_paths = None
186+
187+
from .utils.paths import get_paths
188+
from .utils import get_hetionet_metagraph
189+
metagraph = get_hetionet_metagraph()
190+
output = get_paths(metagraph, metapath, source_identifier, target_identifier, limit=max_paths)
191+
return Response(output)

0 commit comments

Comments
 (0)