Class: LocalVault::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/localvault/api_client.rb

Overview

HTTP client for the InventList API (vault sync, sharing, public keys).

All requests use Bearer token auth. Timeouts: 10s open, 30s read/write. Network and timeout errors are wrapped as ApiError so callers get a consistent exception type.

Examples:

client = ApiClient.new(token: "tok-...", base_url: "https://inventlist.com")
client.me                         # => {"user" => {"handle" => "nauman"}}
client.push_vault("prod", blob)   # => {"name" => "prod", ...}
client.pull_vault("prod")         # => raw binary blob

Defined Under Namespace

Classes: ApiError

Constant Summary collapse

BASE_PATH =
"/api/v1"

Instance Method Summary collapse

Constructor Details

#initialize(token:, base_url: nil) ⇒ ApiClient

Create a new API client.

Parameters:

  • token (String)

    Bearer token for authentication

  • base_url (String, nil) (defaults to: nil)

    API base URL (defaults to Config.api_url)



34
35
36
37
# File 'lib/localvault/api_client.rb', line 34

def initialize(token:, base_url: nil)
  @token    = token
  @base_url = base_url || Config.api_url
end

Instance Method Details

#accept_share(id) ⇒ Hash

Accept a pending vault share.

Parameters:

  • id (Integer, String)

    the share ID to accept

Returns:

  • (Hash)

    updated share data

Raises:

  • (ApiError)

    on HTTP or network failure



104
105
106
# File 'lib/localvault/api_client.rb', line 104

def accept_share(id)
  patch("/vault_shares/#{URI.encode_uri_component(id.to_s)}/accept", {})
end

#create_share(vault_name:, recipient_handle:, encrypted_payload:) ⇒ Hash

Create a new vault share for a recipient.

Parameters:

  • vault_name (String)

    name of the vault being shared

  • recipient_handle (String)

    InventList handle of the recipient

  • encrypted_payload (String)

    base64-encoded encrypted secrets from ShareCrypto

Returns:

  • (Hash)

    created share data

Raises:

  • (ApiError)

    on HTTP or network failure



91
92
93
94
95
96
97
# File 'lib/localvault/api_client.rb', line 91

def create_share(vault_name:, recipient_handle:, encrypted_payload:)
  post("/vault_shares", {
    vault_name:        vault_name,
    recipient_handle:  recipient_handle,
    encrypted_payload: encrypted_payload
  })
end

#crew_public_keys(site_slug) ⇒ Hash

Fetch public keys for all crew members of a site.

Parameters:

  • site_slug (String)

    the site’s slug

Returns:

  • (Hash)

    crew keys (e.g. => [{“handle” => “…”, “public_key” => “…”]})

Raises:

  • (ApiError)

    on HTTP or network failure



131
132
133
# File 'lib/localvault/api_client.rb', line 131

def crew_public_keys(site_slug)
  get("/sites/#{URI.encode_uri_component(site_slug)}/crew/public_keys")
end

#delete_vault(name) ⇒ Hash

Delete a vault from the cloud.

Parameters:

  • name (String)

    vault name to delete

Returns:

  • (Hash)

    confirmation response

Raises:

  • (ApiError)

    on HTTP or network failure



167
168
169
# File 'lib/localvault/api_client.rb', line 167

def delete_vault(name)
  delete("/vaults/#{URI.encode_uri_component(name)}")
end

#get_public_key(handle) ⇒ Hash

Fetch a user’s X25519 public key by handle.

Parameters:

  • handle (String)

    the user’s InventList handle

Returns:

  • (Hash)

    key data (e.g. => “base64…”)

Raises:

  • (ApiError)

    on HTTP or network failure



52
53
54
# File 'lib/localvault/api_client.rb', line 52

def get_public_key(handle)
  get("/users/#{URI.encode_uri_component(handle)}/public_key")
end

#list_vaultsHash

List all vaults stored in the cloud for the authenticated user.

Returns:

  • (Hash)

    vaults data (e.g. => [{“name” => “prod”, …]})

