forked from Azure/azure-linux-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpclientfactory.py
50 lines (38 loc) · 1.82 KB
/
httpclientfactory.py
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
#!/usr/bin/env python2
#
# Copyright (C) Microsoft Corporation, All rights reserved.
import os
from curlhttpclient import CurlHttpClient
from urllib2httpclient import Urllib2HttpClient
PY_MAJOR_VERSION = 0
PY_MINOR_VERSION = 1
PY_MICRO_VERSION = 2
class HttpClientFactory:
"""Factory which returns the appropriate HttpClient based on the provided python version.
Targets :
[2.4.0 - 2.7.9[ : CurlHttpclient
[2.7.9 - 2.7.9+ : Urllib2Httpclient
This is due to the lack of built-in strict certificate verification prior to 2.7.9.
The ssl module was also unavailable for [2.4.0 - 2.6.0[.
"""
def __init__(self, cert, key, insecure=False):
self.cert = cert
self.key = key
self.insecure = insecure
self.proxy_configuration = None
def create_http_client(self, version_info):
"""Create a new instance of the appropriate HttpClient.
Args:
version_info : array, the build-in python version_info array.
insecure : bool, when set to True, httpclient wil bypass certificate verification.
Returns:
An instance of CurlHttpClient if the installed Python version is below 2.7.9
An instance of Urllib2 if the installed Python version is or is above 2.7.9
"""
if version_info[PY_MAJOR_VERSION] == 2 and version_info[PY_MINOR_VERSION] < 7:
return CurlHttpClient(self.cert, self.key, self.insecure, self.proxy_configuration)
elif version_info[PY_MAJOR_VERSION] == 2 and version_info[PY_MINOR_VERSION] <= 7 and version_info[
PY_MICRO_VERSION] < 9:
return CurlHttpClient(self.cert, self.key, self.insecure, self.proxy_configuration)
else:
return Urllib2HttpClient(self.cert, self.key, self.insecure, self.proxy_configuration)