Files
openfoodnetwork/engines/dfc_provider/app/services/fdc_request.rb
Maikel Linke 1f00662709 Add service to access FDC API
The current implementation of the FDC is not adhering to the DFC
standard. The difference is added in this compatibility layer.

This should be temporary code. The FDC dev team should change their API
in their next development cycle.
2024-05-28 17:00:06 +10:00

34 lines
820 B
Ruby

# frozen_string_literal: true
require "private_address_check"
require "private_address_check/tcpsocket_ext"
# Request a JSON document from the FDC API with authentication.
#
# Currently, the API doesn't quite comply with the DFC standard and we need
# to authenticate a little bit differently.
#
# And then we get slightly different data as well.
class FdcRequest < DfcRequest
# Override main method to POST authorization data.
def call(url, data = {})
response = request(url, auth_data.merge(data).to_json)
if response.status >= 400 && token_stale?
refresh_access_token!
response = request(url, auth_data.merge(data).to_json)
end
response.body
end
private
def auth_data
{
userId: @user.oidc_account.uid,
accessToken: @user.oidc_account.token,
}
end
end