def upload_activity(self, session, activity): """ Upload an activity on Garmin Support multiple formats """ assert activity.id is None # Upload file as multipart form files = { "file": (activity.filename, activity.open()), } url = '{}/{}'.format(URL_UPLOAD, activity.extension) res = session.post(url, files=files, headers=self.common_headers) # HTTP Status can either be OK or Conflict if res.status_code not in (200, 201, 409): if res.status_code == 412: logger.error('You may have to give explicit consent for uploading files to Garmin') # noqa raise GarminAPIException('Failed to upload {}'.format(activity)) response = res.json()['detailedImportResult'] if len(response["successes"]) == 0: if len(response["failures"]) > 0: if response["failures"][0]["messages"][0]['code'] == 202: # Activity already exists return response["failures"][0]["internalId"], False else: raise GarminAPIException(response["failures"][0]["messages"]) # noqa else: raise GarminAPIException('Unknown error: {}'.format(response)) else: # Upload was successsful return response["successes"][0]["internalId"], True
def set_activity_type(self, session, activity): """ Update the activity type """ assert activity.id is not None assert activity.type is not None # Load the corresponding type key on Garmin Connect types = self.load_activity_types() type_key = types.get(activity.type) if type_key is None: logger.error("Activity type '{}' not valid".format(activity.type)) return False url = '{}/{}'.format(URL_ACTIVITY_TYPE, activity.id) data = { 'value': type_key, } res = session.post(url, data) if not res.ok: raise GarminAPIException('Activity type not set: {}'.format( res.content)) # noqa res = res.json() if "activityType" not in res or res["activityType"]["key"] != type_key: raise GarminAPIException('Activity type not set: {}'.format( res.content)) # noqa
def main(action, filename): assert os.path.exists(filename) if action != "DOWNLOAD": return 0 # Auth with ~/.guploadrc credentials user = User() if not user.authenticate(): logger.error("Invalid Garmin Connect credentials") return -1 # Upload the activity activity = Activity(filename) if not activity.upload(user): logger.error("Failed to send activity to Garmin") return -1 return 0
def main(action, filename): assert os.path.exists(filename) if action != "DOWNLOAD": return 0 # Auth with ~/.guploadrc credentials user = User() if not user.authenticate(): logger.error('Invalid Garmin Connect credentials') return -1 # Upload the activity activity = Activity(filename) if not activity.upload(user): logger.error('Failed to send activity to Garmin') return -1 return 0
def set_activity_type(self, session, activity): """ Update the activity type """ assert activity.id is not None assert activity.type is not None # Load the corresponding type key on Garmin Connect types = self.load_activity_types() type_key = types.get(activity.type) if type_key is None: logger.error("Activity type '{}' not valid".format(activity.type)) return False url = '{}/{}'.format(URL_ACTIVITY_BASE, activity.id) data = {'activityId': activity.id, 'activityTypeDTO': type_key} headers = dict(self.common_headers) # clone headers['X-HTTP-Method-Override'] = 'PUT' # weird. again. res = session.post(url, json=data, headers=headers) if not res.ok: raise GarminAPIException('Activity type not set: {}'.format( res.content)) # noqa