From a4e9e2a4f95addc27ea19525d5bd4adde3183a61 Mon Sep 17 00:00:00 2001 From: zorynbas Date: Wed, 23 Oct 2019 22:51:45 -0700 Subject: [PATCH] attachments actions initial commit --- .DS_Store | Bin 0 -> 8196 bytes tests/api/__init__.py | 5 + tests/api/test_attachment_actions.py | 118 +++++++++++++++ tests/conftest.py | 1 + tests/test_webexteamssdk.py | 1 + webexteamssdk/__init__.py | 6 +- webexteamssdk/api/__init__.py | 4 + webexteamssdk/api/attachment_actions.py | 137 ++++++++++++++++++ webexteamssdk/models/immutable.py | 6 + .../models/mixins/attachment_action.py | 82 +++++++++++ 10 files changed, 357 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 tests/api/test_attachment_actions.py create mode 100644 webexteamssdk/api/attachment_actions.py create mode 100644 webexteamssdk/models/mixins/attachment_action.py diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fc59abd7380c0e9a817533d38848040ec4295ccd GIT binary patch literal 8196 zcmeHM%Wl&^6ur}?jgt})1X8o~28l&gHI!B$gp{TUQY1hXu3!PEjT^VM>ewN6l9mvZ z4gUcA0iVJb@F9EvoOux0)0Q2m5;M`gcRX{>JkE^cj6+1MKIl}5Dnw+#cq}i$U66P^ zw{vMumE1&R;8WxxSB5=QH6|DZi~>dhqkvJsC~(yjz%!dC>yqcbtTo;!U=;W-72w|w zHjKxbic=ZIR|lLp0wCs4EE4kk0Khr6iZvCdGNJ~FNh+eGGIhjYl8$m);c6;QWt4Pc z>hQr-&rF?AnCKnt+X7CkCZq920i(cq1?1WNZYzim+#qv)zw=WhLCH~vB08X0!+k|< z8fTG5r_`n{^(a7I!?R@#pQk6(qHSu@GjgbzZJmA8&`b1`wt#HWV=x@rrB(7N)L1_> zR-Sf%>d_%Q#*{^iHx-{!Grg8YqlZAu{6ypNnWuS0dX6z|Q3GRag4L&(k}OBeKFzrS zlt*uAfRXWB7Zy?i<|VxfMba&l|Ik8$mC#4PwMp|A(boam;QDA-t_-tM$`pY0FuDP- zt6&|{QFeX=O)?BC!|2gj7SHv_;1|$3_6%1*Lt-ankU1I(t%e#7^Sc=5j^du$-{0I` z;K#|@+IKcLpIcbWTUOC3mWpqj<7nh0?PL(w+x=JkYNwqXyDGmMMZqlJc3=43Xs>*0 zI|`GwA9{U3_B$RRuV4CMCmPkGLD-2!JC3Zdtb$e8D_6$jdm9@o_WJ$x$%;MRe6YD< zZ&a(3Nx@paU47Kr@1DIIzn^^gjKxA?>WN3!<@L;eL2f3*(CLRk6drK`tRw{Do6yJh z4I!3?WnRV7X^SA1`82i6B^m+i!mxed`OO^T)1eD)0hVTpfuVEr{1-|XXB4>F3e3yl zRrvY8iTl>P2e{hnWkzQd_!|naT*GP9(em$K8hR)EtZl>gVLWl%R7OF8%e)A{_3RHr a5dmMwB3#7Xd;B literal 0 HcmV?d00001 diff --git a/tests/api/__init__.py b/tests/api/__init__.py index 0db0f65..b89261c 100644 --- a/tests/api/__init__.py +++ b/tests/api/__init__.py @@ -29,6 +29,7 @@ import webexteamssdk from tests.environment import WEBEX_TEAMS_ACCESS_TOKEN from webexteamssdk.api.access_tokens import AccessTokensAPI +from webexteamssdk.api.attachment_actions import AttachmentActionsAPI from webexteamssdk.api.events import EventsAPI from webexteamssdk.api.licenses import LicensesAPI from webexteamssdk.api.memberships import MembershipsAPI @@ -133,6 +134,10 @@ def test_access_tokens_api_object_creation(api): assert isinstance(api.access_tokens, AccessTokensAPI) +def test_attachment_actions_api_object_creation(api): + assert isinstance(api.attachment_actions, AttachmentActionsAPI) + + def test_events_api_object_creation(api): assert isinstance(api.events, EventsAPI) diff --git a/tests/api/test_attachment_actions.py b/tests/api/test_attachment_actions.py new file mode 100644 index 0000000..92ca717 --- /dev/null +++ b/tests/api/test_attachment_actions.py @@ -0,0 +1,118 @@ +# -*- coding: utf-8 -*- +"""WebexTeamsAPI Messages API fixtures and tests. + +Copyright (c) 2016-2019 Cisco and/or its affiliates. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" + +import itertools + +import pytest + +import webexteamssdk +from tests.environment import WEBEX_TEAMS_TEST_FILE_URL +from tests.utils import create_string + + +# Helper Functions + +def is_valid_attachment_action(obj): + return isinstance(obj, webexteamssdk.AttachmentAction) \ + and obj.id is not None + + +# Fixtures + +@pytest.fixture(scope="session") +def attachment_action_create(api, test_people): + person = test_people["member_added_by_email"] + message = api.messages.create( + toPersonEmail=person.emails[0], + text=create_string("Message"), + attachments=[{ + "contentType": "application/vnd.microsoft.card.adaptive", + "content": { + "$schema": ("http://adaptivecards.io/schemas/" + "adaptive-card.json"), + "type": "AdaptiveCard", + "version": "1.0", + "body": [{ + "type": "ColumnSet", + "columns": [{ + "type": "Column", + "width": 2, + "items": [ + { + "type": "TextBlock", + "text": "Tell us about your problem", + "weight": "bolder", + "size": "medium" + }, + { + "type": "TextBlock", + "text": "Your name", + "wrap": True + }, + { + "type": "Input.Text", + "id": "Name", + "placeholder": "John Andersen" + }, + { + "type": "TextBlock", + "text": "Your email", + "wrap": True + }, + { + "type": "Input.Text", + "id": "Email", + "placeholder": "john.andersen@example.com", + "style": "email" + }, + ] + }] + }], + "actions": [ + { + "type": "Action.Submit", + "title": "Submit" + } + ] + } + }] + ) + attachment_action = api.attachment_actions.create( + type="submit", messageId=message.id, + inputs={"Name": "test_name", "Email": "test_email"} + ) + yield attachment_action + + api.messages.delete(message.id) + +# Tests + + +def test_attachment_actions_create(attachment_action_create): + assert is_valid_attachment_action(attachment_action_create) + + +def test_attachment_actions_get(api, attachment_action_create): + attachment_action = api.attachment_actions.get(attachment_action_create.id) + assert is_valid_attachment_action(attachment_action) diff --git a/tests/conftest.py b/tests/conftest.py index d885502..37be457 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -39,6 +39,7 @@ pytest_plugins = [ 'tests.test_webexteamssdk', 'tests.api', + 'tests.api.test_attachment_actions', 'tests.api.test_licenses', 'tests.api.test_memberships', 'tests.api.test_messages', diff --git a/tests/test_webexteamssdk.py b/tests/test_webexteamssdk.py index e2c3415..bf9cde1 100644 --- a/tests/test_webexteamssdk.py +++ b/tests/test_webexteamssdk.py @@ -43,6 +43,7 @@ def test_package_contents(self): # Data Models assert hasattr(webexteamssdk, "dict_data_factory") assert hasattr(webexteamssdk, "AccessToken") + assert hasattr(webexteamssdk, "AttachmentAction") assert hasattr(webexteamssdk, "Event") assert hasattr(webexteamssdk, "License") assert hasattr(webexteamssdk, "Membership") diff --git a/webexteamssdk/__init__.py b/webexteamssdk/__init__.py index 19457b9..eed2a1c 100644 --- a/webexteamssdk/__init__.py +++ b/webexteamssdk/__init__.py @@ -41,9 +41,9 @@ ) from .models.dictionary import dict_data_factory from .models.immutable import ( - AccessToken, Event, License, Membership, Message, Organization, Person, - Role, Room, Team, TeamMembership, Webhook, WebhookEvent, - immutable_data_factory, + AccessToken, AttachmentAction, Event, License, Membership, Message, + Organization, Person, Role, Room, Team, TeamMembership, Webhook, + WebhookEvent, immutable_data_factory, ) from .models.simple import SimpleDataModel, simple_data_factory from .utils import WebexTeamsDateTime diff --git a/webexteamssdk/api/__init__.py b/webexteamssdk/api/__init__.py index 3542efa..143af9a 100644 --- a/webexteamssdk/api/__init__.py +++ b/webexteamssdk/api/__init__.py @@ -34,6 +34,7 @@ from webexteamssdk.restsession import RestSession from webexteamssdk.utils import check_type from .access_tokens import AccessTokensAPI +from .attachment_actions import AttachmentActionsAPI from .events import EventsAPI from .guest_issuer import GuestIssuerAPI from .licenses import LicensesAPI @@ -179,6 +180,9 @@ def __init__(self, access_token=None, base_url=DEFAULT_BASE_URL, self.team_memberships = TeamMembershipsAPI( self._session, object_factory ) + self.attachment_actions = AttachmentActionsAPI( + self._session, object_factory + ) self.webhooks = WebhooksAPI(self._session, object_factory) self.organizations = OrganizationsAPI(self._session, object_factory) self.licenses = LicensesAPI(self._session, object_factory) diff --git a/webexteamssdk/api/attachment_actions.py b/webexteamssdk/api/attachment_actions.py new file mode 100644 index 0000000..e2cc47d --- /dev/null +++ b/webexteamssdk/api/attachment_actions.py @@ -0,0 +1,137 @@ +# -*- coding: utf-8 -*- +"""Webex Teams Messages API wrapper. + +Copyright (c) 2016-2019 Cisco and/or its affiliates. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" + + +from __future__ import ( + absolute_import, + division, + print_function, + unicode_literals, +) + +from builtins import * + +from past.builtins import basestring +from requests_toolbelt import MultipartEncoder + +from ..generator_containers import generator_container +from ..restsession import RestSession +from ..utils import ( + check_type, dict_from_items_with_values, is_local_file, is_web_url, + open_local_file, +) + + +API_ENDPOINT = 'attachment/actions' +OBJECT_TYPE = 'attachment_action' + + +class AttachmentActionsAPI(object): + """Webex Teams Attachment Actions API. + + Wraps the Webex Teams Attachment Actions API and exposes the API as + native Python methods that return native Python objects. + + """ + + def __init__(self, session, object_factory): + """Init a new AttachmentActionsAPI object with the provided RestSession. + + Args: + session(RestSession): The RESTful session object to be used for + API calls to the Webex Teams service. + + Raises: + TypeError: If the parameter types are incorrect. + + """ + check_type(session, RestSession, may_be_none=False) + super(AttachmentActionsAPI, self).__init__() + self._session = session + self._object_factory = object_factory + + def create(self, type=None, messageId=None, inputs=None, + **request_parameters): + """Create an attachment action. + + Args: + type(attachment action enum): The type of attachment action. + messageId(basestring): The ID of parent message the attachment + action is to be performed on. + inputs(dict): inputs to attachment fields + **request_parameters: Additional request parameters (provides + support for parameters that may be added in the future). + + Returns: + Attachment action: A attachment action object with the details + of the created attachment action. + + Raises: + TypeError: If the parameter types are incorrect. + ApiError: If the Webex Teams cloud returns an error. + ValueError: If the files parameter is a list of length > 1, or if + the string in the list (the only element in the list) does not + contain a valid URL or path to a local file. + + """ + + check_type(type, basestring, may_be_none=False) + check_type(messageId, basestring, may_be_none=False) + check_type(inputs, dict, may_be_none=False) + + post_data = dict_from_items_with_values( + request_parameters, + type=type, + messageId=messageId, + inputs=inputs + ) + + json_data = self._session.post(API_ENDPOINT, json=post_data) + + # Return a attachment action object created from the response JSON data + return self._object_factory(OBJECT_TYPE, json_data) + + def get(self, attachmentId): + """Get the details of a attachment action, by ID. + + Args: + attachmentId(basestring): The ID of the attachment action to be + retrieved. + + Returns: + AttachmentAction: A Attachment Action object with the details of + the requested attachment action. + + Raises: + TypeError: If the parameter types are incorrect. + ApiError: If the Webex Teams cloud returns an error. + + """ + check_type(attachmentId, basestring, may_be_none=False) + + # API request + json_data = self._session.get(API_ENDPOINT + '/' + attachmentId) + + # Return a message object created from the response JSON data + return self._object_factory(OBJECT_TYPE, json_data) diff --git a/webexteamssdk/models/immutable.py b/webexteamssdk/models/immutable.py index 018940c..879e140 100644 --- a/webexteamssdk/models/immutable.py +++ b/webexteamssdk/models/immutable.py @@ -42,6 +42,7 @@ from webexteamssdk.utils import json_dict from .mixins.access_token import AccessTokenBasicPropertiesMixin +from .mixins.attachment_action import AttachmentActionBasicPropertiesMixin from .mixins.event import EventBasicPropertiesMixin from .mixins.license import LicenseBasicPropertiesMixin from .mixins.membership import MembershipBasicPropertiesMixin @@ -230,6 +231,10 @@ class Webhook(ImmutableData, WebhookBasicPropertiesMixin): """Webex Teams Webhook data model.""" +class AttachmentAction(ImmutableData, AttachmentActionBasicPropertiesMixin): + """Webex Attachment Actions data model""" + + class WebhookEvent(ImmutableData, WebhookEventBasicPropertiesMixin): """Webex Teams Webhook-Events data model.""" @@ -246,6 +251,7 @@ class GuestIssuerToken(ImmutableData, GuestIssuerTokenBasicPropertiesMixin): immutable_data_models = defaultdict( lambda: ImmutableData, access_token=AccessToken, + attachment_action=AttachmentAction, event=Event, license=License, membership=Membership, diff --git a/webexteamssdk/models/mixins/attachment_action.py b/webexteamssdk/models/mixins/attachment_action.py new file mode 100644 index 0000000..01d2cfe --- /dev/null +++ b/webexteamssdk/models/mixins/attachment_action.py @@ -0,0 +1,82 @@ +# -*- coding: utf-8 -*- +"""Webex Teams Message data model. + +Copyright (c) 2016-2019 Cisco and/or its affiliates. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +""" + + +from __future__ import ( + absolute_import, + division, + print_function, + unicode_literals, +) + +from builtins import * + +from webexteamssdk.utils import WebexTeamsDateTime + + +class AttachmentActionBasicPropertiesMixin(object): + """Attachment Action basic properties.""" + + @property + def id(self): + """The unique identifier for the attachment action.""" + return self._json_data.get('id') + + @property + def personId(self): + """The ID of the person who performed the attachment action.""" + return self._json_data.get('personId') + + @property + def roomId(self): + """The room ID of the attachment action.""" + return self._json_data.get('roomId') + + @property + def type(self): + """The type of attachment action. + + Attachment action enum: + 'submit': submit filled in inputs + """ + return self._json_data.get('type') + + @property + def messageId(self): + """The ID of parent message the attachment action was performed on.""" + return self._json_data.get('messageId') + + @property + def inputs(self): + """The attachment action's inputs""" + return self._json_data('inputs') + + @property + def created(self): + """The date and time the attachment action was created.""" + created = self._json_data.get('created') + if created: + return WebexTeamsDateTime.strptime(created) + else: + return None