Class: PactBroker::Client::BaseClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty, StringToSymbol, UrlHelpers
Defined in:
lib/pact_broker/client/base_client.rb

Direct Known Subclasses

Matrix, Pacticipants, Versions

Constant Summary collapse

ERROR_CODE_MAPPING =
{
  401 => "Authentication failed",
  403 => "Authorization failed (insufficient permissions)",
  409 => "Potential duplicate pacticipants"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StringToSymbol

#string_keys_to_symbols

Methods included from UrlHelpers

#encode_param, #encode_query_param

Constructor Details

#initialize(options) ⇒ BaseClient

Returns a new instance of BaseClient.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pact_broker/client/base_client.rb', line 47

def initialize options
  @base_url = options[:base_url]
  @client_options = options[:client_options] || {}
  @verbose = @client_options[:verbose]
  self.class.base_uri base_url
  self.class.debug_output($stderr) if verbose?
  self.class.basic_auth(client_options[:basic_auth][:username], client_options[:basic_auth][:password]) if client_options[:basic_auth]
  self.class.headers('Authorization' => "Bearer #{client_options[:token]}") if client_options[:token]
  self.class.ssl_ca_file(ENV['SSL_CERT_FILE']) if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != ''
  self.class.ssl_ca_path(ENV['SSL_CERT_DIR']) if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != ''
  @default_options = {}
  @default_options[:verify] = false if (ENV['PACT_DISABLE_SSL_VERIFICATION'] == 'true' || ENV['PACT_BROKER_DISABLE_SSL_VERIFICATION'] == 'true')
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



45
46
47
# File 'lib/pact_broker/client/base_client.rb', line 45

def base_url
  @base_url
end

#client_optionsObject (readonly)

Returns the value of attribute client_options.



45
46
47
# File 'lib/pact_broker/client/base_client.rb', line 45

def client_options
  @client_options
end

Instance Method Details

#default_get_headersObject



65
66
67
# File 'lib/pact_broker/client/base_client.rb', line 65

def default_get_headers
  default_request_headers
end

#default_patch_headersObject



69
70
71
# File 'lib/pact_broker/client/base_client.rb', line 69

def default_patch_headers
  default_request_headers.merge('Content-Type' => 'application/json')
end

#default_put_headersObject



73
74
75
# File 'lib/pact_broker/client/base_client.rb', line 73

def default_put_headers
  default_request_headers.merge('Content-Type' => 'application/json')
end

#default_request_headersObject



61
62
63
# File 'lib/pact_broker/client/base_client.rb', line 61

def default_request_headers
  {'Accept' => 'application/hal+json, application/json'}
end

#get(url, options = {}, &block) ⇒ Object



114
115
116
# File 'lib/pact_broker/client/base_client.rb', line 114

def get url, options = {}, &block
  self.class.get(url, @default_options.merge(options), &block)
end

#handle_response(response) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pact_broker/client/base_client.rb', line 77

def handle_response response
  if response.success?
    yield response
  elsif response.code == 404
    nil
  elsif ERROR_CODE_MAPPING.key?(response.code)
    message = ERROR_CODE_MAPPING.fetch(response.code)
    if response.body && response.body.size > 0
      message = message + ": #{response.body}"
    end
    raise Error.new(message)
  else
    error_message = nil
    begin
      errors = JSON.parse(response.body)['errors']
      error_message = if errors.is_a?(Array)
        errors.join("\n")
      elsif errors.is_a?(Hash)
        errors.collect{ |key, value| "#{key}: #{value}" }.join("\n")
      else
        response.body
      end
    rescue
      raise Error.new("status=#{response.code} #{response.body}")
    end
    raise Error.new(error_message)
  end
end

#patch(url, options) ⇒ Object



106
107
108
# File 'lib/pact_broker/client/base_client.rb', line 106

def patch url, options
  self.class.patch(url, @default_options.merge(options.merge(body: options[:body].to_json)))
end

#put(url, options = {}, &block) ⇒ Object



110
111
112
# File 'lib/pact_broker/client/base_client.rb', line 110

def put url, options = {}, &block
  self.class.put(url, @default_options.merge(options), &block)
end

#url_for_relation(relation_name, params) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pact_broker/client/base_client.rb', line 118

def url_for_relation relation_name, params
  handle_response(get("/", headers: default_get_headers)) do | response |
    relation = (JSON.parse(response.body)['_links'] || {})[relation_name]
    if relation
      url = relation['href']
      params.each do | (key, value) |
        url = url.gsub("{#{key}}", encode_param(value))
      end
      url
    else
      raise PactBroker::Client::RelationNotFound.new("Could not find relation #{relation_name} in index resource. Try upgrading your Pact Broker as the feature you require may not exist in your version. If you are using PactFlow, you may not have the permissions required for this action.")
    end
  end
end

#verbose?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/pact_broker/client/base_client.rb', line 133

def verbose?
  @verbose
end