Skip to content

Commit c6d43db

Browse files
authored
Merge pull request #66 from ronaldheft/arrow-1.0-support
Fixes #62: Sleep data / arrow 1.0 support
2 parents bb23e97 + f3120bd commit c6d43db

6 files changed

Lines changed: 39 additions & 34 deletions

File tree

poetry.lock

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ build-backend = "poetry.core.masonry.api"
2222

2323
[tool.poetry.dependencies]
2424
python = "^3.6 || ^3.7"
25-
arrow = "0.17.0"
25+
arrow = ">=1.0.3"
2626
requests-oauth = ">=0.4.1"
2727
requests-oauthlib = ">=1.2"
2828
typing-extensions = ">=3.7.4.2"

tests/test_common.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ def test_maybe_update_credentials() -> None:
7171

7272
creds1: Final = Credentials(
7373
access_token="my_access_token",
74-
token_expiry=arrow.get("2020-01-01T00:00:00+07:00").timestamp,
74+
token_expiry=arrow.get("2020-01-01T00:00:00+07:00").int_timestamp,
7575
token_type="Bearer",
7676
refresh_token="my_refresh_token",
7777
userid=1,
7878
client_id="CLIENT_ID",
7979
consumer_secret="CONSUMER_SECRET",
8080
)
8181

