Class: NVX::SDS::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/nvx/sds/transport.rb

Overview

Overview

This is an internal class that handles the requests and responses from the Nirvanix REST web services. Requests can be made via HTTP GET or POST.

Class Method Summary collapse

Class Method Details

.DownloadFile(path, local_path, account_login) ⇒ Object

Obsolete - Use HttpDownload.DownloadFile instead.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/nvx/sds/transport.rb', line 78

def Transport.DownloadFile(path, local_path, )
    # Get the download node this will get the proper node for a path.
    doc = Transport.execute_command_post(APICommand.GetDownloadNodes, [APIParam.new("filePath", path)], )
    node = doc.root.elements["//Response/DownloadNode"].get_text.value
    
    # Transfer the file to the local path.
    Net::HTTP.start(node) do |http|
        resp = http.get(path)
        open(local_path, "wb") do |file|
            file.write(resp.body)
        end
    end
end

.execute_command_post(apicommand, apiparams, account_login) ⇒ Object

A method to execute a nirvanix command with the parameters passed via a POST HTTP call.



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
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nvx/sds/transport.rb', line 26

def Transport.execute_command_post(apicommand, apiparams, )
    rest_api_url = "services.nirvanix.com"
    webservice_root = "/ws/"
    
    url = webservice_root + apicommand.command_path
    
    # add the session token in if valid.
    apiparams = Array.new if apiparams.nil?
    apiparams << APIParam.new("sessionToken", .session_token) if !.nil?

    params = ""
    first = true
    apiparams.each do |apiParam|
        params = first == true ? apiParam.to_s : params + '&' + apiParam.to_s
        first = false
    end
    
    port = apicommand.is_secure? ? 443 : 80
    
    # Create the first http(s) object.
    h = Net::HTTP.new(rest_api_url, port)
    
    # if you are just testing and do not have a SSL certificate setup properly you can
    # disable warnings with the below line.  Just uncomment to ignore SSL session warnings.
    h.verify_mode = OpenSSL::SSL::VERIFY_NONE if apicommand.is_secure?
    
    # put the SSL in the correct mode based on the apicommand
    h.use_ssl = apicommand.is_secure?
    
    # do the command and return the response.
    response = h.post(url, params)

    #print "OUTPUT URL: " + rest_api_url + url + "\r\n\r\n"
    #print "RESPONSE XML: " +  response.body + "\r\n\r\n"

    doc = REXML::Document.new(response.body)
    response_code = (text = doc.root.elements["//Response/ResponseCode"].get_text and text.value)

    if response_code.to_i == SessionNotFound
        # login and refresh session token
        .Login
        # execute the command again so there is no interruption to the user.
        return Transport.execute_command_post(apicommand, apiparams, )
    end
    if response_code.to_i != 0
        error_message = (text = doc.root.elements["//Response/ErrorMessage"].get_text and text.value)
        raise error_message
    end
    return doc
end