|
1 | | -from typing import Optional, Set |
| 1 | +from typing import Optional, Set, Dict, List |
2 | 2 |
|
3 | 3 | from starlette.applications import Starlette |
4 | 4 | from starlette.exceptions import HTTPException |
@@ -47,11 +47,68 @@ def parse_included_params(request: Request) -> Optional[Set[str]]: |
47 | 47 | Parses a request's `include` query parameter, if present, |
48 | 48 | and returns a sequence of included relations. |
49 | 49 |
|
50 | | - For example, if a request were to reach `/some-resource/?include=foo,foo.bar`, then: |
51 | | - `parse_included_params(request) -> {'foo', 'foo.bar'}` |
| 50 | + For example, if a request were to reach |
| 51 | + `/some-resource/?include=foo,foo.bar` |
| 52 | + then: |
| 53 | + `parse_included_params(request) == {'foo', 'foo.bar'}` |
52 | 54 | """ |
53 | 55 | include = request.query_params.get('include') |
54 | 56 | if include: |
55 | 57 | include = set(include.split(',')) |
56 | 58 | return include |
57 | 59 | return None |
| 60 | + |
| 61 | + |
| 62 | +def parse_sparse_fields_params(request: Request) -> Dict[str, List[str]]: |
| 63 | + """ |
| 64 | + Parses a request's `fields` query parameter, if present, |
| 65 | + and returns a dictionary of resource type -> sparse fields. |
| 66 | +
|
| 67 | + For example, if a request were to reach |
| 68 | + `/some-resource/?fields[some-resource]=foo,bar` |
| 69 | + then: |
| 70 | + `parse_sparse_fields_params(request) == {'some-resource': ['foo', 'bar']}` |
| 71 | + """ |
| 72 | + sparse_fields = dict() |
| 73 | + for qp_name, qp_value in request.query_params.items(): |
| 74 | + if qp_name.startswith('fields[') and qp_name.endswith(']'): |
| 75 | + resource_name_start = qp_name.index('[') + 1 |
| 76 | + resource_name_end = qp_name.index(']') |
| 77 | + resource_name = qp_name[resource_name_start:resource_name_end] |
| 78 | + if not resource_name or not qp_value or not all(qp_value.split(',')): |
| 79 | + raise JSONAPIException(status_code=400, detail='Incorrect sparse fields request.') |
| 80 | + |
| 81 | + sparse_fields[resource_name] = qp_value.split(',') |
| 82 | + return sparse_fields |
| 83 | + |
| 84 | + |
| 85 | +def filter_sparse_fields(item: dict, sparse_fields) -> dict: |
| 86 | + """ |
| 87 | + Given a dictionary representation of an item, |
| 88 | + mutate in place to drop fields according to a sparse fields request. |
| 89 | +
|
| 90 | + https://jsonapi.org/format/#fetching-sparse-fieldsets |
| 91 | + """ |
| 92 | + # filter `attributes` |
| 93 | + item_attributes = item.get('attributes') |
| 94 | + if item_attributes: |
| 95 | + new_attributes = item_attributes.copy() |
| 96 | + for attr_name in item_attributes: |
| 97 | + if attr_name not in sparse_fields: |
| 98 | + new_attributes.pop(attr_name, None) |
| 99 | + if new_attributes: |
| 100 | + item['attributes'] = new_attributes |
| 101 | + else: |
| 102 | + del item['attributes'] |
| 103 | + # filter `relationships` |
| 104 | + item_relationships = item.get('relationships') |
| 105 | + if item_relationships: |
| 106 | + new_relationships = item_relationships.copy() |
| 107 | + for rel_name in item_relationships: |
| 108 | + if rel_name not in sparse_fields: |
| 109 | + new_relationships.pop(rel_name, None) |
| 110 | + if new_relationships: |
| 111 | + item['relationships'] = new_relationships |
| 112 | + else: |
| 113 | + del item['relationships'] |
| 114 | + return item |
0 commit comments