Skip to content

Commit

Permalink
document and test streaming uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
balexand committed May 11, 2024
1 parent b8e6ea1 commit 841e35c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/sanity.ex
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,9 @@ defmodule Sanity do
end

@doc """
Generates a request for the [asset endpoint](https://www.sanity.io/docs/http-api-assets).
Generates a request for the [asset endpoint](https://www.sanity.io/docs/http-api-assets). The
`body` argument can be a binary or iolist to upload content from memory. Or it can be an
`Enumerable`, such as a value returned by `File.stream!/2`, for streaming uploads.
## Options
Expand All @@ -398,7 +400,8 @@ defmodule Sanity do
* `creditLine` - The credit to person(s) and/or organization(s) required by the supplier of
the image to be used when published
"""
@spec upload_asset(iodata(), keyword() | map(), keyword() | map()) :: Request.t()
@spec upload_asset(iodata() | Enumerable.t(), keyword() | map(), keyword() | map()) ::
Request.t()
def upload_asset(body, opts \\ [], query_params \\ []) do
opts = NimbleOptions.validate!(opts, @asset_options_schema)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions test/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,49 @@ defmodule Sanity.MutateIntegrationTest do
end
end

describe "upload_asset" do
test "binary jpg asset", %{config: config} do
binary = File.read!("test/fixtures/danielle-stein-10OL1q7oX6c-unsplash.jpg")

assert %Sanity.Response{
body: %{
"document" => %{
"_type" => "sanity.imageAsset",
"metadata" => %{
"dimensions" => %{
"_type" => "sanity.imageDimensions",
"height" => 960,
"width" => 640
}
},
"mimeType" => "image/jpeg",
"sha1hash" => "41de39c94c974305bdda7d901cfca3102a8e6e77",
"size" => 169_633
}
}
} =
Sanity.upload_asset(binary)
|> Sanity.request!(config)
end

test "stream jpg asset", %{config: config} do

Check failure on line 220 in test/integration_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.15, 25)

test upload_asset stream jpg asset (Sanity.MutateIntegrationTest)

Check failure on line 220 in test/integration_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.14, 25)

test upload_asset stream jpg asset (Sanity.MutateIntegrationTest)

Check failure on line 220 in test/integration_test.exs

View workflow job for this annotation

GitHub Actions / Build and test (1.13, 24)

test upload_asset stream jpg asset (Sanity.MutateIntegrationTest)
stream = File.stream!("test/fixtures/danielle-stein-10OL1q7oX6c-unsplash.jpg", 2048)

assert %Sanity.Response{
body: %{
"document" => %{
"_type" => "sanity.imageAsset",
"mimeType" => "image/jpeg",
"sha1hash" => "41de39c94c974305bdda7d901cfca3102a8e6e77",
"size" => 169_633
}
}
} =
Sanity.upload_asset(stream)
|> Sanity.request!(config)
end
end

test "timeout error", %{config: config} do
config = Keyword.put(config, :http_options, receive_timeout: 0, retry: false)

Expand Down

0 comments on commit 841e35c

Please sign in to comment.