Reputation: 141
I have this function whch communicates with SWAPI API and I want to get species name for the movie.
So let's say I search 'New Hope', I have to get ['Hutt','Wookiee','Droid','Human','Rodian']
.
The problem is that in the SWAPI dict, you don't get the names ,you get a list of urls.
"species": [
"https://swapi.co/api/species/5/",
"https://swapi.co/api/species/3/",
"https://swapi.co/api/species/2/",
"https://swapi.co/api/species/1/",
"https://swapi.co/api/species/4/"
],
This is my conde so far:
@api_view(['GET'])
def search_films(request,title):
context = {}
species = []
url = "https://swapi.co/api/?search=" + str(title)
if request.method == "GET":
r = requests.get(url)
if r.status_code == 200:
data = r.json()
species = data['results'][0]['species']
if species:
for species_url in species:
get_request = requests.get(species_url)
response_data = get_request.json()
species.append(response_data)
context['species'] = response_data
print(context)
return Response(data, status=status.HTTP_200_OK)
else:
return Response({"error": "Request failed"}, status=r.status_code)
else:
return Response({"error": "Method not allowed"}, status=status.HTTP_400_BAD_REQUEST)
At this point I get an error:
raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for '{'name': 'Hutt', 'classification': 'gastropod', 'designation': 'sentient', 'average_height': '300', 'skin_colors': 'green, brown, tan', 'hair_colors': 'n/a', 'eye_colors': 'yellow, red', 'average_lifespan': '1000', 'homeworld': 'https://swapi.co/api/planets/24/', 'language': 'Huttese', 'people': ['https://swapi.co/api/people/16/'], 'films': ['https://swapi.co/api/films/3/', 'https://swapi.co/api/films/1/'], 'created': '2014-12-10T17:12:50.410000Z', 'edited': '2014-12-20T21:36:42.146000Z', 'url': 'https://swapi.co/api/species/5/'}'
Any ideas of how to implement this?
Upvotes: 0
Views: 360
Reputation: 1601
Well, take a look at your line species = []
You expect that there will be only urls, right? But it's not so, because you're appending to this list response data on this line: species.append(response_data)
. During your script execution it becomes:
['https://swapi.co/api/species/5/',
'https://swapi.co/api/species/3/',
'https://swapi.co/api/species/2/',
'https://swapi.co/api/species/1/',
'https://swapi.co/api/species/4/',
{'name': 'Hutt', ....},
{'name': 'Wookiee', ....}]
Because list is mutable data type. And on some iteration you're trying to fetch not url, but dictionary
I've made an attempt to fix your code renaming spices at the beginning of function to species_data
and using this variable just for storing response from api(json response). spices urls are still in spices
variable. I'm not sure if return Response(species_data, status=status.HTTP_200_OK)
is correct logic for you, but you should get the idea. You need 2 different variables for urls and response data.
@api_view(['GET'])
def search_films(request,title):
context = {}
species_data = []
url = "https://swapi.co/api/?search=" + str(title)
if request.method == "GET":
r = requests.get(url)
if r.status_code == 200:
data = r.json()
species = data['results'][0]['species']
if species:
for species_url in species:
get_request = requests.get(species_url)
response_data = get_request.json()
species_data.append(response_data)
context['species'] = response_data
print(context)
return Response(species_data, status=status.HTTP_200_OK)
else:
return Response({"error": "Request failed"}, status=r.status_code)
else:
return Response({"error": "Method not allowed"}, status=status.HTTP_400_BAD_REQUEST)
Upvotes: 4