Class: Skynet::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/skynet/client.rb

Overview

Client for interacting with Skynet

Default portal is set to siasky.net

Constant Summary collapse

DEFAULT_SKYNET_PORTAL_URL =
'https://siasky.net'
DEFAULT_SKYNET_PORTAL_PATH =
'/skynet/skyfile'
URI_SKYNET_PREFIX =
'sia://'
DEFAULT_USER_AGENT =
'Sia-Agent'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_config = {}) ⇒ Client

Initializes the client, allows to set a custom portal or uses siasky.net as default

Parameters:

  • config (Hash)

    the configuration options to initialize the client with



34
35
36
37
38
39
40
41
# File 'lib/skynet/client.rb', line 34

def initialize(custom_config = {})
  @config = default_upload_options
  @config.merge!(custom_config)
  @api_key = config[:api_key] || nil
  Typhoeus::Config.user_agent = config[:user_agent]
  @on_upload_progress = config[:on_upload_progress]
  Typhoeus::Config.verbose = true if custom_config[:verbose]
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



20
21
22
# File 'lib/skynet/client.rb', line 20

def config
  @config
end

#user_agentObject (readonly)

Returns the value of attribute user_agent.



19
20
21
# File 'lib/skynet/client.rb', line 19

def user_agent
  @user_agent
end

Instance Method Details

#download_file(path, skylink) ⇒ String

Download a file

Parameters:

  • path (String)

    The local path where the file should be downloaded to.

  • skylink (String)

    The skylink that should be downloaded. The skylink can contain an optional path.

Returns:

  • (String)

    path Path of the downloaded file



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/skynet/client.rb', line 104

def download_file(path, skylink)
  skylink = strip_uri_prefix(skylink)

  f = File.open(path, 'wb')
  begin
    request = Typhoeus::Request.new("#{portal}/#{skylink}", headers: default_headers)
    request.on_headers do |response|
      raise 'Request failed' if response.code != 200
    end
    request.on_body do |chunk|
      f.write(chunk)
    end
    request.on_complete do |_response|
      f.close
    end
    request.run
  ensure
    f.close
  end

  path
end

#get_metadata(skylink) ⇒ Hash

Downloads the metadata of a skylink

Examples:

Response

{
  'filename' => 'foo.pdf',
  'length' => 21_977,
  'subfiles' =>
  { 'foo.pdf' =>
    { 'filename' => 'foo.pdf',
      'contenttype' => 'application/octet-stream',
      'len' => 21_977 } }
}

Parameters:

  • skylink (String)

    Skylink of the file

Returns:

  • (Hash)

Raises:



143
144
145
146
147
148
149
150
151
152
# File 'lib/skynet/client.rb', line 143

def (skylink)
  skylink = strip_uri_prefix(skylink)
  res = Typhoeus::Request.head(
    "#{portal}/#{skylink}", headers: default_headers
  )

  raise Skynet::NoMetadataError, 'No metadata returned' unless res.headers['skynet-file-metadata']

  JSON.parse res.headers['skynet-file-metadata']
end

#upload_directory(directory, opts = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/skynet/client.rb', line 82

def upload_directory(directory, opts = {})
  custom_options = filter_upload_options(config.merge(opts))

  multipart = prepare_multipart_body(directory)
  header = default_headers.merge({ 'Content-Type' => "multipart/form-data; boundary=#{multipart.boundary}" })

  res = Typhoeus::Request.new(
    "#{portal}#{portal_path}",
    method: :post,
    params: custom_options.merge(filename: File.basename(directory)),
    headers: header,
    body: multipart.to_s
  ).run

  format_response(res, custom_options)
end

#upload_file(file, custom_opts = {}) ⇒ String, Hash

Takes a file path and uploads it

Examples:

Default response:

sia://KAA54bKo-YqFRj345xGXdo9h15k.....

Full response as hash

{
  "skylink"=>"KAA54bKo-YqFRjDxRxGXdo9h15k8......",
  "merkleroot"=>"39e1b2a....",
  "bitfield"=>40,
  "sialink"=>"sia://KAA54bKo-YqFRjDxRxGXdo9h15k8...."
}

Parameters:

  • file (String)

    File or file path where the file is located

  • custom_opts (Hash) (defaults to: {})

    additional upload options

Options Hash (custom_opts):

  • :portal_file_field_name (String)

    The field name for files on the portal. Usually should not need to be changed.

  • :portal_directory_file_field_name (String)

    The field name for directories on the portal. Usually should not need to be changed.

  • :filename (String)

    Custom filename. This is the filename that will be returned when downloading the file in a browser

  • :custom_dirname (String)

    Custom dirname. If this is empty, the base name of the directory being uploaded will be used by default.

  • :timeout_seconds (String)

    Custom filename. The timeout in seconds.

  • :full_response (Boolean)

    Returns full hash with skylink, merkleroot and bitfield

Returns:

  • (String)

    Returns the sialink (sia://AABBCC) by default

  • (Hash)

    Response Hash with full response from skynet including skylink, merkleroot bitfield and sialink if :full_response option is given @option response [String] :skylink



76
77
78
79
80
# File 'lib/skynet/client.rb', line 76

def upload_file(file, custom_opts = {})
  res = upload(file, config.merge(custom_opts)).run

  format_response(res, custom_opts)
end