mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-09 23:06:06 +00:00
Enable POSTing DFC data
This commit is contained in:
@@ -20,7 +20,7 @@ module Admin
|
||||
|
||||
catalog_url = params.require(:catalog_url)
|
||||
|
||||
json_catalog = DfcRequest.new(spree_current_user).get(catalog_url)
|
||||
json_catalog = DfcRequest.new(spree_current_user).call(catalog_url)
|
||||
graph = DfcIo.import(json_catalog)
|
||||
|
||||
# * First step: import all products for given enterprise.
|
||||
|
||||
@@ -13,12 +13,12 @@ class DfcRequest
|
||||
@user = user
|
||||
end
|
||||
|
||||
def get(url)
|
||||
response = request(url)
|
||||
def call(url, data = nil)
|
||||
response = request(url, data)
|
||||
|
||||
if response.status != 200 && token_stale?
|
||||
if response.status >= 400 && token_stale?
|
||||
refresh_access_token!
|
||||
response = request(url)
|
||||
response = request(url, data)
|
||||
end
|
||||
|
||||
response.body
|
||||
@@ -26,9 +26,13 @@ class DfcRequest
|
||||
|
||||
private
|
||||
|
||||
def request(url)
|
||||
def request(url, data = nil)
|
||||
only_public_connections do
|
||||
connection.get(url)
|
||||
if data
|
||||
connection.post(url, data)
|
||||
else
|
||||
connection.get(url)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -12,7 +12,16 @@ RSpec.describe DfcRequest do
|
||||
stub_request(:get, "http://example.net/api").
|
||||
to_return(status: 200, body: '{"@context":"/"}')
|
||||
|
||||
expect(api.get("http://example.net/api")).to eq '{"@context":"/"}'
|
||||
expect(api.call("http://example.net/api")).to eq '{"@context":"/"}'
|
||||
end
|
||||
|
||||
it "posts a DFC document" do
|
||||
json = '{"name":"new season apples"}'
|
||||
stub_request(:post, "http://example.net/api").
|
||||
with(body: json).
|
||||
to_return(status: 201) # Created
|
||||
|
||||
expect(api.call("http://example.net/api", json)).to eq ""
|
||||
end
|
||||
|
||||
it "refreshes the access token on fail", vcr: true do
|
||||
@@ -30,7 +39,7 @@ RSpec.describe DfcRequest do
|
||||
account.updated_at = 1.day.ago
|
||||
|
||||
expect {
|
||||
api.get("http://example.net/api")
|
||||
api.call("http://example.net/api")
|
||||
}.to change {
|
||||
account.token
|
||||
}.and change {
|
||||
@@ -44,7 +53,7 @@ RSpec.describe DfcRequest do
|
||||
|
||||
user.oidc_account.updated_at = 1.minute.ago
|
||||
|
||||
expect(api.get("http://example.net/api")).to eq ""
|
||||
expect(api.call("http://example.net/api")).to eq ""
|
||||
|
||||
# Trying to reach the OIDC server via network request to refresh the token
|
||||
# would raise errors because we didn't setup Webmock or VCR.
|
||||
|
||||
Reference in New Issue
Block a user