Class: Habitat::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/habitat/client.rb,
lib/habitat/client/version.rb

Overview

Habitat Client

This class is an API client to the Habitat Depot. It uses Faraday under the covers to give us a nice HTTP interface.

Constant Summary collapse

VERSION =
'0.6.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(depot = 'https://willem.habitat.sh/v1/depot') ⇒ Client

Creates the Habitat client connection object. The public interface should be used for the common interactions with the API, but more complex operations can be done using the connection attribute through Faraday.

Attributes

  • depot - URL to the Habitat Depot, defaults to the public

    service run by the Habitat organization
    
  • connection - A Faraday object representing the HTTP connection

Examples

hc = Habitat::Client.new
hc = Habitat::Client.new('https://habitat-depot.example.com')
hc.connection.get('/pkgs')


49
50
51
52
53
54
55
56
# File 'lib/habitat/client.rb', line 49

def initialize(depot = 'https://willem.habitat.sh/v1/depot')
  @depot = depot
  @connection = Faraday.new(url: @depot) do |f|
    f.request :multipart
    f.request :url_encoded
    f.adapter :net_http
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



30
31
32
# File 'lib/habitat/client.rb', line 30

def connection
  @connection
end

#depotObject (readonly)

Returns the value of attribute depot.



30
31
32
# File 'lib/habitat/client.rb', line 30

def depot
  @depot
end

Instance Method Details

#fetch_key(_key, _path = '.') ⇒ Object

Downloads the specified key. By default, it will download to the current directory as a file named by the X-Filename HTTP header.



61
62
63
# File 'lib/habitat/client.rb', line 61

def fetch_key(_key, _path = '.')
  raise 'Downloading keys is not yet implemented'
end

#fetch_package(pkg, path = '.') ⇒ Object

Downloads the specified package. It will default to the latest version if not specified, or the latest release of a version if the release is not specified.

The file will be downloaded to the current directory as a file named by the X-Filename HTTP header.

Arguments

  • pkg - A package string, like core/zlib



80
81
82
# File 'lib/habitat/client.rb', line 80

def fetch_package(pkg, path = '.')
  download(fetch_package_path(pkg), path)
end

#promote_package(pkg, view) ⇒ Object

Promotes a package to the specified view, for example core/zlib to staging or production

Examples

hc = Habitat::Client.new
hc.promote_package('core/pandas/0.0.1/20160419213120', 'staging')


116
117
118
# File 'lib/habitat/client.rb', line 116

def promote_package(pkg, view)
  promote(promote_artifact_path(pkg, view))
end

#put_key(_key, _path) ⇒ Object

Uploads the specified key from the given path location.



66
67
68
# File 'lib/habitat/client.rb', line 66

def put_key(_key, _path)
  raise 'Uploading keys is not yet implemented'
end

#put_package(file) ⇒ Object

Uploads a package from the specified filepath.

Arguments

  • file - The file to upload

Examples



105
106
107
# File 'lib/habitat/client.rb', line 105

def put_package(file)
  upload(upload_package_path(file), file)
end

#show_package(pkg) ⇒ Object

Show details about the specified package using a package identifier string. If the version or release are not specified, latest is assumed.

Examples

hc = Habitat::Client.new
hc.show_package('core/zlib')
hc.show_package('core/zlib/1.2.8')
hc.show_package('core/zlib/1.2.8/20160222155343')


94
95
96
# File 'lib/habitat/client.rb', line 94

def show_package(pkg)
  JSON.parse(@connection.get(show_package_path(pkg)).body)
end