Created
November 16, 2018 22:28
-
-
Save jtarang/78df670945d53a87a978b2b0e6784760 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from requests import get | |
host = "https://jsonmock.hackerrank.com/api/movies/search/?Title={}" | |
proxies_dict = { | |
"http" : "" | |
} | |
def doGet(url=None, proxy=None): | |
if url is not None: | |
if proxy is not None: | |
proxies_dict['http'] = proxy | |
request = get(url, proxies=proxies_dict) | |
if int(request.status_code) == 200: | |
return request.json() | |
return dict(error=str(request.text)) | |
return dict(error="No url was passed!") | |
def getMovieTitles(title=None): | |
titles = [] | |
if title is not None: | |
target_url = host.format(title) | |
json_resp = doGet(url=target_url) | |
for resp in json_resp['data']: | |
titles.append(resp['Title']) | |
return(sorted(titles, key=len)) | |
return dict(error="Please pass in a title") | |
if __name__ == "__main__": | |
print(getMovieTitles(title="Superman")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I could have created query params @ the request level, but decided to leave the
host
as is .