File: requestsclient.py

package info (click to toggle)
azure-multiapi-storage-python 0.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 23,652 kB
  • sloc: python: 222,802; sh: 61; makefile: 3
file content (84 lines) | stat: -rw-r--r-- 2,926 bytes parent folder | download | duplicates (9)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#-------------------------------------------------------------------------
# Copyright (c) Microsoft.  All rights reserved.
#
# 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.
#--------------------------------------------------------------------------

class _Response(object):

    ''' Response class corresponding to the response returned from httplib
    HTTPConnection. '''

    def __init__(self, response):
        self.status = response.status_code
        self.reason = response.reason
        self.respbody = response.content
        self.length = len(response.content)
        self.headers = []
        for key, name in response.headers.items():
            self.headers.append((key.lower(), name))

    def getheaders(self):
        '''Returns response headers.'''
        return self.headers

    def read(self, _length):
        '''Returns response body. '''
        return self.respbody[:_length]


class _RequestsConnection(object):

    def __init__(self, host, protocol, session, timeout):
        self.host = host
        self.protocol = protocol
        self.session = session
        self.headers = {}
        self.method = None
        self.body = None
        self.response = None
        self.uri = None
        self.timeout = timeout

        # By default, requests adds an Accept:*/* to the session, which causes
        # issues with some Azure REST APIs. Removing it here gives us the flexibility
        # to add it back on a case by case basis via putheader.
        if 'Accept' in self.session.headers:
            del self.session.headers['Accept']

    def close(self):
        pass

    def set_tunnel(self, host, port=None, headers=None):
        self.session.proxies['http'] = 'http://{}:{}'.format(host, port)
        self.session.proxies['https'] = 'https://{}:{}'.format(host, port)
        if headers:
            self.session.headers.update(headers)

    def set_proxy_credentials(self, user, password):
        pass

    def putrequest(self, method, uri):
        self.method = method
        self.uri = self.protocol + '://' + self.host + uri

    def putheader(self, name, value):
        self.headers[name] = value

    def endheaders(self):
        pass

    def send(self, request_body):
        self.response = self.session.request(self.method, self.uri, data=request_body, headers=self.headers, timeout=self.timeout)

    def getresponse(self):
        return _Response(self.response)