Class: StudioApi::GenericRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/studio_api/generic_request.rb

Overview

Class which use itself direct connection to studio for tasks where ActiveResource is not enough. For consistent api is all network exceptions mapped to ones used in ActiveResource.

Examples:

rq = StudioApi::GenericRequest.new @connection
rq.get "/appliances"
rq.post "/file", :file => "/etc/config"

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ GenericRequest

Creates new instance of request for given connection

Parameters:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/studio_api/generic_request.rb', line 43

def initialize(connection)
  @connection = connection
  if connection.proxy
    proxy = connection.proxy
    @http = Net::HTTP.new(connection.uri.host, connection.uri.port,
        proxy.host, proxy.port, proxy.user, proxy.password)
  else
    @http = Net::HTTP.new(connection.uri.host, connection.uri.port)
  end
  @http.read_timeout = connection.timeout
  if connection.uri.scheme == "https"
    @http.use_ssl = true
    Connection::SSL_ATTRIBUTES.each do |attr|
      @http.send :"#{attr}=", connection.ssl[attr.to_sym] if connection.ssl[attr.to_sym]
    end
  end
end

Instance Method Details

#delete(path) ⇒ String

sends delete request

Parameters:

  • path (String)

    relative path from api root

Returns:

  • (String)

    response body from studio

Raises:

  • (ActiveResource::ConnectionError)

    when problem occur during connection



82
83
84
85
# File 'lib/studio_api/generic_request.rb', line 82

def delete(path)
  #Even it is not dry I want to avoid meta programming with dynamic code evaluation so code is clear
  do_request(Net::HTTP::Delete.new(Util.join_relative_url(@connection.uri.request_uri,path)))
end

#get(path) ⇒ String

sends get request

Parameters:

  • path (String)

    relative path from api root

Returns:

  • (String)

    response body from studio

Raises:

  • (ActiveResource::ConnectionError)

    when problem occur during connection



65
66
67
# File 'lib/studio_api/generic_request.rb', line 65

def get(path)
  do_request(Net::HTTP::Get.new(Util.join_relative_url(@connection.uri.request_uri,path)))
end

#get_file(path, &block) ⇒ nil

sends get request to suse studio

Returns:

  • (nil)

    as response

Raises:

  • (ActiveResource::ConnectionError)

    when problem occur during connection



72
73
74
75
76
# File 'lib/studio_api/generic_request.rb', line 72

def get_file(path, &block)
  do_file_request(Net::HTTP::Get.new(
    Util.join_relative_url(@connection.uri.request_uri,path)),
    &block)
end

#post(path, data = {}) ⇒ String

sends post request

Parameters:

  • path (String)

    relative path from api root

  • data (Hash<#to_s,#to_s>, Hash<#to_s,#path>) (defaults to: {})

    hash containing data to attach to body

Returns:

  • (String)

    response body from studio

Raises:

  • (ActiveResource::ConnectionError)

    when problem occur during connection



92
93
94
95
96
# File 'lib/studio_api/generic_request.rb', line 92

def post(path,data={})
  request = Net::HTTP::Post.new(Util.join_relative_url(@connection.uri.request_uri,path))
  set_data(request,data) unless data.empty?
  do_request request
end

#put(path, data = {}) ⇒ String

sends post request

Parameters:

  • path (String)

    relative path from api root

  • data (Hash<#to_s,#to_s>, Hash<#to_s,#path>) (defaults to: {})

    hash containing data to attach to body

Returns:

  • (String)

    response body from studio

Raises:

  • (ActiveResource::ConnectionError)

    when problem occur during connection



103
104
105
106
107
# File 'lib/studio_api/generic_request.rb', line 103

def put(path,data={})
  request = Net::HTTP::Put.new(Util.join_relative_url(@connection.uri.request_uri,path))
  set_data(request,data) unless data.empty?
  do_request request
end