arsduo / batch_api Goto Github PK
View Code? Open in Web Editor NEWA Facebook-style, RESTful Batch API
License: MIT License
A Facebook-style, RESTful Batch API
License: MIT License
There should be an integration with airbrake if it's available, so you get errors reported. Can we use it as is, or do we need special code?
What about Rails 4 and Ruby 2.1. When i'am implement batch_api and rails 4 app. PUT method in ops[] response 404, GET and POST method - ok.
my query:
{"ops":
[
{"method": "PUT",
"url": "/api/stock_locations/1/stock_items/1",
"params":{"stock_item":{"count_on_hand":"2"}}
},
{"method": "get","url": "/api/stock_locations/1/stock_items/1"}
],
"sequential": "true"}
UPDATED:
params on rails controller after batch_api
{"stock_item"=>{"count_on_hand"=>"2"}}
params on rails controller after direct request
{"stock_item"=>{"count_on_hand"=>"2"},
"format"=>"json",
"action"=>"update",
"controller"=>"spree/api/stock_items",
"stock_location_id"=>"1",
"id"=>"2"}
PR: #31
piggy back on Celluloid reactor.
I am using batch_api with a Grape API in a Rails app. I found that all batch requests processed normally except for those using query string parameters. For example, this request would work:
{
"ops": [
{"method": "get", "url": "/api/v1/model"}
],
"sequential": true
}
The response:
{
"results": [
{
"body": "[{:one=>1}]",
"headers": {
"Content-Type": "application/json"
},
"status": 200
}
]
}
The same request with a query string fails:
{
"ops": [
{"method": "get", "url": "/api/v1/model?per_page=5"}
],
"sequential": true
}
Returns:
{
"results": [
{
"body": "Not Found",
"headers": {
"X-Cascade": "pass"
},
"status": 404
}
]
}
This was true whether the query string had values or not, i.e., /api/v1/model?
also failed without any parameters.
I inspected the Rack env
variable in a batch request with a query string. env['PATH_INFO'] = '/api/v1/model?per_page=5'
is the value, and this value does not work. If there is a query string appended to PATH_INFO
then the call will fail. When I reset PATH_INFO
to remove the query string the batch call completed successfully.
The other difference I saw was that rack.request.query_hash
was empty, but this didn't appear to impact the request.
This looks like it can be fixed by setting ORIGINAL_FULLPATH
and PATH_INFO
separately in https://github.com/arsduo/batch_api/blob/master/lib/batch_api/operation/rack.rb#L65.
@env["ORIGINAL_FULLPATH"] = @url
@env["PATH_INFO"] = @url.split('?').first
@arsduo Do you know if there are any unforeseen consequences from this change? Would you accept a PR for this issue?
For example, I tried this request by reference to https://developers.facebook.com/docs/graph-api/making-multiple-requests/
curl -F 'ops=[{"method":"GET", "url":"/api/tags"},{"method":"GET", "url":"/api/notes"}]' -F 'sequential=1' http://localhost:3000/batch
Then, I got this error
"Only 50 operations can be submitted at once, 77 were provided"
By changing like this, it works well.
module BatchApi
class Processor
.....
def process_ops
ops = @request.params.delete("ops")
ops = JSON.parse(ops) # ← json parse
if !ops || ops.empty?
raise Errors::NoOperationsError, "No operations provided"
elsif ops.length > BatchApi.config.limit
raise Errors::OperationLimitExceeded,
"Only #{BatchApi.config.limit} operations can be submitted at once, " +
"#{ops.length} were provided"
else
ops.map do |op|
self.class.operation_klass.new(op, @env, @app)
end
end
end
Sending a request by using jQuery post method to my rails app as below.
$.post('/batch',
{ops:[
{method: "get", url: "/api/tags"},
{method: "get", url: "/api/notes"}
],
sequential: true
},
function(){
console.log(arguments)
});
Then, I got this error
{"error":{"message":"can't convert String into Integer","backtrace":[" .... /batch_api/lib/batch_api/operation/rack.rb:16:in `[]' ...
By changing like this, it works well.
module BatchApi
class Processor
.....
def process_ops
ops = @request.params.delete("ops")
if !ops || ops.empty?
raise Errors::NoOperationsError, "No operations provided"
elsif ops.length > BatchApi.config.limit
raise Errors::OperationLimitExceeded,
"Only #{BatchApi.config.limit} operations can be submitted at once, " +
"#{ops.length} were provided"
else
ops.values.map do |op| # ← call values before map
self.class.operation_klass.new(op, @env, @app)
end
end
end
But it should work without any changes.
Would you give me some examples of requests?
Don't swallow errors silently, they need to go into the logs.
Should set to empty hash instead in BatchApi::Operations::Rack
@env["rack.request.query_hash"] = @method == "get" ? @params : {}
I cant see examples.
It would be great if you could define the opts
in such way that the response of a previously called API call is the input of a sequentially used call, like say using namespace :client_id
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.