-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline.py
More file actions
164 lines (153 loc) · 6.46 KB
/
pipeline.py
File metadata and controls
164 lines (153 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from datetime import datetime, timezone
from typing import Generator
import dlt
from dlt.sources.rest_api import RESTAPIConfig, rest_api_resources
from dlt.extract.resource import DltResource
from requests.models import Response
from .auth import OAuth2OTF
@dlt.source
def otf_source(args) -> Generator[DltResource, None, None]:
_id_token = OAuth2OTF().return_id_token()
def invalidate_null_content(response: Response) -> Response:
"""Set the status code to 204 if the response content is an empty string"""
if not response.content:
response.status_code = 204
return response
def add_workout_id_field(record: dict) -> dict:
"""Add workout.id to the record if it doesn't exist to be used to resolve further endpoints"""
if not record.get("workout"):
record["workout"] = {'id': 'workout_key'}
return record
config: RESTAPIConfig = {
"client": {
"base_url": "",
"headers": {'Authorization': _id_token},
},
"resource_defaults": {
"primary_key": "id",
"write_disposition": "replace" if args.full_refresh else "merge",
"table_format": "delta",
},
"resources": [
{
"name": "me",
"endpoint": {
"path": "https://api.orangetheory.io/v1/people/me",
"data_selector": "$",
"paginator": "single_page",
},
},
{
"name": "bookings",
"processing_steps": [
# make sure we have a workout key to resolve further endpoints
{"map": add_workout_id_field}
],
"endpoint": {
"data_selector": "items",
"path": "https://api.orangetheory.io/v1/bookings/me",
"paginator": "single_page",
"params": {
"starts_after": {
"type": "incremental",
"cursor_path": "created_at",
"initial_value": "2021-01-01T00:00:00Z",
},
"ends_before": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"include_canceled": "false",
},
},
},
{
"name": "performance_summaries",
"include_from_parent": ["id"],
"endpoint": {
"path": "https://api.orangetheory.io/v1/performance-summaries/{resources.bookings.workout.id}",
"paginator": "single_page",
"response_actions": [
invalidate_null_content,
{"status_code": 204, "action": "ignore"}
],
},
},
{
"name": "body_composition",
"primary_key": "scanResultUUId",
"endpoint": {
"path": "https://api.orangetheory.co/member/members/{resources.me.mbo_client_id}/body-composition",
"paginator": "single_page",
}
},
{
"name": "heart_rate",
"primary_key": "memberUuid",
"endpoint": {
"path": "https://api.yuzu.orangetheory.com/v1/physVars/maxHr/history?memberUuid={resources.me.mbo_client_id}",
"paginator": "single_page",
"data_selector": "$",
},
},
{
"name": "telemetry",
"primary_key": "classHistoryUuid",
"include_from_parent": ["id"],
"endpoint": {
"path": "https://api.yuzu.orangetheory.com/v1/performance/summary",
"paginator": "single_page",
"data_selector": "$",
"params": {
"classHistoryUuid": "{resources.bookings.workout.id}",
"maxDataPoints": 150
},
},
},
{
"name": "challenges",
"primary_key": ["ChallengeCategoryId", "ChallengeSubCategoryId"],
"include_from_parent": ["mbo_client_id"],
"endpoint": {
"path": "https://api.orangetheory.co/challenges/v3.1/member/{resources.me.mbo_client_id}",
"data_selector": "$.Dto[Programs,Challenges][*]",
},
},
{
"name": "challenge_activity",
"primary_key": ["ChallengeCategoryId", "ChallengeName"],
"include_from_parent": ["ChallengeSubCategoryId"],
"max_table_nesting": 1,
"endpoint": {
"path": "https://api.orangetheory.co/challenges/v3/member/{resources.challenges._me_mbo_client_id}/benchmarks",
"paginator": "single_page",
"data_selector": "$.Dto",
"params": {
"challengeTypeId": "{resources.challenges.ChallengeCategoryId}",
"challengeSubTypeId": "{resources.challenges.ChallengeSubCategoryId}",
},
},
},
{
# benchmarks have a different schema than challenges, but same endpoint
"name": "benchmarks",
"include_from_parent": ["mbo_client_id"],
"primary_key": ["EquipmentId", "EquipmentName"],
"endpoint": {
"path": "https://api.orangetheory.co/challenges/v3.1/member/{resources.me.mbo_client_id}",
"data_selector": "$.Dto.Benchmarks[*]",
},
},
{
"name": "benchmark_activity",
"primary_key": ["ChallengeCategoryId", "EquipmentId"],
"endpoint": {
"path": "https://api.orangetheory.co/challenges/v3/member/{resources.benchmarks._me_mbo_client_id}/benchmarks",
"paginator": "single_page",
"data_selector": "$.Dto",
"params": {
"challengeTypeId": "1",
"EquipmentId": "{resources.benchmarks.EquipmentId}",
},
},
},
],
}
yield from rest_api_resources(config)