|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from plivo.utils.validators import * |
| 3 | + |
| 4 | +from ..base import ListResponseObject, PlivoResource, PlivoResourceInterface, ListSessionResponseObject |
| 5 | +from ..exceptions import * |
| 6 | +from ..utils import * |
| 7 | + |
| 8 | + |
| 9 | +class Session(PlivoResource): |
| 10 | + _name = 'Session' |
| 11 | + _identifier_string = 'session_uuid' |
| 12 | + |
| 13 | + def delete(self): |
| 14 | + raise InvalidRequestError('Cannot delete a Session resource') |
| 15 | + |
| 16 | + def update(self): |
| 17 | + raise InvalidRequestError('Cannot update a Session resource') |
| 18 | + |
| 19 | + |
| 20 | +class Sessions(PlivoResourceInterface): |
| 21 | + _resource_type = Session |
| 22 | + |
| 23 | + @validate_args( |
| 24 | + app_uuid=[optional(of_type(six.text_type))], |
| 25 | + recipient=[required(is_phonenumber())], |
| 26 | + channel=[optional(all_of(of_type(six.text_type), is_in(('sms', 'voice'))))], |
| 27 | + url=[optional(is_url())], |
| 28 | + method=[optional(of_type(six.text_type))]) |
| 29 | + def create(self, |
| 30 | + app_uuid=None, |
| 31 | + recipient=None, |
| 32 | + channel=None, |
| 33 | + url=None, |
| 34 | + method='POST'): |
| 35 | + if recipient is None: |
| 36 | + raise ValidationError('destination number is required') |
| 37 | + return self.client.request('POST', ('Verify', 'Session', ), |
| 38 | + to_param_dict(self.create, locals())) |
| 39 | + |
| 40 | + @validate_args(session_uuid=[of_type(six.text_type)]) |
| 41 | + def get(self, session_uuid): |
| 42 | + return self.client.request( |
| 43 | + 'GET', ('Verify', 'Session', session_uuid), response_type=Session) |
| 44 | + |
| 45 | + @validate_args( |
| 46 | + subaccount=[optional(is_subaccount())], |
| 47 | + status=[optional(is_in(('in-progress', 'expired', 'verified')))], |
| 48 | + session_time__gt=[optional(is_valid_date())], |
| 49 | + session_time__gte=[optional(is_valid_date())], |
| 50 | + session_time__lt=[optional(is_valid_date())], |
| 51 | + session_time__lte=[optional(is_valid_date())], |
| 52 | + session_time=[optional(is_valid_date())], |
| 53 | + country=[optional(of_type(six.text_type))], |
| 54 | + alias=[optional(of_type(six.text_type))], |
| 55 | + app_uuid=[optional(of_type(six.text_type))], |
| 56 | + recipient=[optional(of_type(six.text_type))], |
| 57 | + limit=[ |
| 58 | + optional( |
| 59 | + all_of( |
| 60 | + of_type(*six.integer_types), |
| 61 | + check(lambda limit: 0 < limit <= 20, '0 < limit <= 20'))) |
| 62 | + ], |
| 63 | + offset=[ |
| 64 | + optional( |
| 65 | + all_of( |
| 66 | + of_type(*six.integer_types), |
| 67 | + check(lambda offset: 0 <= offset, '0 <= offset'))) |
| 68 | + ]) |
| 69 | + def list(self, |
| 70 | + subaccount=None, |
| 71 | + status=None, |
| 72 | + session_time__gt=None, |
| 73 | + session_time__gte=None, |
| 74 | + session_time__lt=None, |
| 75 | + session_time__lte=None, |
| 76 | + session_time=None, |
| 77 | + country=None, |
| 78 | + alias=None, |
| 79 | + app_uuid=None, |
| 80 | + recipient=None, |
| 81 | + limit=None, |
| 82 | + offset=None): |
| 83 | + return self.client.request( |
| 84 | + 'GET', ('Verify', 'Session', ), |
| 85 | + to_param_dict(self.list, locals()), |
| 86 | + response_type=ListSessionResponseObject, |
| 87 | + objects_type=Session) |
| 88 | + |
| 89 | + @validate_args( |
| 90 | + otp=[optional(of_type(six.text_type))], |
| 91 | + session_uuid=[of_type(six.text_type)] |
| 92 | + ) |
| 93 | + def validate(self, session_uuid, otp=None): |
| 94 | + if otp is None: |
| 95 | + raise ValidationError('otp is required') |
| 96 | + return self.client.request('POST', ('Verify', 'Session', session_uuid), |
| 97 | + to_param_dict(self.validate, locals())) |
| 98 | + |
0 commit comments