15
15
"""
16
16
17
17
import json
18
- import re
19
- from poster .encode import multipart_encode
20
- from poster .streaminghttp import register_openers
21
- from httplib2 import Http
18
+ import re , logging
19
+ import requests
22
20
from urllib import urlencode
23
- from urllib2 import Request , urlopen
21
+
22
+
23
+ DEBUG = False
24
+
24
25
25
26
class SocrataBase :
26
27
"""Base class for all Socrata API objects"""
@@ -32,26 +33,25 @@ def __init__(self, configuration):
32
33
"""
33
34
34
35
self .config = configuration
35
- self .username , password , host = (self .config .get ('credentials' , 'user' ),
36
+ self .username , self . password , self . host = (self .config .get ('credentials' , 'user' ),
36
37
self .config .get ('credentials' , 'password' ),
37
38
self .config .get ('server' , 'host' ))
38
39
39
40
self .app_token = self .config .get ('credentials' , 'app_token' )
40
- self .api = Http ()
41
- self .url = host
41
+ self .url = self .host
42
42
self .id_pattern = re .compile ('^[0-9a-z]{4}-[0-9a-z]{4}$' )
43
- self .api .add_credentials (self .username , password )
44
-
45
- # For multipart upload/streaming
46
- register_openers ()
47
43
48
- def _request (self , service , type , data = {}):
44
+ def _request (self , service , type = 'GET' , data = {}):
49
45
"""Generic HTTP request, encoding data as JSON and decoding the response"""
50
- response , content = self .api .request (
51
- self .url + service , type ,
52
- headers = { 'Content-type:' : 'application/json' ,
46
+ client = getattr (requests , type .lower ())
47
+ uri = self .url + service
48
+ logging .warning (uri )
49
+ response = client (uri + service ,
50
+ headers = { 'Content-type:' : 'application/json' ,
53
51
'X-App-Token' : self .app_token },
54
- body = json .dumps (data ) )
52
+ data = json .dumps (data ), auth = (self .username , self .password ))
53
+
54
+ content = response .text
55
55
if content != None and len (content ) > 0 :
56
56
response_parsed = json .loads (content )
57
57
if hasattr (response_parsed , 'has_key' ) and \
@@ -167,8 +167,8 @@ def multipart_post(self, url, filename, field='file'):
167
167
headers ['X-App-Token' ] = self .app_token
168
168
169
169
request = Request ("%s%s" % (self .url , url ), datagen , headers )
170
- response = urlopen (request ).read ()
171
- return json .loads (response )
170
+ response = request . get (request ).read ()
171
+ return json .loads (response . text )
172
172
173
173
# Is the string 'id' a valid four-four ID?
174
174
def is_id (self , id ):
0 commit comments