82-
expires_in = creds1.token_expiry - arrow.utcnow().timestamp
82+
expires_in = creds1.token_expiry - arrow.utcnow().int_timestamp
8383
creds2: Final = Credentials2(
8484
access_token="my_access_token",
8585
expires_in=expires_in,

tests/test_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def test_authorize() -> None:
148148
assert creds.client_id == client_id
149149
assert creds.consumer_secret == consumer_secret
150150
assert creds.expires_in == 11
151-
assert creds.token_expiry == arrow.utcnow().timestamp + 11
151+
assert creds.token_expiry == arrow.utcnow().int_timestamp + 11
152152

153153

154154
@responses.activate

withings_api/__init__.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ def user_get_device(self) -> UserGetDeviceResponse:
9292
def measure_get_activity(
9393
self,
9494
data_fields: Iterable[GetActivityField] = GetActivityField,
95-
startdateymd: Optional[DateType] = None,
96-
enddateymd: Optional[DateType] = None,
95+
startdateymd: Optional[DateType] = arrow.utcnow(),
96+
enddateymd: Optional[DateType] = arrow.utcnow(),
9797
offset: Optional[int] = None,
98-
lastupdate: Optional[DateType] = None,
98+
lastupdate: Optional[DateType] = arrow.utcnow(),
9999
) -> MeasureGetActivityResponse:
100100
"""Get user created activities."""
101101
params: Final[ParamsType] = {}
@@ -120,7 +120,7 @@ def measure_get_activity(
120120
lambda fields: ",".join([field.value for field in fields]),
121121
)
122122
update_params(
123-
params, "lastupdate", lastupdate, lambda val: arrow.get(val).timestamp
123+
params, "lastupdate", lastupdate, lambda val: arrow.get(val).int_timestamp
124124
)
125125
update_params(params, "action", "getactivity")
126126

@@ -132,23 +132,25 @@ def measure_get_meas(
132132
self,
133133
meastype: Optional[MeasureType] = None,
134134
category: Optional[MeasureGetMeasGroupCategory] = None,
135-
startdate: Optional[DateType] = None,
136-
enddate: Optional[DateType] = None,
135+
startdate: Optional[DateType] = arrow.utcnow(),
136+
enddate: Optional[DateType] = arrow.utcnow(),
137137
offset: Optional[int] = None,
138-
lastupdate: Optional[DateType] = None,
138+
lastupdate: Optional[DateType] = arrow.utcnow(),
139139
) -> MeasureGetMeasResponse:
140140
"""Get measures."""
141141
params: Final[ParamsType] = {}
142142

143143
update_params(params, "meastype", meastype, lambda val: val.value)
144144
update_params(params, "category", category, lambda val: val.value)
145145
update_params(
146-
params, "startdate", startdate, lambda val: arrow.get(val).timestamp
146+
params, "startdate", startdate, lambda val: arrow.get(val).int_timestamp
147+
)
148+
update_params(
149+
params, "enddate", enddate, lambda val: arrow.get(val).int_timestamp
147150
)
148-
update_params(params, "enddate", enddate, lambda val: arrow.get(val).timestamp)
149151
update_params(params, "offset", offset)
150152
update_params(
151-
params, "lastupdate", lastupdate, lambda val: arrow.get(val).timestamp
153+
params, "lastupdate", lastupdate, lambda val: arrow.get(val).int_timestamp
152154
)
153155
update_params(params, "action", "getmeas")
154156

@@ -159,16 +161,18 @@ def measure_get_meas(
159161
def sleep_get(
160162
self,
161163
data_fields: Iterable[GetSleepField],
162-
startdate: Optional[DateType] = None,
163-
enddate: Optional[DateType] = None,
164+
startdate: Optional[DateType] = arrow.utcnow(),
165+
enddate: Optional[DateType] = arrow.utcnow(),
164166
) -> SleepGetResponse:
165167
"""Get sleep data."""
166168
params: Final[ParamsType] = {}
167169

168170
update_params(
169-
params, "startdate", startdate, lambda val: arrow.get(val).timestamp
171+
params, "startdate", startdate, lambda val: arrow.get(val).int_timestamp
172+
)
173+
update_params(
174+
params, "enddate", enddate, lambda val: arrow.get(val).int_timestamp
170175
)
171-
update_params(params, "enddate", enddate, lambda val: arrow.get(val).timestamp)
172176
update_params(
173177
params,
174178
"data_fields",
@@ -182,10 +186,10 @@ def sleep_get(
182186
def sleep_get_summary(
183187
self,
184188
data_fields: Iterable[GetSleepSummaryField],
185-
startdateymd: Optional[DateType] = None,
186-
enddateymd: Optional[DateType] = None,
189+
startdateymd: Optional[DateType] = arrow.utcnow(),
190+
enddateymd: Optional[DateType] = arrow.utcnow(),
187191
offset: Optional[int] = None,
188-
lastupdate: Optional[DateType] = None,
192+
lastupdate: Optional[DateType] = arrow.utcnow(),
189193
) -> SleepGetSummaryResponse:
190194
"""Get sleep summary."""
191195
params: Final[ParamsType] = {}
@@ -210,7 +214,7 @@ def sleep_get_summary(
210214
)
211215
update_params(params, "offset", offset)
212216
update_params(
213-
params, "lastupdate", lastupdate, lambda val: arrow.get(val).timestamp
217+
params, "lastupdate", lastupdate, lambda val: arrow.get(val).int_timestamp
214218
)
215219
update_params(params, "action", "getsummary")
216220

@@ -229,18 +233,18 @@ def heart_get(self, signalid: int) -> HeartGetResponse:
229233

230234
def heart_list(
231235
self,
232-
startdate: Optional[DateType] = None,
233-
enddate: Optional[DateType] = None,
236+
startdate: Optional[DateType] = arrow.utcnow(),
237+
enddate: Optional[DateType] = arrow.utcnow(),
234238
offset: Optional[int] = None,
235239
) -> HeartListResponse:
236240
"""Get heart list."""
237241
params: Final[ParamsType] = {}
238242

239243
update_params(
240-
params, "startdate", startdate, lambda val: arrow.get(val).timestamp,
244+
params, "startdate", startdate, lambda val: arrow.get(val).int_timestamp,
241245
)
242246
update_params(
243-
params, "enddate", enddate, lambda val: arrow.get(val).timestamp,
247+
params, "enddate", enddate, lambda val: arrow.get(val).int_timestamp,
244248
)
245249
update_params(params, "offset", offset)
246250
update_params(params, "action", "list")

withings_api/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def validate(cls, value: Any) -> tzinfo:
7979
raise TypeError("string or tzinfo required")
8080

8181

82-
class ArrowType(Arrow): # type: ignore
82+
class ArrowType(Arrow):
8383
"""Subclass of Arrow for parsing dates."""
8484

8585
@classmethod
@@ -605,7 +605,7 @@ class Credentials2(ConfiguredBaseModel):
605605
@property
606606
def token_expiry(self) -> int:
607607
"""Get the token expiry."""
608-
return cast(int, self.created.shift(seconds=self.expires_in).timestamp)
608+
return cast(int, self.created.shift(seconds=self.expires_in).int_timestamp)
609609

610610

611611
CredentialsType = Union[Credentials, Credentials2]
@@ -624,7 +624,7 @@ def maybe_upgrade_credentials(value: CredentialsType) -> Credentials2:
624624
userid=creds.userid,
625625
client_id=creds.client_id,
626626
consumer_secret=creds.consumer_secret,
627-
expires_in=creds.token_expiry - arrow.utcnow().timestamp,
627+
expires_in=creds.token_expiry - arrow.utcnow().int_timestamp,
628628
)
629629

630630

0 commit comments

Comments
 (0)