Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allow overriding headers in resource_gateway #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions lib/namely/resource_gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down