From 1187a183514efeec38bca5a19c8f601ae5b44088 Mon Sep 17 00:00:00 2001 From: klouvas <42942196+klouvas@users.noreply.github.com> Date: Fri, 14 Feb 2020 12:41:41 +0200 Subject: [PATCH] Feat: Allow overriding headers in resource_gateway This change will allow to pass custom headers in resource gateway and will define the default headers for get|head|post|put methods. With this change we can create resources like `files` which require `content-type: multipart/form-data`. --- lib/namely/resource_gateway.rb | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/namely/resource_gateway.rb b/lib/namely/resource_gateway.rb index f578b51..875d2f0 100644 --- a/lib/namely/resource_gateway.rb +++ b/lib/namely/resource_gateway.rb @@ -55,7 +55,14 @@ def json_index_paged end attr_reader :access_token, :subdomain, :paged - + + def default_headers + { + accept: :json, + authorization: "Bearer #{access_token}" + } + end + def resource_name endpoint.split("/").last end @@ -82,31 +89,31 @@ def with_retry raise end - def get(path, params = {}) - JSON.parse(RestClient.get(url(path), accept: :json, params: params, authorization: "Bearer #{access_token}")) + def get(path, params = {}, headers: {}) + headers = default_headers.merge(headers) + JSON.parse(RestClient.get(url(path), params: params, **headers)) end - def head(path, params = {}) - RestClient.head(url(path), accept: :json, params: params, authorization: "Bearer #{access_token}") + def head(path, params = {}, headers: {}) + headers = default_headers.merge(headers) + RestClient.head(url(path), params: params, **headers) end - def post(path, params) + def post(path, params, headers: {}) + headers = default_headers.merge(content_type: :json).merge(headers) RestClient.post( url(path), params.to_json, - accept: :json, - content_type: :json, - authorization: "Bearer #{access_token}" + **headers ) end - def put(path, params) + def put(path, params, headers={}) + headers = default_headers.merge(content_type: :json).merge(headers) RestClient.put( url(path), params.to_json, - accept: :json, - content_type: :json, - authorization: "Bearer #{access_token}" + **headers ) end end