grape-batch 2.1.1 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/CHANGELOG.md +6 -0
- data/README.md +45 -0
- data/lib/grape/{batch.rb → base.rb} +21 -5
- data/lib/grape/batch/version.rb +1 -1
- data/spec/api.rb +32 -0
- data/spec/grape/batch/base_spec.rb +39 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb4dfcf0e90958b52990dfe1baae968e533b2ac8
|
4
|
+
data.tar.gz: ee69288ac014717af2465e68358007cb055be8a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea924509379e820daf936ac90f5c030fbd72ebc7c66aa5119d6354d14070039762c0a8bf141b07bffcf12de32d14b699a6937c5ad7607ab0097b6d8ba76e7f74
|
7
|
+
data.tar.gz: a48b02990c38dadd8dd00ddc3ef6a861ecf904a1f8cc71caacc9a7247dae81616d4848afd0c0ded73713224285c7d8f0214eacdb61bcca0048723575b86a4fce
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.2.2
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -100,6 +100,7 @@ POST http request on the default URL with a similar body:
|
|
100
100
|
'body' is optional.
|
101
101
|
|
102
102
|
### Sessions
|
103
|
+
#### Single authentication
|
103
104
|
It's possible ensure a single session during the execution of the batch. You have to specify your session Proc. Before running the batch, the Proc is executed (with env as argument) and stored in rack env 'api.session' key.
|
104
105
|
```ruby
|
105
106
|
# Example
|
@@ -120,6 +121,50 @@ module AuthHelpers
|
|
120
121
|
end
|
121
122
|
```
|
122
123
|
|
124
|
+
#### Authentication during request batch
|
125
|
+
It is possible to either keep a token (or a session) generated by a request of the batch and pass it to the following ones.
|
126
|
+
```ruby
|
127
|
+
# Token example
|
128
|
+
# Considering this API
|
129
|
+
module Twitter
|
130
|
+
class API < Grape::API
|
131
|
+
version 'v1', using: :path
|
132
|
+
format :json
|
133
|
+
prefix 'api'
|
134
|
+
|
135
|
+
resource :session do
|
136
|
+
get do
|
137
|
+
# route logic
|
138
|
+
header 'HTTP_X_API_TOKEN', 'my_fresh_token'
|
139
|
+
# some other logic
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# This route will return a token header. The header will be kept and passed to the following requests by Grape::Batch. The header will not be added to the final batch response.
|
145
|
+
```
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
# Session example
|
149
|
+
# Considering this API
|
150
|
+
module Twitter
|
151
|
+
class API < Grape::API
|
152
|
+
version 'v1', using: :path
|
153
|
+
format :json
|
154
|
+
prefix 'api'
|
155
|
+
|
156
|
+
resource :session do
|
157
|
+
get do
|
158
|
+
# route logic
|
159
|
+
header 'api.session', OpenStruct(id: '123456')
|
160
|
+
# some other logic
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# This route will return a session object. The header will be kept and passed to the following requests by Grape::Batch. The header will not be added to the final batch response.
|
166
|
+
```
|
167
|
+
|
123
168
|
## Contributing
|
124
169
|
|
125
170
|
1. Fork it ( https://github.com/c4mprod/grape-batch/fork )
|
@@ -11,6 +11,9 @@ module Grape
|
|
11
11
|
module Batch
|
12
12
|
# Gem main class
|
13
13
|
class Base
|
14
|
+
SESSION_HEADER = 'api.session'.freeze
|
15
|
+
TOKEN_HEADER = 'HTTP_X_API_TOKEN'.freeze
|
16
|
+
|
14
17
|
def initialize(app)
|
15
18
|
@app = app
|
16
19
|
@response_klass = Grape::Batch.configuration.formatter
|
@@ -49,19 +52,32 @@ module Grape
|
|
49
52
|
end
|
50
53
|
|
51
54
|
def dispatch(env, batch_requests)
|
52
|
-
# Call session proc
|
53
|
-
env['api.session'] = @session_proc.call(env)
|
54
|
-
|
55
55
|
# Prepare batch request env
|
56
|
-
request_env = env.dup
|
56
|
+
@request_env = env.dup
|
57
|
+
# Call session proc
|
58
|
+
@request_env[SESSION_HEADER] = @session_proc.call(@request_env)
|
57
59
|
|
58
60
|
# Call batch request
|
59
61
|
batch_requests.map do |batch_request|
|
60
|
-
batch_env = Grape::Batch::Request.new(request_env, batch_request).build
|
62
|
+
batch_env = Grape::Batch::Request.new(@request_env, batch_request).build
|
61
63
|
status, headers, response = @app.call(batch_env)
|
64
|
+
|
65
|
+
update_request_env_session_from_headers(headers)
|
66
|
+
update_request_env_token_from_headers(headers)
|
67
|
+
|
62
68
|
@response_klass.format(status, headers, response)
|
63
69
|
end
|
64
70
|
end
|
71
|
+
|
72
|
+
def update_request_env_session_from_headers(headers)
|
73
|
+
return if !headers[SESSION_HEADER] || @request_env[SESSION_HEADER]
|
74
|
+
@request_env[SESSION_HEADER] = headers[SESSION_HEADER].dup
|
75
|
+
end
|
76
|
+
|
77
|
+
def update_request_env_token_from_headers(headers)
|
78
|
+
return if !headers[TOKEN_HEADER] || @request_env[TOKEN_HEADER]
|
79
|
+
@request_env[TOKEN_HEADER] = headers[TOKEN_HEADER].dup
|
80
|
+
end
|
65
81
|
end
|
66
82
|
end
|
67
83
|
end
|
data/lib/grape/batch/version.rb
CHANGED
data/spec/api.rb
CHANGED
@@ -57,6 +57,38 @@ module Twitter
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
+
resource :login do
|
61
|
+
get do
|
62
|
+
header 'HTTP_X_API_TOKEN', 'user_token'
|
63
|
+
|
64
|
+
'login successful'
|
65
|
+
end
|
66
|
+
|
67
|
+
post do
|
68
|
+
if env['HTTP_X_API_TOKEN'] == 'user_token'
|
69
|
+
'token valid'
|
70
|
+
else
|
71
|
+
'token invalid'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
resource :session do
|
77
|
+
get do
|
78
|
+
header 'api.session', OpenStruct.new(nick: 'Bob')
|
79
|
+
|
80
|
+
'session reloaded'
|
81
|
+
end
|
82
|
+
|
83
|
+
post do
|
84
|
+
if env['api.session'] && env['api.session'].nick == 'Bob'
|
85
|
+
'session valid'
|
86
|
+
else
|
87
|
+
'session invalid'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
60
92
|
# 404
|
61
93
|
#
|
62
94
|
route :any, '*path' do
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'rack/test'
|
3
3
|
require 'grape'
|
4
|
-
require 'grape/
|
4
|
+
require 'grape/base'
|
5
5
|
require 'api'
|
6
6
|
|
7
7
|
RSpec.describe Grape::Batch::Base do
|
@@ -177,6 +177,44 @@ RSpec.describe Grape::Batch::Base do
|
|
177
177
|
it { expect(decode(response.body).size).to eq(2) }
|
178
178
|
end
|
179
179
|
end
|
180
|
+
|
181
|
+
describe 'single session' do
|
182
|
+
describe 'without token' do
|
183
|
+
let(:request_1) { { method: 'POST', path: '/api/v1/login' } }
|
184
|
+
let(:request_body) { encode(requests: [request_1]) }
|
185
|
+
it { expect(response.status).to eq(200) }
|
186
|
+
it { expect(response.body).to eq(encode([{ success: 'token invalid' }])) }
|
187
|
+
it { expect(response.headers).to_not include('HTTP_X_API_TOKEN') }
|
188
|
+
end
|
189
|
+
|
190
|
+
describe 'with a token' do
|
191
|
+
let(:request_1) { { method: 'GET', path: '/api/v1/login' } }
|
192
|
+
let(:request_2) { { method: 'POST', path: '/api/v1/login' } }
|
193
|
+
let(:request_body) { encode(requests: [request_1, request_2]) }
|
194
|
+
let(:expected_response) { [{ success: 'login successful' }, { success: 'token valid' }] }
|
195
|
+
it { expect(response.status).to eq(200) }
|
196
|
+
it { expect(response.body).to eq(encode(expected_response)) }
|
197
|
+
it { expect(response.headers).to_not include('HTTP_X_API_TOKEN') }
|
198
|
+
end
|
199
|
+
|
200
|
+
describe 'without session' do
|
201
|
+
let(:request_1) { { method: 'POST', path: '/api/v1/session' } }
|
202
|
+
let(:request_body) { encode(requests: [request_1]) }
|
203
|
+
it { expect(response.status).to eq(200) }
|
204
|
+
it { expect(response.body).to eq(encode([{ success: 'session invalid' }])) }
|
205
|
+
it { expect(response.headers).to_not include('api.session') }
|
206
|
+
end
|
207
|
+
|
208
|
+
describe 'with a session' do
|
209
|
+
let(:request_1) { { method: 'GET', path: '/api/v1/session' } }
|
210
|
+
let(:request_2) { { method: 'POST', path: '/api/v1/session' } }
|
211
|
+
let(:request_body) { encode(requests: [request_1, request_2]) }
|
212
|
+
let(:expected_response) { [{ success: 'session reloaded' }, { success: 'session valid' }] }
|
213
|
+
it { expect(response.status).to eq(200) }
|
214
|
+
it { expect(response.body).to eq(encode(expected_response)) }
|
215
|
+
it { expect(response.headers).to_not include('api.session') }
|
216
|
+
end
|
217
|
+
end
|
180
218
|
end
|
181
219
|
|
182
220
|
describe '#configure' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-batch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lionel Oto
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2016-01-11 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: multi_json
|
@@ -145,7 +145,7 @@ files:
|
|
145
145
|
- README.md
|
146
146
|
- Rakefile
|
147
147
|
- grape-batch.gemspec
|
148
|
-
- lib/grape/
|
148
|
+
- lib/grape/base.rb
|
149
149
|
- lib/grape/batch/configuration.rb
|
150
150
|
- lib/grape/batch/converter.rb
|
151
151
|
- lib/grape/batch/errors.rb
|
@@ -177,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
177
|
version: '0'
|
178
178
|
requirements: []
|
179
179
|
rubyforge_project:
|
180
|
-
rubygems_version: 2.
|
180
|
+
rubygems_version: 2.4.8
|
181
181
|
signing_key:
|
182
182
|
specification_version: 4
|
183
183
|
summary: Extends Grape::API to support request batching
|