Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit 36f837a

Browse files
jdrakens-circle-ci
andauthored
LEXIO-37876 Adds richer metadata model to query resource (#2)
* LEXIO-37876 Adds richer metadata model to query resource * Version bumped to 0.3.0 Co-authored-by: ns-circle-ci <devops-team+circleci@narrativescience.com>
1 parent 31403b5 commit 36f837a

File tree

6 files changed

+68
-7
lines changed

6 files changed

+68
-7
lines changed

crma_api_client/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
from .client import CRMAAPIClient
44

5-
__version__ = "0.2.0"
5+
__version__ = "0.3.0"

crma_api_client/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ async def list_dataset_versions(self, identifier: str) -> DatasetVersionsRespons
163163
identifier: Dataset name or ID
164164
165165
Returns:
166-
DatasetVersionsResponse object
166+
list of all versions for the dataset
167167
168168
"""
169169
response = await self.request(f"/wave/datasets/{identifier}/versions", "GET")
@@ -185,7 +185,7 @@ async def query(
185185
timezone: Timezone for the query
186186
187187
Returns:
188-
QueryResponse object
188+
query results containing records and metadata
189189
190190
"""
191191
json_data = {

crma_api_client/resources/dataset.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ class Dataset(BaseModel):
1717

1818

1919
class DatasetVersion(BaseModel):
20-
"""Dataset version model"""
20+
"""Dataset version model
21+
22+
See https://developer.salesforce.com/docs/atlas.en-us.bi_dev_guide_rest.meta/bi_dev_guide_rest/bi_responses_dataset_version.htm
23+
"""
2124

2225
created_by: User
2326
created_date: datetime
@@ -36,7 +39,10 @@ class Config:
3639

3740

3841
class DatasetVersionsResponse(BaseModel):
39-
"""Response model for a list of dataset versions"""
42+
"""Response model for a list of dataset versions
43+
44+
See https://developer.salesforce.com/docs/atlas.en-us.bi_dev_guide_rest.meta/bi_dev_guide_rest/bi_responses_dataset_version_collection.htm
45+
"""
4046

4147
url: str
4248
versions: List[DatasetVersion]

crma_api_client/resources/query.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,70 @@ class QueryLanguage(str, Enum):
1616
sql = "SQL"
1717

1818

19+
class ProjectionField(BaseModel):
20+
"""Field projected in the query result"""
21+
22+
id: str
23+
type: str
24+
25+
@property
26+
def name(self) -> str:
27+
"""Return field name that omits stream reference"""
28+
return self.id.split(".")[-1]
29+
30+
31+
class LineageProjection(BaseModel):
32+
"""Field projection metadata container"""
33+
34+
field: ProjectionField
35+
36+
37+
class QueryLineage(BaseModel):
38+
"""Lineage that describes field projections in a query result"""
39+
40+
type: str
41+
projections: List[LineageProjection]
42+
43+
44+
class QueryResultsMetadata(BaseModel):
45+
"""Query results metadata"""
46+
47+
lineage: QueryLineage
48+
query_language: QueryLanguage
49+
50+
class Config:
51+
"""Model configuration"""
52+
53+
alias_generator = to_camel
54+
55+
1956
class QueryResults(BaseModel):
2057
"""Query results model"""
2158

59+
metadata: List[QueryResultsMetadata]
2260
records: List[Dict[str, Any]]
2361

2462

2563
class QueryResponse(BaseModel):
26-
"""Response model for the query resource"""
64+
"""Response model for the query resource
65+
66+
See https://developer.salesforce.com/docs/atlas.en-us.bi_dev_guide_rest.meta/bi_dev_guide_rest/bi_resources_query.htm
67+
"""
2768

2869
action: str
2970
response_id: str
3071
results: QueryResults
3172
query: str
3273
response_time: int
3374

75+
@property
76+
def fields(self) -> List[ProjectionField]:
77+
"""Return the fields from the query response metadata
78+
79+
This assumes there is only one metadata object and one lineage object.
80+
"""
81+
return [p.field for p in self.results.metadata[0].lineage.projections]
82+
3483
class Config:
3584
"""Model configuration"""
3685

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "crma-api-client"
3-
version = "0.2.0"
3+
version = "0.3.0"
44
description = "CRM Analytics REST API Client"
55
authors = ["Jonathan Drake <jon.drake@salesforce.com>"]
66
license = "BSD-3-Clause"

tests/functional/test_query.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44

55
from crma_api_client.client import CRMAAPIClient
6+
from crma_api_client.resources.query import ProjectionField
67

78

89
@pytest.mark.asyncio
@@ -24,3 +25,8 @@ async def test_query(client: CRMAAPIClient):
2425
{"Category": "Office Supplies", "Sales": 719047.032},
2526
{"Category": "Technology", "Sales": 836154.033},
2627
]
28+
assert response.fields == [
29+
ProjectionField(id="q.Category", type="string"),
30+
ProjectionField(id="q.Sales", type="numeric"),
31+
]
32+
assert response.fields[0].name == "Category"

0 commit comments

Comments
 (0)