Handling HTTP status messages and multiple returns











up vote
2
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









share|improve this question




























    up vote
    2
    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









    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      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









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 at 7:02









      Jamal

      30.2k11115226




      30.2k11115226










      asked Nov 17 at 5:16









      Rohit Haritash

      112




      112






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          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 or str.format(). This will also avoid calling str(0).

          • None of your elses 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.






          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            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 or str.format(). This will also avoid calling str(0).

            • None of your elses 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.






            share|improve this answer

























              up vote
              1
              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 or str.format(). This will also avoid calling str(0).

              • None of your elses 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.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                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 or str.format(). This will also avoid calling str(0).

                • None of your elses 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.






                share|improve this answer













                • 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 or str.format(). This will also avoid calling str(0).

                • None of your elses 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.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 17 at 6:48









                Reinderien

                1,362516




                1,362516






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Сан-Квентин

                    8-я гвардейская общевойсковая армия

                    Алькесар