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.
		
		
		
		
			
				
					38 lines
				
				1.1 KiB
			
		
		
			
		
	
	
					38 lines
				
				1.1 KiB
			| 
								 
											6 years ago
										 
									 | 
							
								import sys
							 | 
						||
| 
								 | 
							
								import os
							 | 
						||
| 
								 | 
							
								import requests
							 | 
						||
| 
								 | 
							
								from tools.lib.auth_config import clear_token
							 | 
						||
| 
								 | 
							
								API_HOST = os.getenv('API_HOST', 'https://api.commadotai.com')
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class CommaApi():
							 | 
						||
| 
								 | 
							
								  def __init__(self, token=None):
							 | 
						||
| 
								 | 
							
								    self.session = requests.Session()
							 | 
						||
| 
								 | 
							
								    self.session.headers['User-agent'] = 'OpenpilotTools'
							 | 
						||
| 
								 | 
							
								    if token:
							 | 
						||
| 
								 | 
							
								      self.session.headers['Authorization'] = 'JWT ' + token
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def request(self, method, endpoint, **kwargs):
							 | 
						||
| 
								 | 
							
								    resp = self.session.request(method, API_HOST + '/' + endpoint, **kwargs)
							 | 
						||
| 
								 | 
							
								    resp_json = resp.json()
							 | 
						||
| 
								 | 
							
								    if isinstance(resp_json, dict) and resp_json.get('error'):
							 | 
						||
| 
								 | 
							
								      if resp.status_code == 401:
							 | 
						||
| 
								 | 
							
								        clear_token()
							 | 
						||
| 
								 | 
							
								        raise UnauthorizedError('Unauthorized. Authenticate with tools/lib/auth.py')
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      e = APIError(str(resp.status_code) + ":" + resp_json.get('description', str(resp_json['error'])))
							 | 
						||
| 
								 | 
							
								      e.status_code = resp.status_code
							 | 
						||
| 
								 | 
							
								      raise e
							 | 
						||
| 
								 | 
							
								    return resp_json
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def get(self, endpoint, **kwargs):
							 | 
						||
| 
								 | 
							
								    return self.request('GET', endpoint, **kwargs)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  def post(self, endpoint, **kwargs):
							 | 
						||
| 
								 | 
							
								    return self.request('POST', endpoint, **kwargs)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class APIError(Exception):
							 | 
						||
| 
								 | 
							
								  pass
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class UnauthorizedError(Exception):
							 | 
						||
| 
								 | 
							
								  pass
							 |