From 37c2adf3d588700be34a95c838c530013a0f9cb2 Mon Sep 17 00:00:00 2001 From: Andreas Hellwig Date: Mon, 8 Nov 2021 09:06:37 +0100 Subject: [PATCH] Use etags from create and update --- lib/ioki/apis/endpoints/create.rb | 7 +++-- lib/ioki/apis/endpoints/update.rb | 7 +++-- spec/ioki/apis/endpoints/create_spec.rb | 6 ++++- spec/ioki/apis/endpoints/update_spec.rb | 6 ++++- spec/ioki/driver_api_spec.rb | 2 +- spec/ioki/passenger_api_spec.rb | 17 ++++++------ spec/ioki/platform_api_spec.rb | 35 +++++++++++-------------- 7 files changed, 41 insertions(+), 39 deletions(-) diff --git a/lib/ioki/apis/endpoints/create.rb b/lib/ioki/apis/endpoints/create.rb index 06a85c70..3fce5c41 100644 --- a/lib/ioki/apis/endpoints/create.rb +++ b/lib/ioki/apis/endpoints/create.rb @@ -28,11 +28,10 @@ def call(client, model, args = [], options = {}) outgoing_model_class = @outgoing_model_class || options[:outgoing_model_class] || model_class unless model.is_a?(outgoing_model_class) - raise(ArgumentError, - "#{model} is not an instance of #{outgoing_model_class}") + raise(ArgumentError, "#{model} is not an instance of #{outgoing_model_class}") end - parsed_response, = client.request( + parsed_response, response = client.request( url: client.build_request_url(*Endpoints.url_elements(name, full_path, *args)), method: :post, body: { data: model.serialize(:create) }.to_json, @@ -40,7 +39,7 @@ def call(client, model, args = [], options = {}) params: options[:params] ) - model_class.new(parsed_response['data']) + model_class.new(parsed_response['data'], response.headers[:etag]) end end end diff --git a/lib/ioki/apis/endpoints/update.rb b/lib/ioki/apis/endpoints/update.rb index 1fdf632c..f7be7d75 100644 --- a/lib/ioki/apis/endpoints/update.rb +++ b/lib/ioki/apis/endpoints/update.rb @@ -28,11 +28,10 @@ def call(client, model, args = [], options = {}) outgoing_model_class = @outgoing_model_class || options[:outgoing_model_class] || model_class unless model.is_a?(outgoing_model_class) - raise(ArgumentError, - "#{model} is not an instance of #{outgoing_model_class}") + raise(ArgumentError, "#{model} is not an instance of #{outgoing_model_class}") end - parsed_response, = client.request( + parsed_response, response = client.request( url: client.build_request_url(*Endpoints.url_elements(name, full_path, *args)), method: :patch, body: { data: model.serialize(:update) }.to_json, @@ -40,7 +39,7 @@ def call(client, model, args = [], options = {}) params: options[:params] ) - model_class.new(parsed_response['data']) + model_class.new(parsed_response['data'], response.headers[:etag]) end end end diff --git a/spec/ioki/apis/endpoints/create_spec.rb b/spec/ioki/apis/endpoints/create_spec.rb index 14d2835b..653f5f05 100644 --- a/spec/ioki/apis/endpoints/create_spec.rb +++ b/spec/ioki/apis/endpoints/create_spec.rb @@ -20,7 +20,10 @@ headers: client.all_headers, params: params ).and_return( - { 'data' => { 'id' => '0815', name: 'attributes altered by server' } } + [ + { 'data' => { 'id' => '0815', name: 'attributes altered by server' } }, + instance_double(Faraday::Response, headers: { etag: 'ETAG' }) + ] ) result = endpoint.call(client, model, [], params: params) @@ -29,6 +32,7 @@ expect(result).not_to eq(model) expect(result.id).to eq('0815') expect(result.name).to eq('attributes altered by server') + expect(result._etag).to eq('ETAG') end context 'when the passed model instance has the wrong class' do diff --git a/spec/ioki/apis/endpoints/update_spec.rb b/spec/ioki/apis/endpoints/update_spec.rb index a8d3d8f0..aaf9ed1b 100644 --- a/spec/ioki/apis/endpoints/update_spec.rb +++ b/spec/ioki/apis/endpoints/update_spec.rb @@ -21,7 +21,10 @@ headers: client.all_headers, params: params ).and_return( - { 'data' => { 'id' => '0815', name: 'attributes altered by server' } } + [ + { 'data' => { 'id' => '0815', name: 'attributes altered by server' } }, + instance_double(Faraday::Response, headers: { etag: 'ETAG' }) + ] ) result = endpoint.call(client, model, ['0815'], model_class: model_class, params: params) @@ -30,6 +33,7 @@ expect(result).not_to eq(model) expect(result.id).to eq('0815') expect(result.name).to eq('attributes altered by server') + expect(result._etag).to eq('ETAG') end context 'when the passed model instance has the wrong class' do diff --git a/spec/ioki/driver_api_spec.rb b/spec/ioki/driver_api_spec.rb index c689ed20..6f893d6c 100644 --- a/spec/ioki/driver_api_spec.rb +++ b/spec/ioki/driver_api_spec.rb @@ -30,7 +30,7 @@ it 'calls request on the client with expected params' do expect(driver_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/driver/request_tokens') - result_with_data + [result_with_data, full_response] end expect(driver_client.create_request_token(request_token, options)). diff --git a/spec/ioki/passenger_api_spec.rb b/spec/ioki/passenger_api_spec.rb index e7aad6c2..f163af85 100644 --- a/spec/ioki/passenger_api_spec.rb +++ b/spec/ioki/passenger_api_spec.rb @@ -78,7 +78,7 @@ it 'calls request on the client with expected params' do expect(passenger_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/ride_inquiry') - result_with_data + [result_with_data, full_response] end expect(passenger_client.create_ride_inquiry(ride_inquiry, options)). @@ -92,7 +92,7 @@ it 'calls request on the client with expected params' do expect(passenger_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/rides') - result_with_data + [result_with_data, full_response] end expect(passenger_client.create_ride(ride, options)). @@ -120,7 +120,7 @@ }.to_json ) - result_with_data + [result_with_data, full_response] end expect(passenger_client.create_cancellation(ride, cancellation)). @@ -136,7 +136,7 @@ expect(passenger_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/rides/RIDE_ID/booking') expect(params[:body]).to eq '{"data":{"ride_version":2,"payment_method":null}}' - result_with_data + [result_with_data, full_response] end expect(passenger_client.create_booking(ride, booking, options)). @@ -152,7 +152,7 @@ it 'calls request on the client with expected params' do expect(passenger_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/phone_verification_requests') - result_with_data + [result_with_data, full_response] end expect(passenger_client.create_phone_verification_request(phone_verification_request, options)). @@ -166,7 +166,7 @@ it 'calls request on the client with expected params' do expect(passenger_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/user/phone_number') - result_with_data + [result_with_data, full_response] end expect(passenger_client.update_user_phone_number(user_phone_number, options.merge(model: user_phone_number))). @@ -185,8 +185,7 @@ expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/user') expect(JSON.parse(params[:body])['data']). to match(hash_including('first_name' => 'Lupe', 'last_name' => 'Smiles')) - - result_with_data + [result_with_data, full_response] end expect(passenger_client.update_user(user, options)). @@ -212,7 +211,7 @@ it 'calls request on the client with expected params' do expect(passenger_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/rides/RIDE_ID/rating') - result_with_data + [result_with_data, full_response] end expect(passenger_client.create_rating(ride, rating, diff --git a/spec/ioki/platform_api_spec.rb b/spec/ioki/platform_api_spec.rb index 8751c640..77e78b3c 100644 --- a/spec/ioki/platform_api_spec.rb +++ b/spec/ioki/platform_api_spec.rb @@ -102,7 +102,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/stations') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_station('0815', station, options)). @@ -117,7 +117,7 @@ expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/stations/4711') expect(params[:method]).to eq :patch - result_with_data + [result_with_data, full_response] end expect(platform_client.update_station('0815', station, options)). @@ -167,7 +167,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/vehicles') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_vehicle('0815', vehicle, options)). @@ -181,7 +181,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/vehicles/4711') - result_with_data + [result_with_data, full_response] end expect(platform_client.update_vehicle('0815', vehicle, options)). @@ -231,7 +231,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/rides') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_ride('0815', ride, options)). @@ -269,7 +269,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/providers/0815/users') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_user('0815', user, options)). @@ -283,7 +283,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/providers/0815/users/4711') - result_with_data + [result_with_data, full_response] end expect(platform_client.update_user('0815', user, options)). @@ -333,7 +333,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/drivers') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_driver('0815', driver, options)). @@ -347,8 +347,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/drivers/4711') - - result_with_data + [result_with_data, full_response] end expect(platform_client.update_driver('0815', driver, options)). @@ -398,7 +397,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/driver_vehicle_connections') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_driver_vehicle_connection('0815', driver_vehicle_connection, options)). @@ -412,8 +411,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/driver_vehicle_connections/4711') - - result_with_data + [result_with_data, full_response] end expect(platform_client.update_driver_vehicle_connection('0815', driver_vehicle_connection, options)). @@ -475,7 +473,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/rides/4711/rating') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_rating('0815', rating, options)). @@ -489,8 +487,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/rides/4711/rating/1337') - - result_with_data + [result_with_data, full_response] end expect(platform_client.update_rating('0815', '4711', rating, options)). @@ -516,7 +513,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/rides/4711/booking') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_booking('0815', '4711', booking, options)). @@ -530,7 +527,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/providers/0815/users/4711/request_tokens') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_request_token('0815', '4711', passenger_request_token, options)). @@ -544,7 +541,7 @@ it 'calls request on the client with expected params' do expect(platform_client).to receive(:request) do |params| expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/ride_inquiry') - result_with_data + [result_with_data, full_response] end expect(platform_client.create_ride_inquiry('0815', ride_inquiry, options)).