Sha256: e40eab6a4edc3ea32d40aebe5f7a345bd66884c96003a4571af96f9dbca2154a

Contents?: true

Size: 1.9 KB

Versions: 16

Compression:

Stored size: 1.9 KB

Contents

#
# Copyright:: Copyright (c) 2017 Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "chef/server_api"

module ChefDK

  # A wrapper for `Chef::ServerAPI` that supports multi-threading by creating a
  # `Chef::ServerAPI` object per-thread.
  #
  # This is intended to be used for downloading cookbooks from the Chef Infa Server,
  # where the API of the Chef Infra Server requires each file to be downloaded
  # individually.
  #
  # It also configures `Chef::ServerAPI` to enable keepalives by default. To
  # disable them, `keepalives: false` must be set in the options to the
  # constructor.
  class ChefServerAPIMulti

    KEEPALIVES_TRUE = { keepalives: true }.freeze

    attr_reader :url
    attr_reader :opts

    def initialize(url, opts)
      @url = url
      @opts = KEEPALIVES_TRUE.merge(opts)
    end

    def head(*args)
      client_for_thread.head(*args)
    end

    def get(*args)
      client_for_thread.get(*args)
    end

    def put(*args)
      client_for_thread.put(*args)
    end

    def post(*args)
      client_for_thread.post(*args)
    end

    def delete(*args)
      client_for_thread.delete(*args)
    end

    def streaming_request(*args, &block)
      client_for_thread.streaming_request(*args, &block)
    end

    def client_for_thread
      Thread.current[:chef_server_api_multi] ||= Chef::ServerAPI.new(@url, @opts)
    end

  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
chef-dk-4.13.3 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.12.0 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.11.0 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.10.0 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.9.7 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.7.73 lib/chef-dk/chef_server_api_multi.rb
chef-dk-3.13.1 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.6.35 lib/chef-dk/chef_server_api_multi.rb
chef-dk-3.12.10 lib/chef-dk/chef_server_api_multi.rb
chef-dk-3.12.0 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.5.0 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.4.27 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.3.13 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.2.0 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.1.7 lib/chef-dk/chef_server_api_multi.rb
chef-dk-4.0.60 lib/chef-dk/chef_server_api_multi.rb