mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Move DFC API request logic to service object
I'm planning to add more to it.
This commit is contained in:
@@ -20,7 +20,7 @@ module Admin
|
||||
|
||||
catalog_url = params.require(:catalog_url)
|
||||
|
||||
json_catalog = fetch_catalog(catalog_url)
|
||||
json_catalog = DfcRequest.new(spree_current_user).get(catalog_url)
|
||||
graph = DfcIo.import(json_catalog)
|
||||
|
||||
# * First step: import all products for given enterprise.
|
||||
@@ -34,29 +34,6 @@ module Admin
|
||||
|
||||
private
|
||||
|
||||
def fetch_catalog(url)
|
||||
connection = Faraday.new(
|
||||
request: { timeout: 30 },
|
||||
headers: {
|
||||
'Content-Type' => 'application/json',
|
||||
'Authorization' => "Bearer #{spree_current_user.oidc_account.token}",
|
||||
}
|
||||
)
|
||||
response = only_public_connections do
|
||||
connection.get(url)
|
||||
end
|
||||
|
||||
response.body
|
||||
end
|
||||
|
||||
def only_public_connections
|
||||
return yield if Rails.env.development?
|
||||
|
||||
PrivateAddressCheck.only_public_connections do
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
# Most of this code is the same as in the DfcProvider::SuppliedProductsController.
|
||||
def import_product(subject, enterprise)
|
||||
return unless subject.is_a? DataFoodConsortium::Connector::SuppliedProduct
|
||||
|
||||
34
engines/dfc_provider/app/services/dfc_request.rb
Normal file
34
engines/dfc_provider/app/services/dfc_request.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "private_address_check"
|
||||
require "private_address_check/tcpsocket_ext"
|
||||
|
||||
# Request a JSON document from a DFC API with authentication.
|
||||
class DfcRequest
|
||||
def initialize(user)
|
||||
@user = user
|
||||
end
|
||||
|
||||
def get(url)
|
||||
connection = Faraday.new(
|
||||
request: { timeout: 30 },
|
||||
headers: {
|
||||
'Content-Type' => 'application/json',
|
||||
'Authorization' => "Bearer #{@user.oidc_account.token}",
|
||||
}
|
||||
)
|
||||
response = only_public_connections do
|
||||
connection.get(url)
|
||||
end
|
||||
|
||||
response.body
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def only_public_connections(&)
|
||||
return yield if Rails.env.development?
|
||||
|
||||
PrivateAddressCheck.only_public_connections(&)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user