예제 #1
0
    def sg_get_api_output(self, URL, attempts=10, wait_sec=3):
        '''Takes (in future) tokens, preferences, returns JSON file if successful
        after specified # of attempts.
        ints -> dict
        '''
        from time import sleep
        attempt_count = 0

        for i in range(0, attempts):
            try:
                attempt_count += 1
                output = requests.get(URL, verify=common.get_cert_path())
                if output.ok:
                    output = output.json()
                    print("Success. Stored API output in json dict.")
                    return output
            except KeyboardInterrupt:
                pass
            except:
                if attempt_count >= attempts:
                    print("All attempts failed")
                    return
                print("Likely SSLError. Trying again in", wait_sec,
                      "second(s)...")
                sleep(wait_sec)
예제 #2
0
    def sg_emails_json(self,
                       surveyID,
                       campaign_id,
                       api_token,
                       attempts=10,
                       wait_sec=3):
        '''Takes  campaign id and api tokens and returns
        json-formatted dict with email messages.
        int, str, -> dict
        '''

        from time import sleep
        attempt_count = 0
        URL = "https://restapica.surveygizmo.com/v5/survey/" + str(
            surveyID) + "/surveycampaign/" + str(
                campaign_id) + "/emailmessage/" + "?" + api_token
        print(URL)
        for i in range(0, attempts):
            try:
                attempt_count += 1
                output = requests.get(URL, verify=common.get_cert_path())
                if output.ok:
                    output = output.json()
                    print("Success. Stored API output in json dict.")
                    return output
            except KeyboardInterrupt:
                pass
            except:
                if attempt_count >= attempts:
                    print("All attempts failed")
                    return
                print("Likely SSLError. Trying again in", wait_sec,
                      "second(s)...")
                sleep(wait_sec)
예제 #3
0
    def sg_campaigns_json(self, surveyID, api_token, attempts=10, wait_sec=3):
        '''Takes Sgizmo surveyID, api token and returns
        campaigns as dataframe.
        int, str, -> dict
        '''

        attempt_count = 0
        URL = "https://restapica.surveygizmo.com/v5/survey/" + str(
            surveyID) + "/surveycampaign/?resultsperpage=500&" + api_token
        print(URL)
        for i in range(0, attempts):
            try:
                attempt_count += 1
                output = requests.get(URL, verify=common.get_cert_path())
                if output.ok:
                    output = output.json()
                    print("Success. Stored API output in json dict.")
                    return output
            except KeyboardInterrupt:
                pass
            except:
                if attempt_count >= attempts:
                    print("All attempts failed")
                    return
                print("Likely SSLError. Trying again in", wait_sec,
                      "second(s)...")
                sleep(wait_sec)
예제 #4
0
    def get_list_json(self, api_token, attempts=6, wait_sec=3):
        """
        Takes str api token and returns all surveys
        associated with account (in json-like dict).
        str -> dict
        """

        attempt_count = 0
        URL = "https://restapica.surveygizmo.com/v5/survey/?resultsperpage=500&" + str(
            api_token)
        for i in range(0, attempts):
            try:
                if attempt_count == 0:
                    print(URL)
                attempt_count += 1
                output = requests.get(URL, verify=common.get_cert_path())
                if output.ok:
                    output = output.json()
                    print("Success. Stored API output in json dict.")
                    return output
            except KeyboardInterrupt:
                pass
            except Exception as ex:
                if attempt_count >= attempts:
                    print("All attempts failed")
                    return
                print("Likely SSLError. Trying again in", wait_sec,
                      "second(s)...", ex)
                sleep(wait_sec)
