Skip to content

Commit

Permalink
Update RSTUF API client.
Browse files Browse the repository at this point in the history
- use artifacts instead of targets in payloads
  • Loading branch information
simi committed Jun 30, 2024
1 parent 1f308c8 commit 0177c2c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/rstuf/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class Rstuf::Client
Error = Class.new(StandardError)

def self.post_artifacts(targets)
response = connection.post("/api/v1/artifacts/", { targets: targets })
response = connection.post("/api/v1/artifacts/", { artifacts: targets })

return response.body.dig("data", "task_id") if response.success?
raise Error, "Error posting artifacts: #{response.body}"
end

def self.delete_artifacts(targets)
response = connection.post("/api/v1/artifacts/delete", { targets: targets }, {})
response = connection.post("/api/v1/artifacts/delete", { artifacts: targets }, {})

return response.body.dig("data", "task_id") if response.success?
raise Error, "Error deleting artifacts: #{response.body}"
Expand Down
14 changes: 7 additions & 7 deletions test/unit/rstuf/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Rstuf::ClientTest < ActiveSupport::TestCase
teardown_rstuf
end

test "post_artifacts should post targets and return task_id on success" do
test "post_artifacts should post artifacts and return task_id on success" do
task_id = "12345"
stub_request(:post, "#{Rstuf.base_url}/api/v1/artifacts/")
.with(body: { targets: %w[artifact1 artifact2] })
.with(body: { artifacts: %w[artifact1 artifact2] })
.to_return(body: { data: { task_id: task_id } }.to_json, status: 200, headers: { "Content-Type" => "application/json" })

response_task_id = Rstuf::Client.post_artifacts(%w[artifact1 artifact2])
Expand All @@ -21,20 +21,20 @@ class Rstuf::ClientTest < ActiveSupport::TestCase
end

test "post_artifacts should raise Error on failure" do
error_message = "Invalid targets"
error_message = "Invalid artifacts"
stub_request(:post, "#{Rstuf.base_url}/api/v1/artifacts/")
.with(body: { targets: %w[artifact1 artifact2] })
.with(body: { artifacts: %w[artifact1 artifact2] })
.to_return(body: { error: error_message }.to_json, status: 400, headers: { "Content-Type" => "application/json" })

assert_raises(Rstuf::Client::Error) do
Rstuf::Client.post_artifacts(%w[artifact1 artifact2])
end
end

test "delete_artifacts should post targets for deletion and return task_id on success" do
test "delete_artifacts should post artifacts for deletion and return task_id on success" do
task_id = "67890"
stub_request(:post, "#{Rstuf.base_url}/api/v1/artifacts/delete")
.with(body: { targets: %w[artifact1 artifact2] })
.with(body: { artifacts: %w[artifact1 artifact2] })
.to_return(body: { data: { task_id: task_id } }.to_json, status: 200, headers: { "Content-Type" => "application/json" })

response_task_id = Rstuf::Client.delete_artifacts(%w[artifact1 artifact2])
Expand All @@ -45,7 +45,7 @@ class Rstuf::ClientTest < ActiveSupport::TestCase
test "delete_artifacts should raise Error on failure" do
error_message = "Could not delete"
stub_request(:post, "#{Rstuf.base_url}/api/v1/artifacts/delete")
.with(body: { targets: %w[artifact1 artifact2] })
.with(body: { artifacts: %w[artifact1 artifact2] })
.to_return(body: { error: error_message }.to_json, status: 400, headers: { "Content-Type" => "application/json" })

assert_raises(Rstuf::Client::Error) do
Expand Down

0 comments on commit 0177c2c

Please sign in to comment.