Advertisement
Guest User

Untitled

a guest
Sep 6th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. """"
  2. TODO:
  3. - Basic error handling should be implemented, but no heroics necessary.
  4. - The URLs for images to be processed should be inputs to the program, as arguments, stdin or read from a file.
  5. - API responses should be logged
  6. - Provide a ZIP/archive of the program code, logged output and resulting images downloaded from the API results.
  7. """
  8.  
  9. from logging import FileHandler
  10. import requests
  11. import logging
  12. import credentials
  13.  
  14.  
  15. # Init Logging
  16. logger = logging.getLogger("LotLinxAPI")
  17. filehandle = logging.FileHandler("LogLinx.log")
  18. format = logging.Formatter("%(asctime)")
  19. logger.addHandler(filehandle)
  20.  
  21. # API Authentication credentials
  22. username = credentials.username
  23. password = credentials.password
  24. session = requests.Session()
  25. session.auth = (username, password)
  26.  
  27. base_url = "https://photoai.lotlinx.com"
  28.  
  29.  
  30. # 1) Submit Requests
  31. def PostRequest(dealerId, vehicleId, imageId, imageUrl):
  32.     endpoint = base_url + "/images/optimize"
  33.     data = [{"dealerId": dealerId,
  34.             "vehicles": [{"id": vehicleId,
  35.                         "images": [{"imageId": imageId, "imageUrl": imageUrl}]}]
  36.             }]
  37.     resp = session.post(url=endpoint, json=data)
  38.     resp.raise_for_status()
  39.     return resp
  40.  
  41.  
  42. # 2) GET REQUEST to query using token
  43. def GetStatus(token):
  44.     endpoint = base_url + f"/images/{token}/status"
  45.     resp = session.get(url=endpoint)
  46.     resp.raise_for_status()
  47.     return resp
  48.  
  49.  
  50. # 2b) GET REQUEST to query using specified status and start date
  51. def GetRequest(status, startDate):
  52.     endpoint = base_url + f"/images/requests?status={status}&startDate={startDate}"
  53.     resp = session.get(url=endpoint)
  54.     resp.raise_for_status()
  55.     return resp
  56.  
  57.  
  58. # 3) LOAD RESPONSE using token
  59. def GetResponse(token):
  60.     endpoint = base_url + f"/images/{token}"
  61.     resp = session.get(url=endpoint)
  62.     resp.raise_for_status()
  63.     return resp
  64.  
  65.  
  66. if __name__ == '__main__':
  67.     # 1) POST
  68.     post = PostRequest(dealerId=9876655, vehicleId=987676, imageId=76554,
  69.                        imageUrl="https://img.lotlinx.com/vdn/7416/jeep_wrangler%20unlimited_2014_1C4BJWFG3EL326863_7416_5_339187295.jpg")
  70.     post = post.json()
  71.     token = post["data"][0]["token"]
  72.     token="51uA18oltVr6S0SdY2EDZhczppOrC4LALCTSyDu8cCslvYVHQLYmwu6hgz2kHTua"
  73.  
  74.     # 2) GET /w Token
  75.     getStat = GetStatus(token)
  76.     # 2b) GET /w status & startdate
  77.     getReq = GetRequest("completed", "2018-09-05T01:22:33")
  78.     getReq = getReq.json()
  79.  
  80.     # 3) GET LOAD RESPONSE
  81.     load = GetResponse(token)
  82.     load = load.json()
  83.     print(load)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement