|
17 | 17 |
|
18 | 18 | try: |
19 | 19 | from urllib.parse import urlencode |
| 20 | + from urllib.error import HTTPError |
20 | 21 | from urllib.request import Request, urlopen |
21 | 22 | except ImportError: |
22 | 23 | from urllib import urlencode |
23 | 24 |
|
24 | | - from urllib2 import Request, urlopen |
25 | | - |
| 25 | + from urllib2 import Request, urlopen, HTTPError |
26 | 26 |
|
27 | 27 | VOMS_FQANS_OID = b"1.3.6.1.4.1.8005.100.100.4" |
28 | 28 | VOMS_EXTENSION_OID = b"1.3.6.1.4.1.8005.100.100.5" |
@@ -81,7 +81,7 @@ def getVO(proxy_data): |
81 | 81 |
|
82 | 82 |
|
83 | 83 | class BaseRequest(object): |
84 | | - """This class helps supporting multiple kinds of requests that requires connections""" |
| 84 | + """This class helps supporting multiple kinds of requests that require connections""" |
85 | 85 |
|
86 | 86 | def __init__(self, url, caPath, name="unknown"): |
87 | 87 | self.name = name |
@@ -114,21 +114,32 @@ def addHeader(self, key, value): |
114 | 114 | """Add a header (key, value) into the request header""" |
115 | 115 | self.headers[key] = value |
116 | 116 |
|
117 | | - def executeRequest(self, raw_data, insecure=False): |
| 117 | + def executeRequest(self, raw_data, insecure=False, content_type="json"): |
118 | 118 | """Execute a HTTP request with the data, headers, and the pre-defined data (SSL + auth) |
119 | 119 |
|
120 | 120 | :param raw_data: Data to send |
121 | 121 | :type raw_data: dict |
122 | 122 | :param insecure: Deactivate proxy verification WARNING Debug ONLY |
123 | 123 | :type insecure: bool |
| 124 | + :param content_type: Data format to send, either "json" or "x-www-form-urlencoded" |
| 125 | + :type content_type: str |
124 | 126 | :return: Parsed JSON response |
125 | 127 | :rtype: dict |
126 | | - """ |
127 | | - if sys.version_info.major == 3: |
128 | | - data = urlencode(raw_data).encode("utf-8") # encode to bytes ! for python3 |
| 128 | + """ |
| 129 | + if content_type == "json": |
| 130 | + data = json.dumps(raw_data).encode("utf-8") |
| 131 | + self.headers["Content-Type"] = "application/json" |
| 132 | + elif content_type == "x-www-form-urlencoded": |
| 133 | + if sys.version_info.major == 3: |
| 134 | + data = urlencode(raw_data).encode("utf-8") # encode to bytes ! for python3 |
| 135 | + else: |
| 136 | + # Python2 |
| 137 | + data = urlencode(raw_data) |
| 138 | + self.headers["Content-Type"] = "application/x-www-form-urlencoded" |
129 | 139 | else: |
130 | | - # Python2 |
131 | | - data = urlencode(raw_data) |
| 140 | + raise ValueError("Invalid content_type. Use 'json' or 'x-www-form-urlencoded'.") |
| 141 | + |
| 142 | + self.headers["Content-Length"] = str(len(data)) |
132 | 143 |
|
133 | 144 | request = Request(self.url, data=data, headers=self.headers, method="POST") |
134 | 145 |
|
@@ -190,8 +201,12 @@ def __init__(self, url, caPath, certEnv): |
190 | 201 | ) |
191 | 202 | self._hasExtraCredentials = True |
192 | 203 |
|
193 | | - def executeRequest(self, raw_data): |
| 204 | + def executeRequest(self, raw_data, insecure=False, content_type="json"): |
194 | 205 | # Adds a flag if the passed cert is a Directory |
195 | 206 | if self._hasExtraCredentials: |
196 | 207 | raw_data["extraCredentials"] = '"hosts"' |
197 | | - return super(X509BasedRequest, self).executeRequest(raw_data) |
| 208 | + return super(X509BasedRequest, self).executeRequest( |
| 209 | + raw_data, |
| 210 | + insecure=insecure, |
| 211 | + content_type=content_type |
| 212 | + ) |
0 commit comments