From cbeb0c9145787f7b0413986ae01210864db56c34 Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 27 Dec 2021 09:41:41 -0600 Subject: [PATCH] Feature: Add vehicle action handler (#46) * Add new regions (#43) * Add new regions * Update README.md * Feature: Add vehicle action handler (#45) * Add new regions * Update README.md * Add new regions (#43) (#44) * Add new regions * Update README.md * Update vehicle.py * Update vehicle.py --- connectedcar/vehicle.py | 105 ++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 52 deletions(-) diff --git a/connectedcar/vehicle.py b/connectedcar/vehicle.py index a01f8e7..841af9d 100644 --- a/connectedcar/vehicle.py +++ b/connectedcar/vehicle.py @@ -1,5 +1,6 @@ from .api import Api from . import const +import time class Vehicle(object): @@ -333,19 +334,7 @@ def start(self): """ - job_id = self.api.action( - 'PUT', - const.API_URL, - 'vehicles/v2/' + - self.vehicle_id + - '/engine/start').json()['commandId'] - response = self.api.action( - 'GET', - const.API_URL, - 'vehicles/' + - self.vehicle_id + - '/engine/start/' + - job_id) + response = self.action_handler('/engine/start/', 'PUT') return response.json() def stop(self): @@ -359,19 +348,7 @@ def stop(self): """ - job_id = self.api.action( - 'DELETE', - const.API_URL, - 'vehicles/v2/' + - self.vehicle_id + - '/engine/start').json()['commandId'] - response = self.api.action( - 'GET', - const.API_URL, - 'vehicles/' + - self.vehicle_id + - '/engine/start/' + - job_id) + response = self.action_handler('/engine/start/', 'DELETE') return response.json() def lock(self): @@ -385,19 +362,7 @@ def lock(self): """ - job_id = self.api.action( - 'PUT', - const.API_URL, - 'vehicles/v2/' + - self.vehicle_id + - '/doors/lock').json()['commandId'] - response = self.api.action( - 'GET', - const.API_URL, - 'vehicles/' + - self.vehicle_id + - '/doors/lock/' + - job_id) + response = self.action_handler('/doors/lock/', 'PUT') return response.json() def unlock(self): @@ -411,17 +376,53 @@ def unlock(self): """ - job_id = self.api.action( - 'DELETE', - const.API_URL, - 'vehicles/v2/' + - self.vehicle_id + - '/doors/lock').json()['commandId'] - response = self.api.action( - 'GET', - const.API_URL, - 'vehicles/' + - self.vehicle_id + - '/doors/lock/' + - job_id) + response = self.action_handler('/doors/lock/', 'DELETE') return response.json() + + """ PUT/DELETE Vehicle.action_handler + + Returns: + Response: returns response from the action request to the Ford API + + Raises: + Exception + + """ + def action_handler(self, context, method): + if (method == "PUT" or method == "DELETE"): + job_id = self.api.action(method, const.API_URL, 'vehicles/v2/' + self.vehicle_id + context).json()['commandId'] + if (job_id): + return self.action_status_check(context, job_id) + else: + raise Exception("No job id returned") + + + """ GET Vehicle.action_status_check + + Returns: + Response: returns the current status of the action request + + Raises: + Exception + + """ + def action_status_check(self, context, job_id): + success = False + attempts = 0 + while (not success): + response = self.api.action( + 'GET', + const.API_URL, + 'vehicles/' + + self.vehicle_id + + context + + job_id) + if (response.json()['status'] == 200): + success = True + return response + else: + attempts += 1 + if (attempts >= 30): + raise Exception("Timeout waiting for action to complete") + time.sleep(1) +