batch_request_api 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/batch_request_api/batch_parallel.rb +42 -0
- data/lib/batch_request_api/batch_sequential.rb +37 -0
- data/lib/batch_request_api/batch_util.rb +19 -0
- data/lib/batch_request_api/engine.rb +6 -0
- data/lib/batch_request_api/middleware.rb +24 -0
- data/lib/batch_request_api/railtie.rb +7 -0
- data/lib/batch_request_api/version.rb +3 -0
- data/lib/batch_request_api.rb +8 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e25a4370fcce8e1299f9a93b7dc1c8b95eba32f6
|
4
|
+
data.tar.gz: a381a862083f339f8a2655a0a3717d0677135e64
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2a0eb5f254cd0e9b9f9dfa92cecd348c85c890da6f6cfc60e46f6230a2a6a7287a8a82964309b6047c67df67cb2c7585b9918cb829543e653c710274bb09e587
|
7
|
+
data.tar.gz: 694b1a0c633a6793a933459d9fc6d84df8a2f4a1f9728f5552daf944b16838fe42a221b506d825b191aafc03cd3f03a907f0058b0a363d7fcfe51b059ca243f7
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "batch_request_api/batch_util"
|
2
|
+
|
3
|
+
module BatchRequestApi
|
4
|
+
module BatchParallel
|
5
|
+
include BatchUtil
|
6
|
+
|
7
|
+
def batch_parallel(env)
|
8
|
+
requests = get_payload(env)
|
9
|
+
first_request = requests.first
|
10
|
+
# Post and Delete methods do not need any processing
|
11
|
+
set_id_on_record_for_patch(requests) if first_request['method'] == 'PATCH'
|
12
|
+
responses = process_parallel_request(env, first_request, requests)
|
13
|
+
build_response(responses)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def process_parallel_request(env, first_request, requests)
|
19
|
+
json_body = requests.map { |item| item['body'] }.to_json
|
20
|
+
setup_env(env, first_request, json_body)
|
21
|
+
handoff_to_rails(env)
|
22
|
+
end
|
23
|
+
|
24
|
+
def set_id_on_record_for_patch(requests)
|
25
|
+
ids = requests.map { |request| request['url'].match(%r((?<=\/)\d+$|(?<=\/)(\d+)(?=\.json$))).to_s }
|
26
|
+
requests.each_with_index do |request, index|
|
27
|
+
request['body']['data']['attributes']['id'] = ids[index] # dependency on json api spec
|
28
|
+
end
|
29
|
+
requests
|
30
|
+
end
|
31
|
+
|
32
|
+
def handoff_to_rails(env)
|
33
|
+
status, headers, body = @app.call(env)
|
34
|
+
body.close if body.respond_to? :close
|
35
|
+
if status == 200
|
36
|
+
{ status: status, headers: headers, body: JSON.parse(body.join) }
|
37
|
+
else
|
38
|
+
{ status: status, headers: headers, body: JSON.parse(body.body) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "batch_request_api/batch_util"
|
2
|
+
|
3
|
+
module BatchRequestApi
|
4
|
+
module BatchSequential
|
5
|
+
include BatchUtil
|
6
|
+
|
7
|
+
def batch_sequential(env)
|
8
|
+
requests = get_payload(env)
|
9
|
+
responses = requests.map do |item|
|
10
|
+
process_request(env.deep_dup, item)
|
11
|
+
end
|
12
|
+
build_response(response_hash(responses))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def process_request(env, item)
|
18
|
+
json_body = item['body'].to_json
|
19
|
+
setup_env(env, item, json_body)
|
20
|
+
handoff_to_rails(env)
|
21
|
+
end
|
22
|
+
|
23
|
+
def handoff_to_rails(env)
|
24
|
+
status, headers, body = @app.call(env)
|
25
|
+
body.close if body.respond_to? :close
|
26
|
+
if status == 200
|
27
|
+
{ status: status, headers: headers, response: JSON.parse(body.join) }
|
28
|
+
else
|
29
|
+
{ status: status, headers: headers, response: JSON.parse(body.body) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def response_hash(responses)
|
34
|
+
{ status: 200, headers: { 'Content-Type' => 'application/json' }, response: responses }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module BatchRequestApi
|
2
|
+
module BatchUtil
|
3
|
+
def get_payload(env)
|
4
|
+
request = Rack::Request.new(env.deep_dup)
|
5
|
+
payload = JSON.parse(request.body.read)
|
6
|
+
payload['requests']
|
7
|
+
end
|
8
|
+
|
9
|
+
def build_response(responses)
|
10
|
+
[200, { 'Content-Type' => 'application/json' }, [{ responses: responses }.to_json]]
|
11
|
+
end
|
12
|
+
|
13
|
+
def setup_env(env, item, json_body)
|
14
|
+
env['PATH_INFO'] = item['url']
|
15
|
+
env['REQUEST_METHOD'] = item['method']
|
16
|
+
env['rack.input'] = StringIO.new(json_body)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'batch_request_api/batch_parallel'
|
2
|
+
require 'batch_request_api/batch_sequential'
|
3
|
+
|
4
|
+
module BatchRequestApi
|
5
|
+
class Middleware
|
6
|
+
include BatchParallel
|
7
|
+
include BatchSequential
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
def initialize(app)
|
11
|
+
@app = app
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
if env['PATH_INFO'] == '/api/v1/batch_sequential'
|
16
|
+
batch_sequential(env)
|
17
|
+
elsif env['PATH_INFO'] == '/api/v1/batch_parallel'
|
18
|
+
batch_parallel(env)
|
19
|
+
else
|
20
|
+
@app.call(env) # regular RAILS CRUD
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: batch_request_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.12
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Srinivas Raghunathan
|
8
|
+
- Shishir Kakaraddi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2017-05-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '4.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '4.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.7'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.7'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '10.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '10.0'
|
56
|
+
description: Batch API middleware.
|
57
|
+
email:
|
58
|
+
- sraghunathan@netflix.com
|
59
|
+
- skakaraddi@netflix.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- lib/batch_request_api.rb
|
65
|
+
- lib/batch_request_api/batch_parallel.rb
|
66
|
+
- lib/batch_request_api/batch_sequential.rb
|
67
|
+
- lib/batch_request_api/batch_util.rb
|
68
|
+
- lib/batch_request_api/engine.rb
|
69
|
+
- lib/batch_request_api/middleware.rb
|
70
|
+
- lib/batch_request_api/railtie.rb
|
71
|
+
- lib/batch_request_api/version.rb
|
72
|
+
homepage: https://github.com/Netflix/batch_request_api
|
73
|
+
licenses:
|
74
|
+
- Apache-2.0
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.6.11
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Rails Batch Request API.
|
96
|
+
test_files: []
|