Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import http.client
  2. import json
  3. import time
  4. import timeit
  5. import pickle
  6.  
  7. def get_colors(quantity=5, api_key=None):
  8. conn = http.client.HTTPSConnection("rebrickable.com")
  9. key = api_key
  10. auth_token = {'Authorization': 'key '+key}
  11. payload = "{}"
  12. headers = auth_token
  13. params = '/api/v3/lego/colors/?page=1&page_size='+str(quantity) # modify this so that results are limited by the `quantity` argument.
  14.  
  15. conn.request("GET", params, payload, headers=headers)
  16. response = json.loads(conn.getresponse().read().decode('UTF-8'))["results"]
  17. colors = []
  18.  
  19. for c in response:
  20. colors.append(c['name'])
  21. ################################################################
  22. # insert code to handle data returned in the response #
  23. # return a list of strings, one string for each color returned #
  24. ################################################################
  25.  
  26. return colors
  27.  
  28. # uncomment these next 2 lines to test your implementation
  29. colors = get_colors(quantity=5, api_key='83ce576bdf6440495ba3f11f351a3be7')
  30. print(colors)
  31.  
  32. # The following is sample output of the get_colors() function and is used only to exmplify
  33. # the format of the results for this cell.
  34. # you may comment out or delete the below line.
  35. # print("\n-------------sample-output-------------")
  36. # print(['Fuschia', 'Magenta', 'Violet', 'Pink', 'White'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement