Skip to content

Commit

Permalink
Merge pull request #233 from covalenthq/tesla_multipart
Browse files Browse the repository at this point in the history
Tesla multipart
  • Loading branch information
noslav authored Sep 25, 2024
2 parents ff10f05 + adb8cdf commit f066036
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ updates:
directory: "/"
schedule:
interval: "weekly"
target-branch: "develop"
reviewers:
- "noslav"
- "rogarcia"
- "rogarcia"
2 changes: 1 addition & 1 deletion .github/workflows/tag-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:

- name: Build & Publish the Docker image
run: |
docker buildx create --name builder --use --platform=linux/amd64,linux/arm64 && docker buildx build --platform=linux/amd64,linux/arm64 . -t us-docker.pkg.dev/covalent-project/network/refiner:stable -t us-docker.pkg.dev/covalent-project/network/refiner:"${{ env.TAG }}" --push
docker buildx create --name builder --use --platform=linux/amd64 && docker buildx build --platform=linux/amd64 . -t us-docker.pkg.dev/covalent-project/network/refiner:stable -t us-docker.pkg.dev/covalent-project/network/refiner:"${{ env.TAG }}" --push
- uses: "marvinpinto/action-automatic-releases@latest"
with:
Expand Down
28 changes: 15 additions & 13 deletions lib/refiner/ipfs/ipfs_interactor.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
defmodule Refiner.IPFSInteractor do
use GenServer
use Tesla

alias Multipart.Part
alias Refiner.Events

plug(Tesla.Middleware.JSON)

require Logger

@spec start_link([
Expand All @@ -27,20 +30,19 @@ defmodule Refiner.IPFSInteractor do
def handle_call({:pin, file_path}, _from, state) do
start_pin_ms = System.monotonic_time(:millisecond)
ipfs_url = Application.get_env(:refiner, :ipfs_pinner_url)
url = "#{ipfs_url}/upload"

multipart = Multipart.new() |> Multipart.add_part(Part.file_body(file_path))
body_stream = Multipart.body_stream(multipart)
content_length = Multipart.content_length(multipart)
content_type = Multipart.content_type(multipart, "multipart/form-data")
headers = [{"Content-Type", content_type}, {"Content-Length", to_string(content_length)}]

resp =
Finch.build("POST", url, headers, {:stream, body_stream})
|> Finch.request(Refiner.Finch)

case resp do
{:ok, %Finch.Response{body: body, headers: _, status: _}} ->
# Create a multipart request
multipart =
Tesla.Multipart.new()
|> Tesla.Multipart.add_file(file_path,
filename: Path.basename(file_path),
headers: [
{"Content-Type", "multipart/form-data"}
]
)

case post("#{ipfs_url}/upload", multipart) do
{:ok, %Tesla.Env{body: body, headers: _, status: _}} ->
body_map = body |> Poison.decode!()

end_pin_ms = System.monotonic_time(:millisecond)
Expand Down
6 changes: 5 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ defmodule Refiner.MixProject do

# utils
{:briefly, "~> 0.5.0"},
{:rustler, ">= 0.0.0", optional: true}
{:rustler, ">= 0.0.0", optional: true},

# Tesla for multipart
{:tesla, "~> 1.12"},
{:hackney, "~> 1.17"}

# Unused
# {:erlexec, "~> 2.0"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"telemetry_metrics": {:hex, :telemetry_metrics, "0.3.1", "799e7a3e40ebe740df265346dd1f5ff3e3c3e285f1436cd51fc8ac3b2bb7a772", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "2a1e01e752d9fd2afa70c8de4b0309535d8071a34ae329e4d68980a9bd694bfa"},
"telemetry_metrics_prometheus": {:hex, :telemetry_metrics_prometheus, "0.3.1", "ee4304aafb8fa34bc54186707b0f882030b762d4a8d393af1eccd12c2eca104b", [:mix], [{:plug_cowboy, "~> 2.1", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:telemetry_metrics_prometheus_core, "~> 0.2", [hex: :telemetry_metrics_prometheus_core, repo: "hexpm", optional: false]}], "hexpm", "e067bd280ae8d5ce82cc02515c5a6e201a7dd7939542a409d9ed63fb3d60f10c"},
"telemetry_metrics_prometheus_core": {:hex, :telemetry_metrics_prometheus_core, "0.2.2", "c75a99f68a03dd2a4061cd333e3c4728971fada798c2e096f6bca067e15b402c", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.3", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "02c1f738cd0b5c75148daecb28282ccabb806a49b717df88935a48502bb257d6"},
"tesla": {:hex, :tesla, "1.12.1", "fe2bf4250868ee72e5d8b8dfa408d13a00747c41b7237b6aa3b9a24057346681", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.13", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, ">= 1.0.0", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.2", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:msgpax, "~> 2.3", [hex: :msgpax, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "2391efc6243d37ead43afd0327b520314c7b38232091d4a440c1212626fdd6e7"},
"toml": {:hex, :toml, "0.7.0", "fbcd773caa937d0c7a02c301a1feea25612720ac3fa1ccb8bfd9d30d822911de", [:mix], [], "hexpm", "0690246a2478c1defd100b0c9b89b4ea280a22be9a7b313a8a058a2408a2fa70"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
"websockex": {:hex, :websockex, "0.4.3", "92b7905769c79c6480c02daacaca2ddd49de936d912976a4d3c923723b647bf0", [:mix], [], "hexpm", "95f2e7072b85a3a4cc385602d42115b73ce0b74a9121d0d6dbbf557645ac53e4"},
Expand Down

0 comments on commit f066036

Please sign in to comment.