Raises:

  • (ApiError)

    on HTTP or network failure



139
140
141
# File 'lib/localvault/api_client.rb', line 139

def list_vaults
  get("/vaults")
end

#meHash

Fetch the authenticated user’s profile.

Returns:

  • (Hash)

    user data (e.g. => {“handle” => “nauman”})

Raises:

  • (ApiError)

    on HTTP or network failure



43
44
45
# File 'lib/localvault/api_client.rb', line 43

def me
  get("/me")
end

#pending_sharesHash

List vault shares pending acceptance by the current user.

Returns:

  • (Hash)

    shares data (e.g. => […])

Raises:

  • (ApiError)

    on HTTP or network failure



69
70
71
# File 'lib/localvault/api_client.rb', line 69

def pending_shares
  get("/vault_shares/pending")
end

#publish_public_key(public_key_b64) ⇒ Hash

Upload the current user’s X25519 public key to InventList.

Parameters:

  • public_key_b64 (String)

    base64-encoded X25519 public key

Returns:

  • (Hash)

    confirmation response

Raises:

  • (ApiError)

    on HTTP or network failure



61
62
63
# File 'lib/localvault/api_client.rb', line 61

def publish_public_key(public_key_b64)
  put("/profile/public_key", { public_key: public_key_b64 })
end

#pull_vault(name) ⇒ String

Download a vault bundle from the cloud (raw binary).

Parameters:

  • name (String)

    vault name

Returns:

  • (String)

    raw binary blob for SyncBundle.unpack

Raises:

  • (ApiError)

    on HTTP or network failure (404 if vault not found)



158
159
160
# File 'lib/localvault/api_client.rb', line 158

def pull_vault(name)
  request_raw(:get, "/vaults/#{URI.encode_uri_component(name)}")
end

#push_vault(name, blob) ⇒ Hash

Upload a vault bundle to the cloud (raw binary).

Parameters:

  • name (String)

    vault name

  • blob (String)

    packed SyncBundle binary blob

Returns:

  • (Hash)

    confirmation with vault metadata

Raises:

  • (ApiError)

    on HTTP or network failure



149
150
151
# File 'lib/localvault/api_client.rb', line 149

def push_vault(name, blob)
  request_binary(:put, "/vaults/#{URI.encode_uri_component(name)}", blob)
end

#revoke_share(id) ⇒ Hash

Revoke (delete) a vault share.

Parameters:

  • id (Integer, String)

    the share ID to revoke

Returns:

  • (Hash)

    confirmation response

Raises:

  • (ApiError)

    on HTTP or network failure



113
114
115
# File 'lib/localvault/api_client.rb', line 113

def revoke_share(id)
  delete("/vault_shares/#{URI.encode_uri_component(id.to_s)}")
end

#sent_shares(vault_name: nil) ⇒ Hash

List vault shares sent by the current user, optionally filtered by vault.

Parameters:

  • vault_name (String, nil) (defaults to: nil)

    filter by vault name (all shares if nil)

Returns:

  • (Hash)

    shares data (e.g. => […])

Raises:

  • (ApiError)

    on HTTP or network failure



78
79
80
81
82
# File 'lib/localvault/api_client.rb', line 78

def sent_shares(vault_name: nil)
  path = "/vault_shares/sent"
  path += "?vault_name=#{URI.encode_uri_component(vault_name)}" if vault_name
  get(path)
end

#team_public_keys(team_handle) ⇒ Hash

Fetch public keys for all members of a team.

Parameters:

  • team_handle (String)

    the team’s InventList handle

Returns:

  • (Hash)

    member keys (e.g. => [{“handle” => “…”, “public_key” => “…”]})

Raises:

  • (ApiError)

    on HTTP or network failure



122
123
124
# File 'lib/localvault/api_client.rb', line 122

def team_public_keys(team_handle)
  get("/teams/#{URI.encode_uri_component(team_handle)}/members/public_keys")
end