|
| 1 | +from dimo.errors import check_type |
| 2 | + |
| 3 | + |
| 4 | +class VehicleEvents: |
| 5 | + |
| 6 | + def __init__(self, request_method, get_auth_headers): |
| 7 | + self._request = request_method |
| 8 | + self._get_auth_headers = get_auth_headers |
| 9 | + |
| 10 | + def list_all_webhooks(self, developer_jwt: str): |
| 11 | + """ |
| 12 | + Lists all webhooks for a given developer license |
| 13 | + """ |
| 14 | + check_type("developer_jwt", developer_jwt, str) |
| 15 | + url = f"/v1/webhooks" |
| 16 | + return self._request( |
| 17 | + "GET", "VehicleEvents", url, headers=self._get_auth_headers(developer_jwt) |
| 18 | + ) |
| 19 | + |
| 20 | + def register_webhook(self, developer_jwt: str, request: object): |
| 21 | + """ |
| 22 | + Creates a new webhook under the developer license |
| 23 | + """ |
| 24 | + check_type("developer_jwt", developer_jwt, str) |
| 25 | + check_type("request", request, object) |
| 26 | + url = f"/v1/webhooks" |
| 27 | + return self._request( |
| 28 | + "POST", |
| 29 | + "VehicleEvents", |
| 30 | + url, |
| 31 | + headers=self._get_auth_headers(developer_jwt), |
| 32 | + data=request, |
| 33 | + ) |
| 34 | + |
| 35 | + def webhook_signals(self, developer_jwt: str): |
| 36 | + """ |
| 37 | + Fetches the list of signal names available for the data field |
| 38 | + """ |
| 39 | + check_type("developer_jwt", developer_jwt, str) |
| 40 | + url = f"/v1/webhooks/signals" |
| 41 | + return self._request( |
| 42 | + "GET", "VehicleEvents", url, headers=self._get_auth_headers(developer_jwt) |
| 43 | + ) |
| 44 | + |
| 45 | + def list_vehicle_subscriptions(self, developer_jwt: str, token_id: str): |
| 46 | + """ |
| 47 | + Lists all webhooks that a specified vehicle token id is subscribed to |
| 48 | + """ |
| 49 | + check_type("developer_jwt", developer_jwt, str) |
| 50 | + check_type("token_id", token_id, str) |
| 51 | + url = f"/v1/webhooks/vehicles/{token_id}" |
| 52 | + return self._request( |
| 53 | + "GET", "VehicleEvents", url, headers=self._get_auth_headers(developer_jwt) |
| 54 | + ) |
| 55 | + |
| 56 | + def list_vehicle_subscriptions_by_event(self, developer_jwt: str, webhook_id: str): |
| 57 | + """ |
| 58 | + Lists all vehicle subscriptions for a given webhook id |
| 59 | + """ |
| 60 | + check_type("developer_jwt", developer_jwt, str) |
| 61 | + check_type("webhook_id", webhook_id, str) |
| 62 | + url = f"/v1/webhooks/{webhook_id}" |
| 63 | + return self._request( |
| 64 | + "GET", "VehicleEvents", url, headers=self._get_auth_headers(developer_jwt) |
| 65 | + ) |
| 66 | + |
| 67 | + def update_webhook(self, developer_jwt: str, webhook_id: str, request: object): |
| 68 | + """ |
| 69 | + Updates a webhook by a provided webhook id |
| 70 | + """ |
| 71 | + check_type("developer_jwt", developer_jwt, str) |
| 72 | + check_type("webhook_id", webhook_id, str) |
| 73 | + check_type("request", request, object) |
| 74 | + url = f"/v1/webhooks/{webhook_id}" |
| 75 | + return self._request( |
| 76 | + "PUT", |
| 77 | + "VehicleEvents", |
| 78 | + url, |
| 79 | + headers=self._get_auth_headers(developer_jwt), |
| 80 | + data=request, |
| 81 | + ) |
| 82 | + |
| 83 | + def delete_webhook(self, developer_jwt: str, webhook_id: str): |
| 84 | + """ |
| 85 | + Deletes a webhook by a provided webhook id |
| 86 | + """ |
| 87 | + check_type("developer_jwt", developer_jwt, str) |
| 88 | + check_type("webhook_id", webhook_id, str) |
| 89 | + url = f"/v1/webhooks/{webhook_id}" |
| 90 | + return self._request( |
| 91 | + "DELETE", |
| 92 | + "VehicleEvents", |
| 93 | + url, |
| 94 | + headers=self._get_auth_headers(developer_jwt), |
| 95 | + ) |
| 96 | + |
| 97 | + def subscribe_all_vehicles(self, developer_jwt: str, webhook_id: str): |
| 98 | + """ |
| 99 | + Subscribes all vehicles to a specified webhook |
| 100 | + """ |
| 101 | + check_type("developer_jwt", developer_jwt, str) |
| 102 | + check_type("webhook_id", webhook_id, str) |
| 103 | + url = f"/v1/webhooks/{webhook_id}/subscribe/all" |
| 104 | + return self._request( |
| 105 | + "POST", "VehicleEvents", url, headers=self._get_auth_headers(developer_jwt) |
| 106 | + ) |
| 107 | + |
| 108 | + def subscribe_vehicle(self, developer_jwt: str, token_id: str, webhook_id: str): |
| 109 | + """ |
| 110 | + Subscribes a single vehicle to a specified webhook |
| 111 | + """ |
| 112 | + check_type("developer_jwt", developer_jwt, str) |
| 113 | + check_type("token_id", token_id, str) |
| 114 | + check_type("webhook_id", webhook_id, str) |
| 115 | + url = f"/v1/webhooks/{webhook_id}/subscribe/{token_id}" |
| 116 | + return self._request( |
| 117 | + "POST", "VehicleEvents", url, headers=self._get_auth_headers(developer_jwt) |
| 118 | + ) |
| 119 | + |
| 120 | + def unsubscribe_all_vehicles(self, developer_jwt: str, webhook_id: str): |
| 121 | + """ |
| 122 | + Unsubscribes all vehicles from a specified webhook |
| 123 | + """ |
| 124 | + check_type("developer_jwt", developer_jwt, str) |
| 125 | + check_type("webhook_id", webhook_id, str) |
| 126 | + url = f"/v1/webhooks/{webhook_id}/unsubscribe/all" |
| 127 | + return self._request( |
| 128 | + "DELETE", |
| 129 | + "VehicleEvents", |
| 130 | + url, |
| 131 | + headers=self._get_auth_headers(developer_jwt), |
| 132 | + ) |
| 133 | + |
| 134 | + def unsubscribe_vehicle(self, developer_jwt: str, token_id: str, webhook_id: str): |
| 135 | + """ |
| 136 | + Unsubscribes a single vehicle from a specified webhook |
| 137 | + """ |
| 138 | + check_type("developer_jwt", developer_jwt, str) |
| 139 | + check_type("token_id", token_id, str) |
| 140 | + check_type("webhook_id", webhook_id, str) |
| 141 | + url = f"/v1/webhooks/{webhook_id}/unsubscribe/{token_id}" |
| 142 | + return self._request( |
| 143 | + "DELETE", |
| 144 | + "VehicleEvents", |
| 145 | + url, |
| 146 | + headers=self._get_auth_headers(developer_jwt), |
| 147 | + ) |
0 commit comments