Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch to the measurements/new endpoint to permit arbitrary dates #89

Merged
merged 5 commits into from May 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,8 +1,8 @@
language: python
python:
- "2.7"
- "3.6"
install:
- pip install --upgrade pip
- pip install -q -e .
script:
- python setup.py test
51 changes: 16 additions & 35 deletions myfitnesspal/client.py
Expand Up @@ -586,13 +586,19 @@ def get_measurements(
return measurements

def set_measurements(
self, measurement='Weight', value=None
self, measurement='Weight', value=None, date=None
):
""" Sets measurement for today's date."""
if value is None:
raise ValueError(
"Cannot update blank value."
)
if date is None:
date = dt.datetime.now().date()
if not isinstance(date, datetime.date):
raise ValueError(
"Date must be a datetime.date object."
)

# get the URL for the main check in page
# this is left in because we need to parse
Expand All @@ -613,43 +619,18 @@ def set_measurements(
# build the update url.
update_url = parse.urljoin(
self.BASE_URL_SECURE,
'measurements/save'
'measurements/new'
)

# setup a dict for the post
data = {}

# here's where we need that required element
data['authenticity_token'] = self._authenticity_token

# Weight has it's own key value pair
if measurement == 'Weight':
data['weight[display_value]'] = value

# the other measurements have generic names with
# an incrementing numeric index.
measurement_index = 0

# iterate all the measurement_ids
for measurement_id in measurement_ids.keys():
# create the measurement_type[n]
# key value pair
n = str(measurement_index)
meas_type = 'measurement_type[' + n + ']'
meas_val = 'measurement_value[' + n + ']'

data[meas_type] = measurement_ids[measurement_id]

# and if it corresponds to the value we want to update
if measurement == measurement_id:
# create the measurement_value[n]
# key value pair and assign it the value.
data[meas_val] = value
else:
# otherwise, create the key value pair and leave it blank
data[meas_val] = ""

measurement_index += 1
data = {
'authenticity_token': self._authenticity_token,
'measurement[display_value]': value,
'type': measurement_ids.get(measurement),
'measurement[entry_date(2i)]': date.month,
'measurement[entry_date(3i)]': date.day,
'measurement[entry_date(1i)]': date.year
}

# now post it.
result = self.session.post(
Expand Down