Class: Morpheus::FileCopyRequestInterface

Inherits:
APIClient
  • Object
show all
Defined in:
lib/morpheus/api/file_copy_request_interface.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_token, refresh_token, expires_at = nil, base_url = nil) ⇒ FileCopyRequestInterface

Returns a new instance of FileCopyRequestInterface.



5
6
7
8
9
10
# File 'lib/morpheus/api/file_copy_request_interface.rb', line 5

def initialize(access_token, refresh_token, expires_at = nil, base_url=nil) 
  @access_token = access_token
  @refresh_token = refresh_token
  @base_url = base_url
  @expires_at = expires_at
end

Instance Method Details

#create(local_file, params = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/morpheus/api/file_copy_request_interface.rb', line 20

def create(local_file, params={})
  # puts "upload_file #{local_file} to destination #{destination}"
  if !local_file.kind_of?(File)
    local_file = File.new(local_file, 'rb')
  end
  url = "#{@base_url}/api/file-copy-request/execute"
  headers = { :params => params, :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/octet-stream'}
  headers[:params][:filename] = File.basename(local_file)
  payload = local_file
  headers['Content-Length'] = local_file.size # File.size(local_file)
  execute(method: :post, url: url, headers: headers, payload: payload)
end

#download_file_chunked(id, outfile, params = {}) ⇒ Object



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
# File 'lib/morpheus/api/file_copy_request_interface.rb', line 46

def download_file_chunked(id, outfile, params={})
  raise "#{self.class}.download_file() passed a blank id!" if id.to_s == ''
  url = "#{@base_url}/api/file-copy-request/download/#{URI.escape(id)}"
  headers = { params: params, authorization: "Bearer #{@access_token}" }
  opts = {method: :get, url: url, headers: headers}
  # execute(opts, false)
  if Dir.exists?(outfile)
    raise "outfile is invalid. It is the name of an existing directory: #{outfile}"
  end
  # if @verify_ssl == false
  #   opts[:verify_ssl] = OpenSSL::SSL::VERIFY_NONE
  # end
  if @dry_run
    return opts
  end
  http_response = nil
  File.open(outfile, 'w') {|f|
    block = proc { |response|
      response.read_body do |chunk|
        # writing to #{outfile} ..."
        f.write chunk
      end
    }
    opts[:block_response] = block
    http_response = RestClient::Request.new(opts).execute
    # RestClient::Request.execute(opts)
  }
  return http_response
end

#execute_against_lease(id, local_file, params) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/morpheus/api/file_copy_request_interface.rb', line 33

def execute_against_lease(id, local_file, params)
  # puts "upload_file #{local_file} to destination #{destination}"
  if !local_file.kind_of?(File)
    local_file = File.new(local_file, 'rb')
  end
  url = "#{@base_url}/api/file-copy-request/lease/#{URI.escape(id)}"
  headers = { :params => params, :authorization => "Bearer #{@access_token}", 'Content-Type' => 'application/octet-stream'}
  headers[:params][:filename] = File.basename(local_file)
  payload = local_file
  headers['Content-Length'] = local_file.size # File.size(local_file)
  execute(method: :post, url: url, headers: headers, payload: payload)
end

#get(id, params = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/morpheus/api/file_copy_request_interface.rb', line 12

def get(id, params={})
  raise "#{self.class}.get() passed a blank id!" if id.to_s == ''
  url = "#{@base_url}/api/file-copy-request/#{id}"
  headers = { :params => params, authorization: "Bearer #{@access_token}"}
  opts = {method: :get, url: url, headers: headers}
  execute(opts)
end