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

- (GenericRequest) initialize(connection)

Creates new instance of request for given connection



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

- (String) delete(path)

sends delete request

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

- (String) get(path)

sends get request

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

- (nil) get_file(path, &block)

sends get request to suse studio

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

- (String) post(path, data = {})

sends post request

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

- (String) put(path, data = {})

sends post request

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