Handling HTTP status messages and multiple returns
up vote
0
down vote
favorite
I am new to Python and have written a function which calls an API and does some processing. Please review the code and explain what is the right and pythonic way.
def fetch_storedetails(api_link, zone_id, latitude, longitude):
"""
:param api_link:
:param zone_id:
:param latitude:
:param longitude:
:return:
"""
import requests
store_details = requests.get(
api_link + '/' + str(zone_id) + '/' + str(0) + '/' + str(latitude) + '/' + str(longitude),
headers={'language': 'en'})
print(store_details, store_details)
print(store_details.json)
s_details = store_details.json()
if store_details.status_code == 400:
return "'Sorry , we do not deliver to your selected address at this moment as it’s out of delivery reach."
elif store_details.status_code == 404:
return "Stores are not available in your location"
elif store_details.status_code == 500:
return "Internal server error"
else:
print(s_details)
store_name =
store_address =
banner_image =
store_rating =
store_id =
for i in s_details['data']:
store_name.append(i['businessName'])
store_address.append(i['storeAddr'])
banner_image.append(i['bannerLogos']['bannerimage'])
store_rating.append(i['businessRating'])
store_id.append(i['businessId'])
return store_name, store_address, banner_image, store_rating, store_id
python python-3.x https
New contributor
add a comment |
up vote
0
down vote
favorite
I am new to Python and have written a function which calls an API and does some processing. Please review the code and explain what is the right and pythonic way.
def fetch_storedetails(api_link, zone_id, latitude, longitude):
"""
:param api_link:
:param zone_id:
:param latitude:
:param longitude:
:return:
"""
import requests
store_details = requests.get(
api_link + '/' + str(zone_id) + '/' + str(0) + '/' + str(latitude) + '/' + str(longitude),
headers={'language': 'en'})
print(store_details, store_details)
print(store_details.json)
s_details = store_details.json()
if store_details.status_code == 400:
return "'Sorry , we do not deliver to your selected address at this moment as it’s out of delivery reach."
elif store_details.status_code == 404:
return "Stores are not available in your location"
elif store_details.status_code == 500:
return "Internal server error"
else:
print(s_details)
store_name =
store_address =
banner_image =
store_rating =
store_id =
for i in s_details['data']:
store_name.append(i['businessName'])
store_address.append(i['storeAddr'])
banner_image.append(i['bannerLogos']['bannerimage'])
store_rating.append(i['businessRating'])
store_id.append(i['businessId'])
return store_name, store_address, banner_image, store_rating, store_id
python python-3.x https
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am new to Python and have written a function which calls an API and does some processing. Please review the code and explain what is the right and pythonic way.
def fetch_storedetails(api_link, zone_id, latitude, longitude):
"""
:param api_link:
:param zone_id:
:param latitude:
:param longitude:
:return:
"""
import requests
store_details = requests.get(
api_link + '/' + str(zone_id) + '/' + str(0) + '/' + str(latitude) + '/' + str(longitude),
headers={'language': 'en'})
print(store_details, store_details)
print(store_details.json)
s_details = store_details.json()
if store_details.status_code == 400:
return "'Sorry , we do not deliver to your selected address at this moment as it’s out of delivery reach."
elif store_details.status_code == 404:
return "Stores are not available in your location"
elif store_details.status_code == 500:
return "Internal server error"
else:
print(s_details)
store_name =
store_address =
banner_image =
store_rating =
store_id =
for i in s_details['data']:
store_name.append(i['businessName'])
store_address.append(i['storeAddr'])
banner_image.append(i['bannerLogos']['bannerimage'])
store_rating.append(i['businessRating'])
store_id.append(i['businessId'])
return store_name, store_address, banner_image, store_rating, store_id
python python-3.x https
New contributor
I am new to Python and have written a function which calls an API and does some processing. Please review the code and explain what is the right and pythonic way.
def fetch_storedetails(api_link, zone_id, latitude, longitude):
"""
:param api_link:
:param zone_id:
:param latitude:
:param longitude:
:return:
"""
import requests
store_details = requests.get(
api_link + '/' + str(zone_id) + '/' + str(0) + '/' + str(latitude) + '/' + str(longitude),
headers={'language': 'en'})
print(store_details, store_details)
print(store_details.json)
s_details = store_details.json()
if store_details.status_code == 400:
return "'Sorry , we do not deliver to your selected address at this moment as it’s out of delivery reach."
elif store_details.status_code == 404:
return "Stores are not available in your location"
elif store_details.status_code == 500:
return "Internal server error"
else:
print(s_details)
store_name =
store_address =
banner_image =
store_rating =
store_id =
for i in s_details['data']:
store_name.append(i['businessName'])
store_address.append(i['storeAddr'])
banner_image.append(i['bannerLogos']['bannerimage'])
store_rating.append(i['businessRating'])
store_id.append(i['businessId'])
return store_name, store_address, banner_image, store_rating, store_id
python python-3.x https
python python-3.x https
New contributor
New contributor
edited 1 hour ago
Jamal♦
30.2k11115226
30.2k11115226
New contributor
asked 3 hours ago
Rohit Haritash
11
11
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
- If you're going to drop in a comment block for function documentation, fill it out. Otherwise, delete it.
- Generally you shouldn't
import
on the inside of a function.import
at the top of a file. - Your constructed URL shouldn't use a series of
+
. You should be using the%
formatting operator orstr.format()
. This will also avoid callingstr(0)
. - None of your
else
s are necessary. You return before each of them, so you can simply continue writing the rest of the function afterward. - If I were you, I would simply return
s_details['data']
. Decomposing the payload into a series of lists isn't really useful, and if it is indeed useful for your application, it should be done in a separate function. - Returning an error string to indicate an error is a bad idea. You should
raise
an exception instead.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
- If you're going to drop in a comment block for function documentation, fill it out. Otherwise, delete it.
- Generally you shouldn't
import
on the inside of a function.import
at the top of a file. - Your constructed URL shouldn't use a series of
+
. You should be using the%
formatting operator orstr.format()
. This will also avoid callingstr(0)
. - None of your
else
s are necessary. You return before each of them, so you can simply continue writing the rest of the function afterward. - If I were you, I would simply return
s_details['data']
. Decomposing the payload into a series of lists isn't really useful, and if it is indeed useful for your application, it should be done in a separate function. - Returning an error string to indicate an error is a bad idea. You should
raise
an exception instead.
add a comment |
up vote
0
down vote
- If you're going to drop in a comment block for function documentation, fill it out. Otherwise, delete it.
- Generally you shouldn't
import
on the inside of a function.import
at the top of a file. - Your constructed URL shouldn't use a series of
+
. You should be using the%
formatting operator orstr.format()
. This will also avoid callingstr(0)
. - None of your
else
s are necessary. You return before each of them, so you can simply continue writing the rest of the function afterward. - If I were you, I would simply return
s_details['data']
. Decomposing the payload into a series of lists isn't really useful, and if it is indeed useful for your application, it should be done in a separate function. - Returning an error string to indicate an error is a bad idea. You should
raise
an exception instead.
add a comment |
up vote
0
down vote
up vote
0
down vote
- If you're going to drop in a comment block for function documentation, fill it out. Otherwise, delete it.
- Generally you shouldn't
import
on the inside of a function.import
at the top of a file. - Your constructed URL shouldn't use a series of
+
. You should be using the%
formatting operator orstr.format()
. This will also avoid callingstr(0)
. - None of your
else
s are necessary. You return before each of them, so you can simply continue writing the rest of the function afterward. - If I were you, I would simply return
s_details['data']
. Decomposing the payload into a series of lists isn't really useful, and if it is indeed useful for your application, it should be done in a separate function. - Returning an error string to indicate an error is a bad idea. You should
raise
an exception instead.
- If you're going to drop in a comment block for function documentation, fill it out. Otherwise, delete it.
- Generally you shouldn't
import
on the inside of a function.import
at the top of a file. - Your constructed URL shouldn't use a series of
+
. You should be using the%
formatting operator orstr.format()
. This will also avoid callingstr(0)
. - None of your
else
s are necessary. You return before each of them, so you can simply continue writing the rest of the function afterward. - If I were you, I would simply return
s_details['data']
. Decomposing the payload into a series of lists isn't really useful, and if it is indeed useful for your application, it should be done in a separate function. - Returning an error string to indicate an error is a bad idea. You should
raise
an exception instead.
answered 1 hour ago
Reinderien
932415
932415
add a comment |
add a comment |
Rohit Haritash is a new contributor. Be nice, and check out our Code of Conduct.
Rohit Haritash is a new contributor. Be nice, and check out our Code of Conduct.
Rohit Haritash is a new contributor. Be nice, and check out our Code of Conduct.
Rohit Haritash is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207851%2fhandling-http-status-messages-and-multiple-returns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown