ruby rest-client: make it never timeout?

后端 未结 6 1278
温柔的废话
温柔的废话 2021-02-12 21:34

I am trying to use ruby rest-client to upload a large number of images to a site that I\'m writing. My code looks like:

RestClient.post url, :timeout => 9000         


        
6条回答
  •  误落风尘
    2021-02-12 22:26

    I already use RestClient.get and RestClient.post extensively, so for me, it was easier to 'Monkey Patch' RestClient. I would recommend using RestClient::Resource.new or RestClient::Request.Execute if possible.

    However, since I'm lazy, and don't want to go swap out every occurrence of RestClient.get / RestClient.post in my code, I've decided to take a shortcut.

    $timeout = 30
    $open_timeout = 30
    
    module RestClient2
      include RestClient
    
      def self.get(url, headers={}, &block)
        Request.execute(:method => :get, :url => url, :headers => headers, 
         :timeout => $timeout, :open_timeout => $open_timeout, &block)
      end
    
      def self.post(url, payload, headers={}, &block)
        Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers,
         :timeout => $timeout, :open_timeout => $open_timeout, &block)
      end
    end
    

    And than I just just quick replaced RestClient.get/post with RestClient2.get/post.

    It would be nice, if RestClient::Request had a default timeout specified, like:

      @timeout = args[:timeout] || 30
      @open_timeout = args[:open_timeout] || 30
    

提交回复
热议问题