mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
API, batch: remove simplejson refs, fix linting errors
This commit is contained in:
@@ -3,23 +3,18 @@
|
||||
import io
|
||||
from urlparse import urlparse
|
||||
import json
|
||||
|
||||
import pkg_resources
|
||||
pkg_resources.require( "Paste" )
|
||||
from paste import httpexceptions
|
||||
|
||||
import pprint
|
||||
import logging
|
||||
log = logging.getLogger( __name__ )
|
||||
|
||||
|
||||
class BatchMiddleware(object):
|
||||
class BatchMiddleware( object ):
|
||||
"""
|
||||
"""
|
||||
DEFAULT_CONFIG = {
|
||||
'route' : '/api/batch'
|
||||
}
|
||||
# TODO: whitelist or blacklisted urls
|
||||
|
||||
def __init__( self, galaxy, application, config=None ):
|
||||
#: the original galaxy webapp
|
||||
@@ -35,42 +30,25 @@ class BatchMiddleware(object):
|
||||
return self.application( environ, start_response )
|
||||
|
||||
def process_batch_requests( self, batch_environ, start_response ):
|
||||
print '=' * 40, 'BATCH_REQUEST'
|
||||
pprint.pprint( batch_environ )
|
||||
print
|
||||
print 'batch environ body:', batch_environ[ 'wsgi.input' ]
|
||||
|
||||
payload = self._read_post_payload( batch_environ )
|
||||
print 'payload:', payload
|
||||
requests = payload.get( 'batch', [] )
|
||||
print 'requests:', requests
|
||||
|
||||
responses = []
|
||||
for request in requests:
|
||||
if not self._valid( request ):
|
||||
continue
|
||||
|
||||
print '-' * 40, 'REQUEST BEGIN'
|
||||
request_environ = self._build_request_environ( batch_environ, request )
|
||||
pprint.pprint( request_environ )
|
||||
response = self._proccess_batch_request( request, request_environ, start_response )
|
||||
print '-' * 40, 'RESPONSE'
|
||||
pprint.pprint( response )
|
||||
responses.append( response )
|
||||
print '-' * 40, 'REQUEST END'
|
||||
|
||||
# log.debug( 'batch_response_body: %s', pprint.pformat( responses ) )
|
||||
batch_response_body = json.dumps( responses )
|
||||
start_response( '200 OK', [
|
||||
( 'Content-Length', len( batch_response_body ) ),
|
||||
( 'Content-Type', 'application/json' ),
|
||||
])
|
||||
print '=' * 40
|
||||
return batch_response_body
|
||||
|
||||
def _valid( self, request ):
|
||||
return True
|
||||
|
||||
def _read_post_payload( self, environ ):
|
||||
request_body_size = int( environ.get( 'CONTENT_LENGTH', 0 ) )
|
||||
request_body = environ[ 'wsgi.input' ].read( request_body_size ) or '{}'
|
||||
@@ -79,6 +57,9 @@ class BatchMiddleware(object):
|
||||
payload = json.loads( request_body )
|
||||
return payload
|
||||
|
||||
def _valid( self, request ):
|
||||
return True
|
||||
|
||||
def _build_request_environ( self, original_environ, request ):
|
||||
"""
|
||||
Given a request and the original environ used to call the batch, return
|
||||
@@ -109,8 +90,6 @@ class BatchMiddleware(object):
|
||||
return request_environ
|
||||
|
||||
def _proccess_batch_request( self, request, environ, start_response ):
|
||||
# log.debug( ( '.' * 40 ) )
|
||||
|
||||
# We may need to include middleware to record various reponses, but this way of doing that won't work:
|
||||
# status, headers, body = self.application( environ, start_response, body_renderer=self.body_renderer )
|
||||
|
||||
@@ -124,7 +103,6 @@ class BatchMiddleware(object):
|
||||
except httpexceptions.HTTPNotFound:
|
||||
response = dict( status=404, headers=self._default_headers(), body={} )
|
||||
return response
|
||||
# log.debug( ( '.' * 40 ) )
|
||||
|
||||
def body_renderer( self, trans, body, environ, start_response ):
|
||||
# this is a dummy renderer that does not call start_response
|
||||
|
||||
@@ -767,10 +767,10 @@ def wrap_in_middleware( app, global_conf, **local_conf ):
|
||||
app = RequestIDMiddleware( app )
|
||||
log.debug( "Enabling 'Request ID' middleware" )
|
||||
|
||||
# batch call processing middleware
|
||||
# api batch call processing middleware
|
||||
from galaxy.web.framework.middleware.batch import BatchMiddleware
|
||||
app = BatchMiddleware( webapp, app, {})
|
||||
log.debug( "Enabling 'Batch' middleware" );
|
||||
log.debug( "Enabling 'Batch' middleware" )
|
||||
|
||||
return app
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import simplejson
|
||||
import json
|
||||
from requests import post
|
||||
import pprint
|
||||
|
||||
@@ -18,7 +18,7 @@ class ApiBatchTestCase( api.ApiTestCase ):
|
||||
return url + '?key=' + self._get_api_key( admin=admin )
|
||||
|
||||
def _post_batch( self, batch ):
|
||||
data = simplejson.dumps({ "batch" : batch })
|
||||
data = json.dumps({ "batch" : batch })
|
||||
return post( "%s/batch" % ( self.galaxy_interactor.api_url ), data=data )
|
||||
|
||||
def log_reponse( self, response ):
|
||||
@@ -28,7 +28,7 @@ class ApiBatchTestCase( api.ApiTestCase ):
|
||||
batch = [
|
||||
dict( url=self._with_key( '/api/histories' ) ),
|
||||
dict( url=self._with_key( '/api/histories' ),
|
||||
method='POST', body=simplejson.dumps( dict( name='Wat' ) ) ),
|
||||
method='POST', body=json.dumps( dict( name='Wat' ) ) ),
|
||||
dict( url=self._with_key( '/api/histories' ) ),
|
||||
]
|
||||
response = self._post_batch( batch )
|
||||
|
||||
Reference in New Issue
Block a user