You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					47 lines
				
				1.5 KiB
			
		
		
			
		
	
	
					47 lines
				
				1.5 KiB
			| 
								 
											6 years ago
										 
									 | 
							
								import jwt
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								import os
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								import requests
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								from datetime import datetime, timedelta, UTC
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.system.hardware.hw import Paths
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.system.version import get_version
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								API_HOST = os.getenv('API_HOST', 'https://api.commadotai.com')
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								class Api:
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  def __init__(self, dongle_id):
							 | 
						||
| 
								 | 
							
								    self.dongle_id = dongle_id
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    with open(Paths.persist_root()+'/comma/id_rsa') as f:
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      self.private_key = f.read()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def get(self, *args, **kwargs):
							 | 
						||
| 
								 | 
							
								    return self.request('GET', *args, **kwargs)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def post(self, *args, **kwargs):
							 | 
						||
| 
								 | 
							
								    return self.request('POST', *args, **kwargs)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def request(self, method, endpoint, timeout=None, access_token=None, **params):
							 | 
						||
| 
								 | 
							
								    return api_get(endpoint, method=method, timeout=timeout, access_token=access_token, **params)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								  def get_token(self, expiry_hours=1):
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								    now = datetime.now(UTC).replace(tzinfo=None)
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    payload = {
							 | 
						||
| 
								 | 
							
								      'identity': self.dongle_id,
							 | 
						||
| 
								 | 
							
								      'nbf': now,
							 | 
						||
| 
								 | 
							
								      'iat': now,
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								      'exp': now + timedelta(hours=expiry_hours)
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    token = jwt.encode(payload, self.private_key, algorithm='RS256')
							 | 
						||
| 
								 | 
							
								    if isinstance(token, bytes):
							 | 
						||
| 
								 | 
							
								      token = token.decode('utf8')
							 | 
						||
| 
								 | 
							
								    return token
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								def api_get(endpoint, method='GET', timeout=None, access_token=None, **params):
							 | 
						||
| 
								 | 
							
								  headers = {}
							 | 
						||
| 
								 | 
							
								  if access_token is not None:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    headers['Authorization'] = "JWT " + access_token
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  headers['User-Agent'] = "openpilot-" + get_version()
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  return requests.request(method, API_HOST + "/" + endpoint, timeout=timeout, headers=headers, params=params)
							 |