|
14 | 14 | def api_root(request):
|
15 | 15 | return Response({
|
16 | 16 | '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), |
18 | 19 | })
|
19 | 20 |
|
20 | 21 |
|
@@ -42,7 +43,7 @@ def get_queryset(self):
|
42 | 43 | return queryset
|
43 | 44 |
|
44 | 45 |
|
45 |
| -class QueryPairView(APIView): |
| 46 | +class QueryMetapathsView(APIView): |
46 | 47 | http_method_names = ['get']
|
47 | 48 |
|
48 | 49 | def polish_pathcounts(self, source_id, target_id, pathcounts_data):
|
@@ -130,3 +131,61 @@ def get(self, request):
|
130 | 131 | )
|
131 | 132 |
|
132 | 133 | 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