diff --git a/docs/action_network.rst b/docs/action_network.rst index a8056c218d..c23162c01c 100644 --- a/docs/action_network.rst +++ b/docs/action_network.rst @@ -99,6 +99,148 @@ You can then call various endpoints: # Get a specific wrapper specific_wrapper = an.get_wrapper('wrapper_id') + + # Get all surveys + all_surveys = an.get_surveys() + + # Get a specific survey + specific_survey = an.get_survey('survey_id') + + # Create a survey + survey_payload = { + "title": "My Free Survey", + "origin_system": "FreeSurveys.com" + } + created_survey = an.create_survey(survey_payload) + + created_survey = { + "identifiers": [ + "action_network:1234" + ], + "created_date": "2014-03-26T15:26:30Z", + "modified_date": "2014-03-26T15:26:30Z", + "title": "My Free Survey", + "total_responses": 0, + "origin_system": "FreeSurveys.com", + "action_network:hidden": false, + "_embedded": { + "osdi:creator": { + "given_name": "John", + "family_name": "Doe", + "created_date": "2014-03-20T21:04:31Z", + "modified_date": "2014-03-20T21:04:31Z", + "identifiers": [ + "action_network:1234" + ], + "email_addresses": [ + { + "primary": true, + "address": "jdoe@mail.com", + "status": "subscribed" + } + ], + "phone_numbers": [ + { + "primary": true, + "number": "12021234444", + "number_type": "Mobile", + "status": "subscribed" + } + ], + "postal_addresses": [ + { + "primary": true, + "address_lines": [ + "1600 Pennsylvania Ave" + ], + "locality": "Washington", + "region": "DC", + "postal_code": "20009", + "country": "US", + "language": "en", + "location": { + "latitude": 32.249, + "longitude": -73.0339, + "accuracy": "Approximate" + } + } + ], + "languages_spoken": [ + "en" + ], + "_links": { + "self": { + "href": "https://actionnetwork.org/api/v2/people/1234" + }, + "osdi:attendances": { + "href": "https://actionnetwork.org/api/v2/people/1234/attendances" + }, + "osdi:signatures": { + "href": "https://actionnetwork.org/api/v2/people/1234/signatures" + }, + "osdi:submissions": { + "href": "https://actionnetwork.org/api/v2/people/1234/submissions" + }, + "osdi:donations": { + "href": "https://actionnetwork.org/api/v2/people/1234/donations" + }, + "osdi:outreaches": { + "href": "https://actionnetwork.org/api/v2/people/1234/outreaches" + }, + "osdi:taggings": { + "href": "https://actionnetwork.org/api/v2/people/1234/taggings" + }, + "action_network:responses": { + "href": "https://actionnetwork.org/api/v2/people/1234/responses" + }, + "curies": [ + { + "name": "osdi", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": true + }, + { + "name": "action_network", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": true + } + ] + } + } + }, + "_links": { + "osdi:creator": { + "href": "https://actionnetwork.org/api/v2/people/1234" + }, + "self": { + "href": "https://actionnetwork.org/api/v2/surveys/1234" + }, + "action_network:responses": { + "href": "https://actionnetwork.org/api/v2/surveys/1234/responses" + }, + "action_network:record_response_helper": { + "href": "https://actionnetwork.org/api/v2/surveys/1234/responses" + }, + "action_network:embed": { + "href": "https://actionnetwork.org/api/v2/surveys/1234/embed" + }, + "curies": [ + { + "name": "osdi", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": true + }, + { + "name": "action_network", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": true + } + ] + } + } + + # Update a survey + updated_survey = an.update_survey('survey_id', survey_payload) *********** SQL Mirror diff --git a/parsons/action_network/action_network.py b/parsons/action_network/action_network.py index 8c74da6486..ddf3d48731 100644 --- a/parsons/action_network/action_network.py +++ b/parsons/action_network/action_network.py @@ -1788,6 +1788,80 @@ def update_submission(self, form_id, submission_id, data): f"forms/{form_id}/submissions/{submission_id}", data=json.dumps(data) ) + # Surveys + def get_surveys(self, limit=None, per_page=25, page=None, filter=None): + """ + Survey resources are sometimes presented as collections of surveys. + For example, calling the surveys endpoint will return a collection + of all the surveys associated with your API key. + `Args:` + limit: + The number of entries to return. When None, returns all entries. + per_page: + The number of entries per page to return. 25 maximum. + `Returns:` + """ + if page: + return self._get_page("surveys", page, per_page, filter) + return self._get_entry_list("surveys", limit, per_page, filter) + + def get_survey(self, survey_id): + """ + `Args:` + survey_id: + The unique id of the survey + `Returns:` + A JSON with the survey entry + `Documentation Reference`: + https://actionnetwork.org/docs/v2/surveys + """ + return self.api.get_request(f"surveys/{survey_id}") + + def create_survey(self, data): + """ + `Args:` + data: + The payload for creating the survey + { + "title": "My Free Survey", + "origin_system": "FreeSurveys.com" + } + OR + The payload for creating the survey with a creator link + { + "title": "My Free Survey", + "origin_system": "FreeSurveys.com" + "_links" : { + "osdi:creator" : { + "href" : "https://actionnetwork.org/api/v2/people/[person_id]" + } + } + } + `Returns:` + A JSON with the created survey entry + `Documentation Reference`: + https://actionnetwork.org/docs/v2/surveys + """ + return self.api.post_request("surveys", data=json.dumps(data)) + + def update_survey(self, survey_id, data): + """ + `Args:` + survey_id: + The unique id of the survey + data: + The payload for updating the survey + { + "title": "My Free Survey", + "origin_system": "FreeSurveys.com", + } + `Returns:` + A JSON with the updated survey entry + `Documentation Reference`: + https://actionnetwork.org/docs/v2/surveys + """ + return self.api.post_request(f"surveys/{survey_id}", data=json.dumps(data)) + # Tags def get_tags(self, limit=None, per_page=None): """ diff --git a/test/test_action_network/test_action_network.py b/test/test_action_network/test_action_network.py index e1d2fd9e0e..3404dffa60 100644 --- a/test/test_action_network/test_action_network.py +++ b/test/test_action_network/test_action_network.py @@ -3006,6 +3006,348 @@ def setUp(self, m): }, } + # Surveys + self.fake_surveys = { + "total_pages": 7, + "per_page": 25, + "page": 1, + "total_records": 162, + "_links": { + "next": {"href": "https://actionnetwork.org/api/v2/surveys?page=2"}, + "self": {"href": "https://actionnetwork.org/api/v2/surveys"}, + "action_network:surveys": [ + {"href": "https://actionnetwork.org/api/v2/surveys/123"}, + {"href": "https://actionnetwork.org/api/v2/surveys/123"}, + # truncated for brevity + ], + "curies": [ + { + "name": "osdi", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": True, + }, + { + "name": "action_network", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": True, + }, + ], + }, + "_embedded": { + "action_network:surveys": [ + { + "identifiers": ["action_network:123"], + "origin_system": "Action Network", + "created_date": "2014-03-24T18:03:45Z", + "modified_date": "2014-03-25T15:00:22Z", + "title": "Tell us about yourself!", + "description": "
Tell us a bit more about yourself.
", + "call_to_action": "Let us know", + "browser_url": "https://actionnetwork.org/surveys/my-survey", + "featured_image_url": "https://actionnetwork.org/images/my-image.jpg", + "total_responses": 2354, + "action_network:hidden": False, + "_embedded": { + "osdi:creator": { + "given_name": "John", + "family_name": "Doe", + "identifiers": ["action_network:123"], + "created_date": "2014-03-24T18:03:45Z", + "modified_date": "2014-03-25T15:00:22Z", + "email_addresses": [ + { + "primary": True, + "address": "jdoe@mail.com", + "status": "subscribed", + } + ], + "phone_numbers": [ + { + "primary": True, + "number": "12021234444", + "number_type": "Mobile", + "status": "subscribed", + } + ], + "postal_addresses": [ + { + "primary": True, + "address_lines": ["1600 Pennsylvania Ave."], + "locality": "Washington", + "region": "DC", + "postal_code": "20009", + "country": "US", + "language": "en", + "location": { + "latitude": 35.919, + "longitude": -72.0379, + "accuracy": "Approximate", + }, + } + ], + "languages_spoken": ["en"], + "_links": { + "self": {"href": f"{self.api_url}/api/v2/people/123"}, + "osdi:attendances": { + "href": f"{self.api_url}/api/v2/people/123/attendances" + }, + "osdi:signatures": { + "href": f"{self.api_url}/api/v2/people/123/signatures" + }, + "osdi:submissions": { + "href": f"{self.api_url}/api/v2/people/123/submissions" + }, + "osdi:donations": { + "href": f"{self.api_url}/api/v2/people/123/donations" + }, + "osdi:outreaches": { + "href": f"{self.api_url}/api/v2/people/123/outreaches" + }, + "osdi:taggings": { + "href": f"{self.api_url}/api/v2/people/123/taggings" + }, + "action_network:responses": { + "href": f"{self.api_url}/api/v2/people/123/responses" + }, + }, + } + }, + "_links": { + "self": {"href": f"{self.api_url}/api/v2/surveys/123"}, + "action_network:responses": { + "href": f"{self.api_url}/api/v2/surveys/123/responses" + }, + "action_network:record_response_helper": { + "href": f"{self.api_url}/api/v2/surveys/123/responses" + }, + "osdi:creator": {"href": f"{self.api_url}/api/v2/people/123"}, + "action_network:embed": { + "href": f"{self.api_url}/api/v2/surveys/123/embed" + }, + }, + }, + { + "identifiers": ["action_network:123", "foreign_system:1"], + "origin_system": "Another System", + "created_date": "2014-03-14T15:21:05Z", + "modified_date": "2014-03-17T19:56:11Z", + "title": "Volunteer survey", + "total_responses": 123, + "action_network:hidden": False, + "_embedded": { + "osdi:creator": { + "given_name": "John", + "family_name": "Doe", + "identifiers": ["action_network:123"], + "created_date": "2014-03-24T18:03:45Z", + "modified_date": "2014-03-25T15:00:22Z", + "email_addresses": [ + { + "primary": True, + "address": "jdoe@mail.com", + "status": "subscribed", + } + ], + "phone_numbers": [ + { + "primary": True, + "number": "12021234444", + "number_type": "Mobile", + "status": "subscribed", + } + ], + "postal_addresses": [ + { + "primary": True, + "address_lines": ["1600 Pennsylvania Ave."], + "locality": "Washington", + "region": "DC", + "postal_code": "20009", + "country": "US", + "language": "en", + "location": { + "latitude": 35.919, + "longitude": -72.0379, + "accuracy": "Approximate", + }, + } + ], + "languages_spoken": ["en"], + "_links": { + "self": {"href": f"{self.api_url}/api/v2/people/123"}, + "osdi:attendances": { + "href": f"{self.api_url}/api/v2/people/123/attendances" + }, + "osdi:signatures": { + "href": f"{self.api_url}/api/v2/people/123/signatures" + }, + "osdi:submissions": { + "href": f"{self.api_url}/api/v2/people/123/submissions" + }, + "osdi:donations": { + "href": f"{self.api_url}/api/v2/people/123/donations" + }, + "osdi:outreaches": { + "href": f"{self.api_url}/api/v2/people/123/outreaches" + }, + "osdi:taggings": { + "href": f"{self.api_url}/api/v2/people/123/taggings" + }, + "action_network:responses": { + "href": f"{self.api_url}/api/v2/people/123/responses" + }, + }, + } + }, + "action_network:sponsor": { + "title": "Progressive Action Now", + "url": f"{self.api_url}/groups/progressive-action-now", + }, + "_links": { + "self": {"href": f"{self.api_url}/api/v2/surveys/123"}, + "action_network:responses": { + "href": f"{self.api_url}/api/v2/surveys/123/responses" + }, + "action_network:record_response_helper": { + "href": f"{self.api_url}/api/v2/surveys/123/respnoses" + }, + "osdi:creator": {"href": f"{self.api_url}/api/v2/people/123"}, + "action_network:embed": { + "href": f"{self.api_url}/api/v2/surveys/123/embed" + }, + }, + }, + # truncated for brevity + ] + }, + } + + self.fake_survey = { + "identifiers": ["action_network:123"], + "origin_system": "Action Network", + "created_date": "2014-03-24T18:03:45Z", + "modified_date": "2014-03-25T15:00:22Z", + "title": "Tell us about yourself", + "description": "Tell us a bit more about yourself.
", + "call_to_action": "Let us know", + "browser_url": "https://actionnetwork.org/surveys/tell-us-about-yourself", + "featured_image_url": "https://actionnetwork.org/images/tell-us-about-yourself.jpg", + "total_responses": 2354, + "action_network:hidden": False, + "_embedded": { + "osdi:creator": { + "given_name": "John", + "family_name": "Doe", + "identifiers": ["action_network:123"], + "origin_system": "Action Network", + "created_date": "2014-03-24T18:03:45Z", + "modified_date": "2014-03-25T15:00:22Z", + "email_addresses": [ + {"primary": True, "address": "jdoe@mail.com", "status": "subscribed"} + ], + "phone_numbers": [ + { + "primary": True, + "number": "12021234444", + "number_type": "Mobile", + "status": "subscribed", + } + ], + "postal_addresses": [ + { + "primary": True, + "address_lines": ["1600 Pennsylvania Ave."], + "locality": "Washington", + "region": "DC", + "postal_code": "20009", + "country": "US", + "language": "en", + "location": { + "latitude": 35.919, + "longitude": -72.0379, + "accuracy": "Approximate", + }, + } + ], + "languages_spoken": ["en"], + "_links": { + "self": {"href": "https://actionnetwork.org/api/v2/people/123"}, + "osdi:attendances": { + "href": "https://actionnetwork.org/api/v2/people/123/attendances" + }, + "osdi:signatures": { + "href": "https://actionnetwork.org/api/v2/people/123/signatures" + }, + "osdi:submissions": { + "href": "https://actionnetwork.org/api/v2/people/123/submissions" + }, + "osdi:donations": { + "href": "https://actionnetwork.org/api/v2/people/123/donations" + }, + "osdi:outreaches": { + "href": "https://actionnetwork.org/api/v2/people/123/outreaches" + }, + "osdi:taggings": { + "href": "https://actionnetwork.org/api/v2/people/123/taggings" + }, + "action_network:responses": { + "href": "https://actionnetwork.org/api/v2/people/123/responses" + }, + "curies": [ + { + "name": "osdi", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": True, + }, + { + "name": "action_network", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": True, + }, + ], + }, + } + }, + "_links": { + "self": {"href": "https://actionnetwork.org/api/v2/surveys/123"}, + "action_network:responses": { + "href": "https://actionnetwork.org/api/v2/surveys/123/responses" + }, + "action_network:record_response_helper": { + "href": "https://actionnetwork.org/api/v2/surveys/123/responses" + }, + "osdi:creator": {"href": "https://actionnetwork.org/api/v2/people/123"}, + "action_network:embed": { + "href": "https://actionnetwork.org/api/v2/surveys/123/embed" + }, + "curies": [ + { + "name": "osdi", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": True, + }, + { + "name": "action_network", + "href": "https://actionnetwork.org/docs/v2/{rel}", + "templated": True, + }, + ], + }, + } + + self.fake_survey_with_creator_payload = { + "title": "My Free Survey", + "origin_system": "FreeSurveys.com", + "_links": { + "osdi:creator": {"href": "https://actionnetwork.org/api/v2/people/1234567890"} + }, + } + + self.fake_survey_payload = { + "title": "My Free Survey", + "origin_system": "FreeSurveys.com", + } + # Tags self.fake_tags = { "total_pages": 10, @@ -4233,6 +4575,46 @@ def test_update_submission(self, m): self.fake_submission, ) + # Surveys + @requests_mock.Mocker() + def test_get_surveys(self, m): + m.get( + f"{self.api_url}/surveys?page=1&per_page=25", + text=json.dumps(self.fake_surveys), + ) + m.get( + f"{self.api_url}/surveys?page=2&per_page=25", + text=json.dumps({"_embedded": {"action_network:surveys": []}}), + ) + assert_matching_tables( + self.an.get_surveys(), + Table(self.fake_surveys["_embedded"]["action_network:surveys"]), + ) + + @requests_mock.Mocker() + def test_get_survey(self, m): + m.get(f"{self.api_url}/surveys/123", text=json.dumps(self.fake_survey)) + assert_matching_tables( + self.an.get_survey("123"), + self.fake_survey, + ) + + @requests_mock.Mocker() + def test_create_survey(self, m): + m.post(f"{self.api_url}/surveys", text=json.dumps(self.fake_survey_payload)) + assert_matching_tables( + self.an.create_survey(self.fake_survey_payload), + self.fake_survey_payload, + ) + + @requests_mock.Mocker() + def test_update_survey(self, m): + m.post(f"{self.api_url}/surveys/123", text=json.dumps(self.fake_survey_payload)) + assert_matching_tables( + self.an.update_survey("123", self.fake_survey_payload), + self.fake_survey_payload, + ) + # Tags @requests_mock.Mocker() def test_get_tags(self, m):