File: phone_number_helper.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (48 lines) | stat: -rw-r--r-- 2,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
from azure_devtools.scenario_tests import RecordingProcessor
from _shared.testcase import ResponseReplacerProcessor

class PhoneNumberUriReplacer(RecordingProcessor):
    """Replace the identity in request uri"""

    def process_request(self, request):
        import re
        request.uri = re.sub('phoneNumbers/[%2B\d]+', 'phoneNumbers/sanitized', request.uri)
        return request

    def process_response(self, response):
        import re
        if 'url' in response:
            response['url'] = re.sub('capabilities/([^/?&]+)', 'capabilities/sanitized', response['url'])
            response['url'] = re.sub('releases/([^/?&]+)', 'releases/sanitized', response['url'])
            response['url'] = re.sub('searches/([^/?&]+)', 'searches/sanitized', response['url'])
            response['url'] = re.sub('phoneNumbers/[%2B\d]+', 'phoneNumbers/sanitized', response['url'])
            response['url'] = re.sub('^(.*?)\.communication.azure.com', 'https://sanitized.communication.azure.com', response['url'])
        return response

class PhoneNumberResponseReplacerProcessor(ResponseReplacerProcessor):

    def process_response(self, response):
        import json
        try:
            body = json.loads(response['body']['string'])
            if 'phoneNumbers' in body:
                for item in body["phoneNumbers"]:
                    if isinstance(item, str):
                        body["phoneNumbers"] = [self._replacement]
                        break
                    if "phoneNumber" in item:
                        item['phoneNumber'] = self._replacement
                    if "id" in item:
                        item['id'] = self._replacement
            response['body']['string'] = json.dumps(body)
            response['url'] = self._replacement
            return response
        except (KeyError, ValueError, TypeError):
            return response