0001-replace-urllib-calls-by-requests-7898.patch
passerelle/apps/choosit/models.py | ||
---|---|---|
1 | 1 |
import re |
2 | 2 |
import urllib |
3 |
import urllib2 |
|
4 | 3 |
import logging |
5 | 4 |
import json |
5 |
import requests |
|
6 | 6 | |
7 | 7 |
from django.utils.translation import ugettext_lazy as _ |
8 | 8 |
from django.db import models |
... | ... | |
60 | 60 |
} |
61 | 61 |
try: |
62 | 62 |
data = urllib.urlencode([('data', json.dumps(params))]) |
63 |
r = urllib2.Request('http://sms.choosit.com/webservice', data) |
|
64 |
p = urllib2.urlopen(r, timeout=5) |
|
65 |
output = json.load(p) |
|
66 |
p.close() |
|
63 |
r = request.get('http://sms.choosit.com/webservice', params=data, timeout=5) |
|
64 |
output = r.json() |
|
67 | 65 |
except Exception, e: |
68 | 66 |
logging.exception(u'%s unable to send message %s to %s', |
69 | 67 |
self, text, destinations) |
... | ... | |
156 | 154 |
reg = ChoositRegisterWS(self.url, self.key) |
157 | 155 |
ws = reg.update(subscriptions, user) |
158 | 156 |
return {"message": ws['status']} |
159 |
passerelle/apps/clicrdv/models.py | ||
---|---|---|
7 | 7 |
import base64 |
8 | 8 |
import datetime |
9 | 9 |
import json |
10 |
import urllib2
|
|
10 |
import requests
|
|
11 | 11 | |
12 | 12 |
from django.conf import settings |
13 | 13 |
from django.core.urlresolvers import reverse |
... | ... | |
64 | 64 |
return 'clock' |
65 | 65 | |
66 | 66 | |
67 |
def get_request(self, uri): |
|
67 |
def get_request(self, uri, req='get', data=None, headers=None): |
|
68 |
kwargs = {} |
|
68 | 69 |
url = 'https://%s/api/v1/%s' % (self.server, uri) |
69 | 70 |
if '?' in url: |
70 | 71 |
url = url + '&apikey=%s&format=json' % self.apikey |
71 | 72 |
else: |
72 | 73 |
url = url + '?apikey=%s&format=json' % self.apikey |
73 |
req = urllib2.Request(url) |
|
74 |
authheader = 'Basic ' + \ |
|
75 |
base64.encodestring('%s:%s' % (self.username, self.password))[:-1] |
|
76 |
req.add_header('Authorization', authheader) |
|
77 |
return req |
|
74 |
if data: |
|
75 |
kwargs.update({'data': data}) |
|
76 |
if headers: |
|
77 |
kwargs.update({'headers': headers}) |
|
78 |
return requests.request(req, url, |
|
79 |
auth=((self.username, self.password)), **kwargs) |
|
78 | 80 | |
79 | 81 |
def get_json(self, uri): |
80 |
req = self.get_request(uri)
|
|
81 |
return json.load(urllib2.urlopen(req))
|
|
82 |
r = get_request(uri)
|
|
83 |
return r.json()
|
|
82 | 84 | |
83 | 85 |
def get_interventionsets(self, **kwargs): |
84 | 86 |
records = self.get_json('interventionsets').get('records') |
... | ... | |
156 | 158 | |
157 | 159 |
def cancel(self, id, **kwargs): |
158 | 160 |
appointment_id = int(id) |
159 |
req = self.get_request('appointments/%s' % appointment_id) |
|
160 |
req.get_method = (lambda: 'DELETE') |
|
161 |
try: |
|
162 |
fd = urllib2.urlopen(req) |
|
163 |
none = fd.read() |
|
164 |
except urllib2.HTTPError as e: |
|
161 |
req = self.get_request('appointments/%s' % appointment_id, 'delete') |
|
162 |
if not req.ok(): |
|
165 | 163 |
# clicrdv will return a "Bad Request" (HTTP 400) response |
166 | 164 |
# when it's not possible to remove an appointment |
167 | 165 |
# (for example because it's too late) |
168 |
response = e.read() |
|
169 |
response = json.loads(response) |
|
170 |
return {'success': False, 'error': response} |
|
166 |
return {'success': False, 'error': req.json()} |
|
171 | 167 |
return {'success': True} |
172 | 168 | |
173 | ||
174 | 169 |
def create_appointment(self, intervention, websource, data): |
175 | 170 |
fields = data.get('fields') or {} |
176 | 171 |
extra = data.get('extra') or {} |
... | ... | |
206 | 201 |
# optional parameters, if any... |
207 | 202 |
for fieldname in (fields.keys() + extra.keys() + data.keys()): |
208 | 203 |
if fieldname.startswith('clicrdv_fiche_'): |
209 |
appointment['fiche'][fieldname[14:]] = get_data(fieldname) or '' |
|
210 |
req = self.get_request('appointments') |
|
211 |
req.add_data(json.dumps({'appointment': appointment})) |
|
212 |
req.add_header('Content-Type', 'application/json') |
|
213 |
try: |
|
214 |
fd = urllib2.urlopen(req) |
|
215 |
except urllib2.HTTPError, e: |
|
204 |
appointment['fiche'][fieldname[14:]] = fields[fieldname] or '' |
|
205 |
req = self.get_request('appointments', |
|
206 |
data=json.dumps({'appointment': appointment}), |
|
207 |
headers={'Content-Type': 'application/json'}) |
|
208 |
if not req.ok(): |
|
216 | 209 |
try: |
217 |
error = json.load(e.fp)[0].get('error')
|
|
210 |
error = req.json().get('error')
|
|
218 | 211 |
except: |
219 | 212 |
error = 'Unknown error (Passerelle)' |
220 | 213 |
return { |
... | ... | |
223 | 216 |
} |
224 | 217 |
else: |
225 | 218 |
success = True |
226 |
response = json.load(fd)
|
|
219 |
response = req.json()
|
|
227 | 220 |
appointment_id = response.get('records')[0].get('id') |
228 | 221 |
return { |
229 | 222 |
'success': True, |
passerelle/apps/mobyt/models.py | ||
---|---|---|
1 | 1 |
import re |
2 |
import urllib |
|
3 |
import urllib2 |
|
4 | 2 |
import logging |
5 |
import json
|
|
3 |
import requests
|
|
6 | 4 | |
7 | 5 |
from django.utils.translation import ugettext_lazy as _ |
8 | 6 |
from django.db import models |
... | ... | |
63 | 61 |
# unfortunately it lacks a batch API... |
64 | 62 |
destinations = self.clean_numbers(destinations, self.default_country_code) |
65 | 63 |
rcpt = ','.join(destinations) |
66 |
params = urllib.urlencode({
|
|
64 |
params = { |
|
67 | 65 |
'user': self.username, |
68 | 66 |
'pass': self.password, |
69 | 67 |
'rcpt': rcpt, |
70 | 68 |
'data': text, |
71 | 69 |
'sender': sender, |
72 | 70 |
'qty': self.quality |
73 |
})
|
|
71 |
} |
|
74 | 72 |
try: |
75 |
r = urllib2.urlopen(self.URL, params)
|
|
73 |
r = requests.get(self.URL, params=params)
|
|
76 | 74 |
except Exception, e: |
77 |
logger.error('unable to urlopen %s: %s', self.URL, e) |
|
78 |
raise MobytError('unable to urlopen %s: %s' % (self.URL, e)) |
|
79 |
answer = r.read() |
|
80 |
r.close() |
|
81 |
if answer[:2] != "OK": |
|
82 |
raise MobytError('MobyT error: %r' % answer) |
|
75 |
logger.error('unable to open %s: %s', self.URL, e) |
|
76 |
raise MobytError('unable to open %s: %s' % (self.URL, e)) |
|
77 |
if not r.ok(): |
|
78 |
raise MobytError('MobyT error: %r' % r.content) |
|
83 | 79 |
return { 'message': 'OK' } |
passerelle/apps/ovh/models.py | ||
---|---|---|
1 | 1 |
import re |
2 |
import urllib |
|
3 |
import urllib2 |
|
4 | 2 |
import logging |
5 |
import json
|
|
3 |
import requests
|
|
6 | 4 | |
7 | 5 |
from django.utils.translation import ugettext_lazy as _ |
8 | 6 |
from django.db import models |
... | ... | |
85 | 83 |
} |
86 | 84 |
ret = {} |
87 | 85 |
try: |
88 |
stream = urllib.urlopen('%s?%s' % (self.URL, urllib.urlencode(params)))
|
|
86 |
r = requests.get(self.URL, params=params)
|
|
89 | 87 |
except Exception, e: |
90 | 88 |
logger.error('unable to urlopen %s: %s', self.URL, e) |
91 | 89 |
raise OVHError('unable to urlopen %s: %s' % (self.URL, e)) |
92 | 90 |
else: |
93 |
result = json.loads(stream.read())
|
|
94 |
if 100 <= result['status'] < 200:
|
|
91 |
result = r.json()
|
|
92 |
if 100 <= r.status_code < 200:
|
|
95 | 93 |
credit_left = float(result['creditLeft']) |
96 | 94 |
# update credit left |
97 | 95 |
OVHSMSGateway.objects.filter(id=self.id) \ |
passerelle/apps/oxyd/models.py | ||
---|---|---|
1 | 1 |
import re |
2 |
import urllib |
|
3 |
import urllib2 |
|
4 | 2 |
import logging |
5 |
import json
|
|
3 |
import requests
|
|
6 | 4 | |
7 | 5 |
from django.db import models |
8 | 6 |
from django.core.urlresolvers import reverse |
... | ... | |
54 | 52 |
destinations = self.clean_numbers(destinations, |
55 | 53 |
self.default_country_code, prefix='') |
56 | 54 |
for dest in destinations: |
57 |
params = urllib.urlencode({ |
|
58 |
'id': self.username, |
|
59 |
'pass': self.password, |
|
60 |
'num': number, |
|
61 |
'sms': text.encode('utf-8'), |
|
62 |
'flash': '0' |
|
63 |
}) |
|
55 |
params = {'id': self.username, |
|
56 |
'pass': self.password, |
|
57 |
'num': number, |
|
58 |
'sms': text.encode('utf-8'), |
|
59 |
'flash': '0' |
|
60 |
} |
|
64 | 61 |
try: |
65 |
r = urllib2.urlopen('http://sms.oxyd.fr/send.php', params) |
|
66 |
except Exception, e: |
|
67 |
# XXX: add proper handling of errors |
|
68 |
logger.error('urlopen oxyd.fr failed : %s', e) |
|
69 |
raise OxydError('urlopen oxyd.fr failed : %s' % e) |
|
70 |
else: |
|
71 |
line = r.read() |
|
72 |
code = line.split()[0] |
|
73 |
if code != '200': |
|
74 |
logger.error('OXYD error: %r', line) |
|
75 |
raise OxydError('OXYD error: %r' % line) |
|
76 |
r.close() |
|
62 |
r = requests.get('http://sms.oxyd.fr/send.php', params=params) |
|
63 |
except requests.ConnectionError: |
|
64 |
logger.error('open oxyd.fr failed : %s', e) |
|
65 |
raise OxydError('open oxyd.fr failed : %s' % e) |
|
66 |
if not r.ok(): |
|
67 |
logger.error('OXYD error: %r', r.content) |
|
68 |
raise OxydError('OXYD error: %r' % r.content) |
|
77 | 69 | |
78 | 70 |
def get_sms_left(self, type='standard'): |
79 | 71 |
raise NotImplementedError |
passerelle/repost/models.py | ||
---|---|---|
1 | 1 |
import json |
2 |
from urllib2 import Request, urlopen |
|
2 |
import requests |
|
3 | 3 | |
4 | 4 |
from django.db import models |
5 | 5 |
from passerelle.base.models import BaseResource |
... | ... | |
9 | 9 |
timeout = models.IntegerField(null=True, blank=True) |
10 | 10 | |
11 | 11 |
def repost(self, data, **kwargs): |
12 |
r = Request(self.url) |
|
13 |
r.add_header('Accept', 'application/json') |
|
14 |
r.add_header('Content-Type', 'application/json;charset=UTF-8') |
|
15 |
r.add_data(json.dumps(data)) |
|
12 |
headers = {'Accept': 'application/json', |
|
13 |
'Content-Type': 'application/json;charset=UTF-8'} |
|
16 | 14 |
if self.timeout: |
17 |
p = urlopen(r, timeout=self.timeout)
|
|
15 |
r = requests.get(self.url, data=data, timeout=self.timeout)
|
|
18 | 16 |
else: |
19 |
p = urlopen(r) |
|
20 |
out_data = json.loads(p.read()) |
|
21 |
p.close() |
|
17 |
r = requests.get(self.url, data=data) |
|
18 |
return r.json() |
|
22 | 19 |
return out_data |
23 |
- |