ConvertAPI / convertapi-ruby

A Ruby Gem for the ConvertAPI
https://www.convertapi.com
MIT License
11 stars 10 forks source link

Support thread-safe async conversions #22

Open danhorst opened 1 year ago

danhorst commented 1 year ago

We have a use case where some of our conversion requests will be synchronous and some will be asynchronous in the same application.

Async requests are specified by a URI path prefix instead of a request param and I don't see a way to add that prefix to the request builder in a ConvertApi::Task as it is hard-coded to start with /convert

I could modify the base_uri in ConvertApi::Configuration but that would change it for all calls to ConvertApi#convert in the current thread instead of for a single request.

Am I missing something?

danhorst commented 1 year ago

A quick and dirty solution would be extending ConvertApi::Task#run to be something like this:

    def run
      params = normalize_params(@params).merge(
        Timeout: @conversion_timeout,
        StoreFile: true,
      )

      from_format = @from_format || detect_format(params)
      read_timeout = @conversion_timeout + config.conversion_timeout_delta if @conversion_timeout
      converter = params[:converter] ? "/converter/#{params[:converter]}" : ''
      async = params[:async] ? 'async/' : ''

      response = ConvertApi.client.post(
        "#{async}convert/#{from_format}/to/#{@to_format}#{converter}",
        params,
        read_timeout: read_timeout,
      )

      Result.new(response)
    end

There's already a lot going on in that method and I hesitate to overload it even more.