예제 #5
0
    def sg_get_api_output_answers(self, attempts, wait_sec, surveyID,
                                  api_token):
        '''Takes (in future) tokens, preferences, returns JSON file if successful
        after specified # of attempts.
        ints -> dict
        '''
        from time import sleep

        attempt_count = 0
        page_cnt = 1
        which_page = 1
        result_pages = []
        URL = sg_responses.create_response_API_URL(surveyID,
                                                   api_token,
                                                   resultsperpage=200,
                                                   page=which_page)
        print(URL)
        for i in range(0, attempts):
            try:

                output = requests.get(URL, verify=common.get_cert_path())
                if output.ok:
                    output = output.json()
                    result_pages.append(output)
                    print("Success. Stored API output in json dict.")
                    resultsperpage = int(output["results_per_page"])
                    totalresults = int(output["total_count"])
                    if totalresults > resultsperpage:
                        print("total exceeds results_per_page")
                    page_cnt = output["total_pages"]
                    print("Final page count will be", page_cnt)
                    if page_cnt > 1:
                        for i in range(which_page, page_cnt):
                            print("Making call to API for another page")
                            which_page += 1
                            URL = sg_responses.create_response_API_URL(
                                surveyID,
                                api_token,
                                resultsperpage=200,
                                page=which_page)
                            resultpage = sg_responses.sg_get_api_output(
                                URL, 10, 3)
                            result_pages.append(resultpage)

                    print("Success. All results stored in dict(s).")
                    if len(result_pages) == 1:
                        print("Output: Single dict")
                        return output
                    elif len(result_pages) > 1:
                        print("Output: List of dicts")
                        return result_pages
            except KeyboardInterrupt:
                pass
            except:
                attempt_count += 1
                if attempt_count >= attempts:
                    print("All attempts failed")
                    return
                print("Likely SSLError. Trying again in", wait_sec,
                      "second(s)...")
                sleep(wait_sec)
예제 #6
0
    def sg_status_json(self, surveyID, campaignID, api_token, attempts=5, wait_sec=3):

        attempt_count = 0
        which_page = 1
        pg_cnt = 1
        URL = "https://restapica.surveygizmo.com/v5/survey/" \
              + str(surveyID) \
              + "/surveycampaign/" \
              + str(campaignID) \
              + "/surveycontact"\
              + "/?resultsperpage=300&" \
              + "page=" + str(pg_cnt) + "&"\
              + api_token
        pages = []

        for i in range(0, attempts):
            try:
                attempt_count += 1
                output = requests.get(URL, verify=common.get_cert_path())
                if output.ok:
                    output = output.json()
                    pages.append(output)
                    pg_cnt = output["total_pages"]
                    print("Success. Stored API output in json dict.")

                    if pg_cnt > 1:
                        for i in range(which_page, pg_cnt):
                            which_page += 1
                            last_page = which_page - 1
                            replace_this = "page=" + str(last_page) + "&"
                            with_this = "page=" + str(which_page) + "&"
                            URL = URL.replace(replace_this, with_this)
                            output = requests.get(URL, verify=common.get_cert_path())
                            if output.ok:
                                output = output.json()
                            pages.append(output)

                    print("Success. All results stored in dict(s).")
                    if len(pages) == 1:
                        print("Output: Single dict")
                        return output
                    elif len(pages) > 1:
                        print("Output: List of dicts")
                        return pages

                    return output

                # if output.ok:
                #     output = output.json()
                #     result_pages.append(output)
                #     print("Success. Stored API output in json dict.")
                #     resultsperpage = int(output["results_per_page"])
                #     totalresults = int(output["total_count"])
                #     if totalresults > resultsperpage:
                #         print("total exceeds results_per_page")
                #     page_cnt = output["total_pages"]
                #     print("Final page count will be", page_cnt)
                #     if page_cnt > 1:
                #         for i in range(which_page, page_cnt):
                #             print("Making call to API for another page")
                #             which_page += 1
                #             URL = sg_responses.create_response_API_URL(surveyID, api_token, resultsperpage=100, page=which_page)
                #             resultpage = sg_responses.sg_get_api_output(URL, 10, 3)
                #             result_pages.append(resultpage)







            except KeyboardInterrupt:
                pass
            except:
                if attempt_count >= attempts:
                    print("All attempts failed")
                    return
                print("Likely SSLError. Trying again in", wait_sec, "second(s)...")
                time.sleep(wait_sec)