Module: MixinBot::API::App

Included in:
MixinBot::API
Defined in:
lib/mixin_bot/api/app.rb

Instance Method Summary collapse

Instance Method Details

#add_favorite_app(app_id, access_token: nil) ⇒ Object Also known as: favorite_app



104
105
106
107
108
# File 'lib/mixin_bot/api/app.rb', line 104

def add_favorite_app(app_id, access_token: nil)
  path = format('/apps/%<id>s/favorite', id: app_id)

  client.post path, access_token:
end

#app(app_id, access_token: nil) ⇒ Object Also known as: fetch_app



6
7
8
9
# File 'lib/mixin_bot/api/app.rb', line 6

def app(app_id, access_token: nil)
  path = format('/apps/%<id>s', id: app_id)
  client.get path, access_token:
end

#app_billing(app_id, access_token: nil) ⇒ Object



22
23
24
25
# File 'lib/mixin_bot/api/app.rb', line 22

def app_billing(app_id, access_token: nil)
  path = format('/safe/apps/%<id>s/billing', id: app_id)
  client.get path, access_token:
end

#app_properties(access_token: nil) ⇒ Object Also known as: app_property



17
18
19
# File 'lib/mixin_bot/api/app.rb', line 17

def app_properties(access_token: nil)
  client.get '/apps/property', access_token:
end

#apps(access_token: nil) ⇒ Object Also known as: fetch_apps



12
13
14
# File 'lib/mixin_bot/api/app.rb', line 12

def apps(access_token: nil)
  client.get '/apps', access_token:
end

#create_app(**kwargs) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mixin_bot/api/app.rb', line 59

def create_app(**kwargs)
  payload = {
    redirect_uri: kwargs[:redirect_uri],
    home_uri: kwargs[:home_uri],
    name: kwargs[:name],
    description: kwargs[:description],
    icon_base64: kwargs[:icon_base64],
    category: kwargs[:category],
    capabilities: kwargs[:capabilities],
    resource_patterns: kwargs[:resource_patterns]
  }.compact
  client.post '/apps', **payload, access_token: kwargs[:access_token]
end

#ensure_app_billing_credit!(force: false, access_token: nil, increment: 0) ⇒ Object

Verifies the app has billing headroom before a billed operation (e.g. creating a network user). Skipped when force is true.

Parameters:

  • force (Boolean) (defaults to: false)

    skip the preflight and call the API anyway

  • increment (Numeric, String) (defaults to: 0)

    estimated cost added to total billing cost for headroom (default 0)

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mixin_bot/api/app.rb', line 37

def ensure_app_billing_credit!(force: false, access_token: nil, increment: 0)
  return if force

  app_id = config.app_id
  billing = app_billing(app_id, access_token:)['data']

  credit = billing_decimal billing['credit']
  cost_users = billing_decimal billing.dig('cost', 'users')
  cost_resources = billing_decimal billing.dig('cost', 'resources')
  cost = cost_users + cost_resources
  increment = billing_decimal increment

  return if credit > cost + increment

  raise InsufficientAppBillingError.new(
    app_id:,
    credit: credit.to_s('F'),
    cost: cost.to_s('F'),
    increment: increment.to_s('F')
  )
end

#favorite_apps(user_id = nil, access_token: nil) ⇒ Object



118
119
120
121
122
# File 'lib/mixin_bot/api/app.rb', line 118

def favorite_apps(user_id = nil, access_token: nil)
  path = format('/users/%<id>s/apps/favorite', id: user_id || config.app_id)

  client.get path, access_token:
end

#register_app_safe(app_id, spend_public_key:, signature_base64:, access_token: nil) ⇒ Object



99
100
101
102
# File 'lib/mixin_bot/api/app.rb', line 99

def register_app_safe(app_id, spend_public_key:, signature_base64:, access_token: nil)
  path = format('/safe/apps/%<id>s/register', id: app_id)
  client.post path, spend_public_key:, signature_base64:, access_token:
end

#remove_favorite_app(app_id, access_token: nil) ⇒ Object Also known as: unfavorite_app



111
112
113
114
115
# File 'lib/mixin_bot/api/app.rb', line 111

def remove_favorite_app(app_id, access_token: nil)
  path = format('/apps/%<id>s/unfavorite', id: app_id)

  client.post path, access_token:
end

#rotate_app_secret(app_id, access_token: nil) ⇒ Object Also known as: update_app_secret



88
89
90
91
# File 'lib/mixin_bot/api/app.rb', line 88

def rotate_app_secret(app_id, access_token: nil)
  path = format('/apps/%<id>s/secret', id: app_id)
  client.post path, access_token:
end

#transfer_app_ownership(receiver_user_id:, pin:, access_token: nil) ⇒ Object Also known as: migrate



124
125
126
127
128
# File 'lib/mixin_bot/api/app.rb', line 124

def transfer_app_ownership(receiver_user_id:, pin:, access_token: nil)
  path = format('/apps/%<app_id>s/transfer', app_id: config.app_id)
  tip = tip_or_legacy_pin_payload(pin, 'TIP:APP:OWNERSHIP:TRANSFER:', receiver_user_id)
  client.post path, user_id: receiver_user_id, pin_base64: tip[:pin_base64] || tip[:pin], access_token:
end

#update_app(app_id, **kwargs) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mixin_bot/api/app.rb', line 73

def update_app(app_id, **kwargs)
  path = format('/apps/%<id>s', id: app_id)
  payload = {
    redirect_uri: kwargs[:redirect_uri],
    home_uri: kwargs[:home_uri],
    name: kwargs[:name],
    description: kwargs[:description],
    icon_base64: kwargs[:icon_base64],
    category: kwargs[:category],
    capabilities: kwargs[:capabilities],
    resource_patterns: kwargs[:resource_patterns]
  }.compact
  client.post path, **payload, access_token: kwargs[:access_token]
end

#update_app_safe_session(app_id, session_public_key:, access_token: nil) ⇒ Object



94
95
96
97
# File 'lib/mixin_bot/api/app.rb', line 94

def update_app_safe_session(app_id, session_public_key:, access_token: nil)
  path = format('/safe/apps/%<id>s/session', id: app_id)
  client.post path, session_public_key:, access_token:
end