From a9e2b9ec16e15267d812b0148a014dde6e3db64c Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 8 Mar 2017 11:53:36 +0000 Subject: Add msisdn util file --- synapse/util/msisdn.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 synapse/util/msisdn.py (limited to 'synapse/util/msisdn.py') diff --git a/synapse/util/msisdn.py b/synapse/util/msisdn.py new file mode 100644 index 0000000000..77ef0aa194 --- /dev/null +++ b/synapse/util/msisdn.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# Copyright 2015, 2016 OpenMarket Ltd +# Copyright 2017 Vector Creations Ltd +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import phonenumbers +from synapse.api.errors import SynapseError + +def phone_number_to_msisdn(country, number): + try: + phoneNumber = phonenumbers.parse(number, country) + except phonenumbers.NumberParseException: + raise SynapseError(400, "Unable to parse phone number") + return phonenumbers.format_number( + phoneNumber, phonenumbers.PhoneNumberFormat.E164 + )[1:] -- cgit 1.4.1 From 1c99934b280485f564b357de204785d55cf2ca62 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 8 Mar 2017 11:58:20 +0000 Subject: pep8 --- synapse/rest/client/v2_alpha/account.py | 2 +- synapse/util/msisdn.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'synapse/util/msisdn.py') diff --git a/synapse/rest/client/v2_alpha/account.py b/synapse/rest/client/v2_alpha/account.py index 71723ef3d6..aac76edf1c 100644 --- a/synapse/rest/client/v2_alpha/account.py +++ b/synapse/rest/client/v2_alpha/account.py @@ -72,7 +72,7 @@ class MsisdnPasswordRequestTokenRestServlet(RestServlet): def on_POST(self, request): body = parse_json_object_from_request(request) - assert_params_in_request(body,[ + assert_params_in_request(body, [ 'id_server', 'client_secret', 'country', 'phone_number', 'send_attempt', ]) diff --git a/synapse/util/msisdn.py b/synapse/util/msisdn.py index 77ef0aa194..6905c5ce75 100644 --- a/synapse/util/msisdn.py +++ b/synapse/util/msisdn.py @@ -17,6 +17,7 @@ import phonenumbers from synapse.api.errors import SynapseError + def phone_number_to_msisdn(country, number): try: phoneNumber = phonenumbers.parse(number, country) -- cgit 1.4.1 From 3edc57296dbefa4a453ac11031614d827e32edf3 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 8 Mar 2017 19:00:51 +0000 Subject: Incorrectly copied copyright This file post-dates OM --- synapse/util/msisdn.py | 1 - 1 file changed, 1 deletion(-) (limited to 'synapse/util/msisdn.py') diff --git a/synapse/util/msisdn.py b/synapse/util/msisdn.py index 6905c5ce75..d320e411f8 100644 --- a/synapse/util/msisdn.py +++ b/synapse/util/msisdn.py @@ -1,5 +1,4 @@ # -*- coding: utf-8 -*- -# Copyright 2015, 2016 OpenMarket Ltd # Copyright 2017 Vector Creations Ltd # # Licensed under the Apache License, Version 2.0 (the "License"); -- cgit 1.4.1 From 9d0d40fc15e4bc79eee46242ae27980073cd528b Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 8 Mar 2017 19:05:29 +0000 Subject: Docs --- synapse/util/msisdn.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'synapse/util/msisdn.py') diff --git a/synapse/util/msisdn.py b/synapse/util/msisdn.py index d320e411f8..607161e7f0 100644 --- a/synapse/util/msisdn.py +++ b/synapse/util/msisdn.py @@ -18,6 +18,19 @@ from synapse.api.errors import SynapseError def phone_number_to_msisdn(country, number): + """ + Takes an ISO-3166-1 2 letter country code and phone number and + returns an msisdn representing the canonical version of that + phone number. + Args: + country (str): ISO-3166-1 2 letter country code + number (str): Phone number in a national or international format + + Returns: + (str) The canonical form of the phone number, as an msisdn + Raises: + SynapseError if the number could not be parsed. + """ try: phoneNumber = phonenumbers.parse(number, country) except phonenumbers.NumberParseException: -- cgit 1.4.1