add support for Excon connection options in hub requests

This commit is contained in:
Neil Lalonde 2018-08-30 15:57:11 -04:00
parent 415201e1b4
commit 4b6381367e
1 changed files with 16 additions and 6 deletions

View File

@ -39,23 +39,33 @@ module DiscourseHub
end
def self.singular_action(action, rel_url, params = {})
connect_opts = connect_opts(params)
JSON.parse(Excon.send(action,
"#{hub_base_url}#{rel_url}",
{
headers: { 'Referer' => referer, 'Accept' => accepts.join(', ') },
query: params,
omit_default_port: true
}.merge(connect_opts)
).body)
end
def self.collection_action(action, rel_url, params = {})
connect_opts = connect_opts(params)
JSON.parse(Excon.send(action,
"#{hub_base_url}#{rel_url}",
{
body: JSON[params],
headers: { 'Referer' => referer, 'Accept' => accepts.join(', '), "Content-Type" => "application/json" },
omit_default_port: true
}.merge(connect_opts)
).body)
end
def self.connect_opts(params = {})
params.delete(:connect_opts)&.except(:body, :headers, :query) || {}
end
def self.hub_base_url
if Rails.env.production?
ENV['HUB_BASE_URL'] || 'https://api.discourse.org/api'