From 62efaf86693df814d8469ceb1813f2e2e434d073 Mon Sep 17 00:00:00 2001 From: Daniel Szatmari Date: Thu, 24 Oct 2019 22:25:56 +0200 Subject: [PATCH 1/5] wip --- lib/bookingsync/api.rb | 2 + lib/bookingsync/api/client.rb | 56 +++++++++++++++------------- lib/bookingsync/api/configuration.rb | 20 ++++++++++ 3 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 lib/bookingsync/api/configuration.rb diff --git a/lib/bookingsync/api.rb b/lib/bookingsync/api.rb index ed0d68f5..76548ae3 100644 --- a/lib/bookingsync/api.rb +++ b/lib/bookingsync/api.rb @@ -1,9 +1,11 @@ require "faraday" require "bookingsync/api/version" require "bookingsync/api/client" +require "bookingsync/api/configuration" module BookingSync module API + # configure {} # Return new API Client # # @param token [String] OAuth token diff --git a/lib/bookingsync/api/client.rb b/lib/bookingsync/api/client.rb index 2eb2ad18..f93ca6bf 100644 --- a/lib/bookingsync/api/client.rb +++ b/lib/bookingsync/api/client.rb @@ -133,46 +133,46 @@ def initialize(token, options = {}) # Make a HTTP GET request # # @param path [String] The path, relative to {#api_endpoint} - # @param options [Hash] Query params for the request + # @param payload [Hash] Query params for the request # @return [Array] Array of resources. - def get(path, options = {}) - request :get, path, query: options + def get(path, payload = {}) + request :get, path, query: payload end # Make a HTTP POST request # # @param path [String] The path, relative to {#api_endpoint} - # @param options [Hash] Body params for the request + # @param payload [Hash] Body params for the request # @return [Array] - def post(path, options = {}) - request :post, path, options + def post(path, payload = {}) + request :post, path, payload end # Make a HTTP PUT request # # @param path [String] The path, relative to {#api_endpoint} - # @param options [Hash] Body params for the request + # @param payload [Hash] Body params for the request # @return [Array] - def put(path, options = {}) - request :put, path, options + def put(path, payload = {}) + request :put, path, payload end # Make a HTTP PATCH request # # @param path [String] The path, relative to {#api_endpoint} - # @param options [Hash] Body params for the request + # @param payload [Hash] Body params for the request # @return [Array] - def patch(path, options = {}) - request :patch, path, options + def patch(path, payload = {}) + request :patch, path, payload end # Make a HTTP DELETE request # # @param path [String] The path, relative to {#api_endpoint} - # @param options [Hash] Body params for the request + # @param payload [Hash] Body params for the request # @return [Array] - def delete(path, options = {}) - request :delete, path, options + def delete(path, payload = {}) + request :delete, path, payload end # Return API endpoint @@ -202,13 +202,13 @@ def decode_body(str) # # @param method [Symbol] HTTP verb to use. # @param path [String] The path, relative to {#api_endpoint}. - # @param data [Hash] Data to be send in the request's body + # @param payload [Hash] payload to be send in the request's body # it can include query: key with requests params for GET requests # @param options [Hash] A customizable set of request options. # @return [Array] Array of resources. - def request(method, path, data = nil, options = nil) + def request(method, path, payload = nil, options = nil) instrument("request.bookingsync_api", method: method, path: path) do - response = call(method, path, data, options) + response = call(method, path, payload, options) response.respond_to?(:resources) ? response.resources : response end end @@ -244,15 +244,15 @@ def paginate(path, options = {}, &block) # # @param method [Symbol] HTTP verb to use. # @param path [String] The path, relative to {#api_endpoint}. - # @param data [Hash] Data to be send in the request's body + # @param payload [Hash] payload to be send in the request's body # it can include query: key with requests params for GET requests # @param options [Hash] A customizable set of request options. # @return [BookingSync::API::Response] A Response object. - def call(method, path, data = nil, options = nil) + def call(method, path, payload = nil, options = nil) instrument("call.bookingsync_api", method: method, path: path) do if [:get, :head].include?(method) - options = data - data = {} + options = payload + payload = {} end options ||= {} options[:headers] ||= {} @@ -272,13 +272,15 @@ def call(method, path, data = nil, options = nil) url = expand_url(path, options[:uri]) res = @conn.send(method, url) do |req| - if data - req.body = data.is_a?(String) ? data : encode_body(data) + if payload + req.body = payload.is_a?(String) ? payload : encode_body(payload) end if params = options[:query] req.params.update params end req.headers.update options[:headers] + p options + # req.options.timeout = options[:timeout] if options[:timeout] end handle_response(res) end @@ -308,7 +310,11 @@ def middleware end def faraday_options - { builder: middleware, ssl: { verify: verify_ssl? } } + { + builder: middleware, + ssl: { verify: verify_ssl? }, + request: { timeout: BookingSync::API.configuration.timeout } + } end # Return BookingSync base URL. Default is https://www.bookingsync.com diff --git a/lib/bookingsync/api/configuration.rb b/lib/bookingsync/api/configuration.rb new file mode 100644 index 00000000..4e8a5688 --- /dev/null +++ b/lib/bookingsync/api/configuration.rb @@ -0,0 +1,20 @@ +module BookingSync::API + class << self + attr_accessor :configuration + end + + def self.configure + self.configuration ||= Configuration.new + yield(configuration) if block_given? + end + + class Configuration + attr_accessor :timeout + + def initialize + @timeout = 20 + end + end + + configure +end From 7401340ac881dc6200ea00c3c3e6cd57179a7d7e Mon Sep 17 00:00:00 2001 From: Daniel Szatmari Date: Wed, 30 Oct 2019 15:43:05 +0100 Subject: [PATCH 2/5] revert to only initialiser --- lib/bookingsync/api.rb | 1 - lib/bookingsync/api/client.rb | 50 +++++++++++++++++------------------ 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/bookingsync/api.rb b/lib/bookingsync/api.rb index 76548ae3..acf76ad1 100644 --- a/lib/bookingsync/api.rb +++ b/lib/bookingsync/api.rb @@ -5,7 +5,6 @@ module BookingSync module API - # configure {} # Return new API Client # # @param token [String] OAuth token diff --git a/lib/bookingsync/api/client.rb b/lib/bookingsync/api/client.rb index f93ca6bf..e089db9b 100644 --- a/lib/bookingsync/api/client.rb +++ b/lib/bookingsync/api/client.rb @@ -133,46 +133,46 @@ def initialize(token, options = {}) # Make a HTTP GET request # # @param path [String] The path, relative to {#api_endpoint} - # @param payload [Hash] Query params for the request + # @param options [Hash] Query params for the request # @return [Array] Array of resources. - def get(path, payload = {}) - request :get, path, query: payload + def get(path, options = {}) + request :get, path, query: options end # Make a HTTP POST request # # @param path [String] The path, relative to {#api_endpoint} - # @param payload [Hash] Body params for the request + # @param options [Hash] Body params for the request # @return [Array] - def post(path, payload = {}) - request :post, path, payload + def post(path, options = {}) + request :post, path, options end # Make a HTTP PUT request # # @param path [String] The path, relative to {#api_endpoint} - # @param payload [Hash] Body params for the request + # @param options [Hash] Body params for the request # @return [Array] - def put(path, payload = {}) - request :put, path, payload + def put(path, options = {}) + request :put, path, options end # Make a HTTP PATCH request # # @param path [String] The path, relative to {#api_endpoint} - # @param payload [Hash] Body params for the request + # @param options [Hash] Body params for the request # @return [Array] - def patch(path, payload = {}) - request :patch, path, payload + def patch(path, options = {}) + request :patch, path, options end # Make a HTTP DELETE request # # @param path [String] The path, relative to {#api_endpoint} - # @param payload [Hash] Body params for the request + # @param options [Hash] Body params for the request # @return [Array] - def delete(path, payload = {}) - request :delete, path, payload + def delete(path, options = {}) + request :delete, path, options end # Return API endpoint @@ -202,13 +202,13 @@ def decode_body(str) # # @param method [Symbol] HTTP verb to use. # @param path [String] The path, relative to {#api_endpoint}. - # @param payload [Hash] payload to be send in the request's body + # @param data [Hash] Data to be send in the request's body # it can include query: key with requests params for GET requests # @param options [Hash] A customizable set of request options. # @return [Array] Array of resources. - def request(method, path, payload = nil, options = nil) + def request(method, path, data = nil, options = nil) instrument("request.bookingsync_api", method: method, path: path) do - response = call(method, path, payload, options) + response = call(method, path, data, options) response.respond_to?(:resources) ? response.resources : response end end @@ -244,15 +244,15 @@ def paginate(path, options = {}, &block) # # @param method [Symbol] HTTP verb to use. # @param path [String] The path, relative to {#api_endpoint}. - # @param payload [Hash] payload to be send in the request's body + # @param data [Hash] Data to be send in the request's body # it can include query: key with requests params for GET requests # @param options [Hash] A customizable set of request options. # @return [BookingSync::API::Response] A Response object. - def call(method, path, payload = nil, options = nil) + def call(method, path, data = nil, options = nil) instrument("call.bookingsync_api", method: method, path: path) do if [:get, :head].include?(method) - options = payload - payload = {} + options = data + data = {} end options ||= {} options[:headers] ||= {} @@ -272,15 +272,13 @@ def call(method, path, payload = nil, options = nil) url = expand_url(path, options[:uri]) res = @conn.send(method, url) do |req| - if payload - req.body = payload.is_a?(String) ? payload : encode_body(payload) + if data + req.body = data.is_a?(String) ? data : encode_body(data) end if params = options[:query] req.params.update params end req.headers.update options[:headers] - p options - # req.options.timeout = options[:timeout] if options[:timeout] end handle_response(res) end From d48ed5a98aa13d6cb50f0cdc35859356e66ed688 Mon Sep 17 00:00:00 2001 From: Daniel Szatmari Date: Mon, 4 Nov 2019 10:52:53 +0100 Subject: [PATCH 3/5] add option for initializer --- lib/bookingsync/api.rb | 1 - lib/bookingsync/api/client.rb | 9 ++++----- lib/bookingsync/api/configuration.rb | 20 -------------------- spec/bookingsync/api/client_spec.rb | 19 +++++++++++++++++++ 4 files changed, 23 insertions(+), 26 deletions(-) delete mode 100644 lib/bookingsync/api/configuration.rb diff --git a/lib/bookingsync/api.rb b/lib/bookingsync/api.rb index acf76ad1..ed0d68f5 100644 --- a/lib/bookingsync/api.rb +++ b/lib/bookingsync/api.rb @@ -1,7 +1,6 @@ require "faraday" require "bookingsync/api/version" require "bookingsync/api/client" -require "bookingsync/api/configuration" module BookingSync module API diff --git a/lib/bookingsync/api/client.rb b/lib/bookingsync/api/client.rb index e089db9b..dd270e85 100644 --- a/lib/bookingsync/api/client.rb +++ b/lib/bookingsync/api/client.rb @@ -122,7 +122,7 @@ def initialize(token, options = {}) @instrumenter = options[:instrumenter] || NoopInstrumenter @base_url = options[:base_url] @serializer = Serializer.new - @conn = Faraday.new(faraday_options) + @conn = Faraday.new(faraday_options(options)) @conn.headers[:accept] = MEDIA_TYPE @conn.headers[:content_type] = MEDIA_TYPE @conn.headers[:user_agent] = user_agent @@ -307,12 +307,11 @@ def middleware end end - def faraday_options + def faraday_options(options) { builder: middleware, - ssl: { verify: verify_ssl? }, - request: { timeout: BookingSync::API.configuration.timeout } - } + ssl: { verify: verify_ssl? } + }.merge(options.fetch(:faraday_options, {})) end # Return BookingSync base URL. Default is https://www.bookingsync.com diff --git a/lib/bookingsync/api/configuration.rb b/lib/bookingsync/api/configuration.rb deleted file mode 100644 index 4e8a5688..00000000 --- a/lib/bookingsync/api/configuration.rb +++ /dev/null @@ -1,20 +0,0 @@ -module BookingSync::API - class << self - attr_accessor :configuration - end - - def self.configure - self.configuration ||= Configuration.new - yield(configuration) if block_given? - end - - class Configuration - attr_accessor :timeout - - def initialize - @timeout = 20 - end - end - - configure -end diff --git a/spec/bookingsync/api/client_spec.rb b/spec/bookingsync/api/client_spec.rb index 3bc386b1..f8fdc1c3 100644 --- a/spec/bookingsync/api/client_spec.rb +++ b/spec/bookingsync/api/client_spec.rb @@ -3,6 +3,25 @@ describe BookingSync::API::Client do let(:client) { BookingSync::API::Client.new(test_access_token) } + describe "options" do + let(:client) { BookingSync::API::Client.new(test_access_token, options) } + + context "faraday_options" do + let(:options) do + { + faraday_options: + { + request: { timeout: 1 } + } + } + end + + it "sets timeout" do + expect(client.instance_variable_get("@conn").options[:timeout]).to eq 1 + end + end + end + describe "#new" do it "initializes client object with given token" do client = BookingSync::API::Client.new("xyz") From 1c5215c620eb66be6a057cc412805a33ca51c0e9 Mon Sep 17 00:00:00 2001 From: Daniel Szatmari Date: Mon, 4 Nov 2019 10:56:44 +0100 Subject: [PATCH 4/5] rubo --- lib/bookingsync/api/client.rb | 4 ++-- spec/bookingsync/api/client_spec.rb | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/bookingsync/api/client.rb b/lib/bookingsync/api/client.rb index dd270e85..078e7b36 100644 --- a/lib/bookingsync/api/client.rb +++ b/lib/bookingsync/api/client.rb @@ -308,8 +308,8 @@ def middleware end def faraday_options(options) - { - builder: middleware, + { + builder: middleware, ssl: { verify: verify_ssl? } }.merge(options.fetch(:faraday_options, {})) end diff --git a/spec/bookingsync/api/client_spec.rb b/spec/bookingsync/api/client_spec.rb index f8fdc1c3..c4a07431 100644 --- a/spec/bookingsync/api/client_spec.rb +++ b/spec/bookingsync/api/client_spec.rb @@ -7,12 +7,12 @@ let(:client) { BookingSync::API::Client.new(test_access_token, options) } context "faraday_options" do - let(:options) do - { - faraday_options: - { - request: { timeout: 1 } - } + let(:options) do + { + faraday_options: + { + request: { timeout: 1 } + } } end From e57463cdfef4e4476b7c711cd6fd9333086b12af Mon Sep 17 00:00:00 2001 From: Daniel Szatmari Date: Wed, 20 Nov 2019 10:46:24 +0100 Subject: [PATCH 5/5] add docs, bump version --- CHANGELOG.md | 3 +++ README.md | 17 +++++++++++++++++ lib/bookingsync/api/version.rb | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34f4f427..191b00c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ # master +## 0.1.11.1 - 2019-11-20 +- Added faraday_options to client. Can be used to modify faraday connection. + ## 0.1.11 - 2018-05-18 - Added `add_attachment_to_message`. diff --git a/README.md b/README.md index 6f7af984..d7f609c7 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,23 @@ rentals = api.rentals # => [BookingSync::API::Resource, BookingSync::API::Resour rentals.first.name # => "Small apartment" ``` +## Options + +### Faraday options + +can be used to add any options supported by a faraday request https://www.rubydoc.info/gems/faraday/Faraday/Request + +for example +```ruby +options = { + faraday_options: { + request: { timeout: 5 } + } +} + +api = BookingSync::API.new("OAUTH_TOKEN", options) +``` + ### Pagination All endpoints returning a collection of resources can be paginated. There are three ways to do it. diff --git a/lib/bookingsync/api/version.rb b/lib/bookingsync/api/version.rb index f8a83b35..8a22c562 100644 --- a/lib/bookingsync/api/version.rb +++ b/lib/bookingsync/api/version.rb @@ -1,5 +1,5 @@ module BookingSync module API - VERSION = "0.1.11" + VERSION = "0.1.11.1" end end