Class: OpenBuildServiceAPI::Connection
- Inherits:
-
Object
- Object
- OpenBuildServiceAPI::Connection
- Defined in:
- lib/connection.rb
Instance Attribute Summary collapse
-
#api_endpoint ⇒ Object
readonly
Returns the value of attribute api_endpoint.
Instance Method Summary collapse
- #about ⇒ Object
-
#initialize(username, password, opts = {}) ⇒ Connection
constructor
A new instance of Connection.
- #inspect ⇒ Object
- #projects ⇒ Object
- #send_request(method, path, params = {}) ⇒ Object
Constructor Details
#initialize(username, password, opts = {}) ⇒ Connection
Returns a new instance of Connection.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/connection.rb', line 5 def initialize(username, password, opts = {}) @username = username @password = password @api_endpoint = opts[:api_endpoint] ? opts[:api_endpoint] : 'https://api.opensuse.org' @request_timeout = opts[:request_timeout] ? opts[:request_timeout].to_i : 10 @ca_file = opts[:ca_file] # send a simple request to test authentication - it raises an exception if the credentials are wrong send_request(:get, '/') end |
Instance Attribute Details
#api_endpoint ⇒ Object (readonly)
Returns the value of attribute api_endpoint.
3 4 5 |
# File 'lib/connection.rb', line 3 def api_endpoint @api_endpoint end |
Instance Method Details
#about ⇒ Object
65 66 67 68 |
# File 'lib/connection.rb', line 65 def about return @ref_about if @ref_about @ref_about = API::About.new(self) end |
#inspect ⇒ Object
16 17 18 19 20 |
# File 'lib/connection.rb', line 16 def inspect inspected_object = super inspected_object.gsub!(/\@username="([^\"]+)"/, '@username="..."') inspected_object.gsub(/\@password="([^\"]+)"/, '@password="..."') end |
#projects ⇒ Object
70 71 72 73 |
# File 'lib/connection.rb', line 70 def projects return @ref_projects if @ref_projects @ref_projects = API::Projects.new(self) end |
#send_request(method, path, params = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/connection.rb', line 22 def send_request(method, path, params = {}) request_body = params[:request_body] if params[:request_body] params.delete(:request_body) path = "/#{path}" unless path.start_with?('/') request_params = "?#{format_params(params)}" unless format_params(params).empty? uri = URI("#{@api_endpoint}#{path}#{request_params}") begin request = Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https'), open_timeout: @request_timeout, ca_file: @ca_file) if method.to_s.downcase == 'post' request_method = Net::HTTP::Post.new(uri) elsif method.to_s.downcase == 'put' request_method = Net::HTTP::Put.new(uri) elsif method.to_s.downcase == 'get' request_method = Net::HTTP::Get.new(uri) elsif method.to_s.downcase == 'delete' request_method = Net::HTTP::Delete.new(uri) end request_method['Accept'] = 'application/xml' request_method['User-Agent'] = "open-build-service-api (Ruby Gem Version: #{OpenBuildServiceAPI::VERSION})" request_method.basic_auth(@username, @password) request_method.body = request_body if request_body puts "[DEBUG] #{uri.to_s}" if ENV['OBS_API_LIBRARY_DEBUG'] response = request.request(request_method) raise InternalServerError.new(response) if response.is_a?(Net::HTTPInternalServerError) raise AuthenticationError.new(response, "Authentication failed. Please check your credentials.") if response.is_a?(Net::) code = response.code.to_i raise RequestError.new(response) if code >= 400 return response rescue Errno::ECONNREFUSED, SocketError, Net::OpenTimeout => err raise ConnectionError.new(err.to_s) end end |