Class: FaaStRuby::API
- Inherits:
-
Object
- Object
- FaaStRuby::API
- Defined in:
- lib/faastruby/api.rb
Constant Summary collapse
- @@api_version =
'v2'
Instance Attribute Summary collapse
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#credentials ⇒ Object
readonly
Returns the value of attribute credentials.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #confirm_account(confirmation_token) ⇒ Object
- #create_workspace(workspace_name:, email: nil, provider: nil) ⇒ Object
- #delete_file(workspace_name:, relative_path:) ⇒ Object
- #delete_from_workspace(function_name:, workspace_name:) ⇒ Object
- #deploy(workspace_name:, package:, root_to: nil, catch_all: nil, context: nil) ⇒ Object
- #destroy_workspace(workspace_name) ⇒ Object
- #error(errors, code) ⇒ Object
- #execute_request ⇒ Object
- #get_static_metadata(workspace_name) ⇒ Object
- #get_workspace_info(workspace_name) ⇒ Object
-
#initialize ⇒ API
constructor
A new instance of API.
- #login(email:, password:) ⇒ Object
- #logout(api_key:, api_secret:, all: false) ⇒ Object
- #migrate_to_account(workspace_name:, api_key:, api_secret:) ⇒ Object
- #parse(response) ⇒ Object
- #refresh_credentials(workspace_name) ⇒ Object
- #run(function_name:, workspace_name:, payload:, method:, headers: {}, time: false, query: nil) ⇒ Object
- #send_confirmation_code(email) ⇒ Object
- #signup(email:, password:) ⇒ Object
- #update_function_context(function_name:, workspace_name:, payload:) ⇒ Object
- #update_runners(workspace_name:, runners_max:) ⇒ Object
- #upload_file(workspace_name:, relative_path:, package:) ⇒ Object
Constructor Details
#initialize ⇒ API
Returns a new instance of API.
7 8 9 10 11 12 13 14 |
# File 'lib/faastruby/api.rb', line 7 def initialize @api_url = "#{FaaStRuby.api_host}/#{@@api_version}" @credentials = {'API-KEY' => FaaStRuby.api_key, 'API-SECRET' => FaaStRuby.api_secret} @base_headers = {client_version: FaaStRuby::VERSION, content_type: 'application/json', accept: 'application/json'} @headers = @base_headers.merge(@credentials) @struct = Struct.new(:response, :body, :errors, :code) @timeout = nil # disable request timeouts end |
Instance Attribute Details
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
6 7 8 |
# File 'lib/faastruby/api.rb', line 6 def api_url @api_url end |
#credentials ⇒ Object (readonly)
Returns the value of attribute credentials.
6 7 8 |
# File 'lib/faastruby/api.rb', line 6 def credentials @credentials end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
6 7 8 |
# File 'lib/faastruby/api.rb', line 6 def headers @headers end |
Instance Method Details
#confirm_account(confirmation_token) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/faastruby/api.rb', line 78 def confirm_account(confirmation_token) url = "#{@api_url}/users/confirm" payload = { 'code' => confirmation_token } execute_request do parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, headers: @base_headers, payload: Oj.dump(payload)) end end |
#create_workspace(workspace_name:, email: nil, provider: nil) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/faastruby/api.rb', line 32 def create_workspace(workspace_name:, email: nil, provider: nil) url = "#{@api_url}/workspaces" payload = {'name' => workspace_name} payload['email'] = email if email payload['provider'] = provider if provider execute_request do parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, payload: Oj.dump(payload), headers: @headers) end end |
#delete_file(workspace_name:, relative_path:) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/faastruby/api.rb', line 137 def delete_file(workspace_name:, relative_path:) url = "#{@api_url}/workspaces/#{workspace_name}/static/sync" payload = { relative_path: relative_path } execute_request do parse RestClient::Request.execute(method: :delete, timeout: @timeout, url: url, headers: @credentials, payload: payload) end end |
#delete_from_workspace(function_name:, workspace_name:) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/faastruby/api.rb', line 174 def delete_from_workspace(function_name:, workspace_name:) url = "#{@api_url}/workspaces/#{workspace_name}/functions/#{function_name}" parse RestClient::Request.execute(method: :delete, timeout: @timeout, url: url, headers: @headers) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#deploy(workspace_name:, package:, root_to: nil, catch_all: nil, context: nil) ⇒ Object
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/faastruby/api.rb', line 163 def deploy(workspace_name:, package:, root_to: nil, catch_all: nil, context: nil) url = "#{@api_url}/workspaces/#{workspace_name}/deploy" payload = {package: File.new(package, 'rb')} payload[:root_to] = root_to if root_to payload[:catch_all] = catch_all if catch_all payload[:context] = context if context execute_request do parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, payload: payload, headers: @credentials) end end |
#destroy_workspace(workspace_name) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/faastruby/api.rb', line 42 def destroy_workspace(workspace_name) url = "#{@api_url}/workspaces/#{workspace_name}" execute_request do parse RestClient::Request.execute(method: :delete, timeout: @timeout, url: url, headers: @headers) end end |
#error(errors, code) ⇒ Object
238 239 240 |
# File 'lib/faastruby/api.rb', line 238 def error(errors, code) @struct.new(nil, nil, errors, code) end |
#execute_request ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/faastruby/api.rb', line 16 def execute_request begin yield rescue SocketError, Errno::ENETUNREACH => e FaaStRuby::CLI.error("\nCould not contact the server.\n#{e.message}") if defined?(FaaStRuby::CLI) raise e rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end end |
#get_static_metadata(workspace_name) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/faastruby/api.rb', line 121 def (workspace_name) url = "#{@api_url}/workspaces/#{workspace_name}/static/metadata" execute_request do parse RestClient::Request.execute(method: :get, timeout: @timeout, url: url, headers: @headers) end end |
#get_workspace_info(workspace_name) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/faastruby/api.rb', line 148 def get_workspace_info(workspace_name) url = "#{@api_url}/workspaces/#{workspace_name}" execute_request do parse RestClient::Request.execute(method: :get, timeout: @timeout, url: url, headers: @headers) end end |
#login(email:, password:) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/faastruby/api.rb', line 99 def login(email:, password:) url = "#{@api_url}/users/login" payload = { 'email' => email, 'password' => password } execute_request do parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, headers: @base_headers, payload: Oj.dump(payload)) end end |
#logout(api_key:, api_secret:, all: false) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/faastruby/api.rb', line 88 def logout(api_key:, api_secret:, all: false) url = "#{@api_url}/users/logout" headers = @base_headers.merge({'API-KEY' => api_key, 'API-SECRET' => api_secret}) payload = { 'all' => all } execute_request do parse RestClient::Request.execute(method: :delete, timeout: @timeout, url: url, headers: headers, payload: Oj.dump(payload)) end end |
#migrate_to_account(workspace_name:, api_key:, api_secret:) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/faastruby/api.rb', line 110 def migrate_to_account(workspace_name:, api_key:, api_secret:) url = "#{@api_url}/workspaces/#{workspace_name}/migrate" payload = { 'api_key' => api_key, 'api_secret' => api_secret } execute_request do parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, headers: @headers, payload: Oj.dump(payload)) end end |
#parse(response) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/faastruby/api.rb', line 206 def parse(response) if response.headers && latest_version = response.headers[:x_update_available] puts "########### FaaStRuby Update Available ###########" puts "# Latest version: #{latest_version} #" puts "# Please run 'faastruby update' #" puts "##################################################" end begin body = Oj.load(response.body) unless [500, 408].include?(response.code) rescue Oj::ParseError => e FaaStRuby::CLI.error("\nThe server has returned an invalid response. Status code: #{response.code}") if defined?(FaaStRuby::CLI) puts response.body raise e end case response.code when 401 then return error(["(401) Unauthorized - #{body['error']}"], 401) when 404 then return error(["(404) Not Found - #{body['error']}"], 404) when 409 then return error(["(409) Conflict - #{body['error']}"], 409) when 500 then return error(["(500) Error"], 500) when 408 then return error(["(408) Request Timeout"], 408) when 402 then return error(["(402) Limit Exceeded - #{body['error']}"], 402) when 422 errors = ["(422) Unprocessable Entity"] errors << body['error'] if body['error'] errors += body['errors'] if body['errors'] return error(errors, 422) else return @struct.new(response, body, (body['errors'] || []), response.code) end end |
#refresh_credentials(workspace_name) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/faastruby/api.rb', line 155 def refresh_credentials(workspace_name) url = "#{@api_url}/workspaces/#{workspace_name}/credentials" payload = {} execute_request do parse RestClient::Request.execute(method: :put, timeout: @timeout, url: url, payload: payload, headers: @credentials) end end |
#run(function_name:, workspace_name:, payload:, method:, headers: {}, time: false, query: nil) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/faastruby/api.rb', line 186 def run(function_name:, workspace_name:, payload:, method:, headers: {}, time: false, query: nil) url = "#{FaaStRuby.api_host}/#{workspace_name}/#{function_name}#{query}" headers['Benchmark'] = true if time execute_request do if method == 'get' RestClient::Request.execute(method: :get, timeout: @timeout, url: url, headers: headers) else RestClient::Request.execute(method: method.to_sym, timeout: @timeout, url: url, payload: payload, headers: headers) end end end |
#send_confirmation_code(email) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/faastruby/api.rb', line 68 def send_confirmation_code(email) url = "#{@api_url}/users/confirm" payload = { 'email' => email } execute_request do parse RestClient::Request.execute(method: :patch, timeout: @timeout, url: url, headers: @base_headers, payload: Oj.dump(payload)) end end |
#signup(email:, password:) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/faastruby/api.rb', line 57 def signup(email:, password:) url = "#{@api_url}/users/signup" payload = { 'email' => email, 'password' => password } execute_request do parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, headers: @base_headers, payload: Oj.dump(payload)) end end |
#update_function_context(function_name:, workspace_name:, payload:) ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/faastruby/api.rb', line 198 def update_function_context(function_name:, workspace_name:, payload:) # payload is a string url = "#{@api_url}/workspaces/#{workspace_name}/functions/#{function_name}" execute_request do parse RestClient::Request.execute(method: :patch, timeout: @timeout, url: url, payload: Oj.dump(payload), headers: @headers) end end |
#update_runners(workspace_name:, runners_max:) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/faastruby/api.rb', line 49 def update_runners(workspace_name:, runners_max:) url = "#{@api_url}/workspaces/#{workspace_name}/runners" payload = {'runners_max' => runners_max} execute_request do parse RestClient::Request.execute(method: :patch, timeout: @timeout, url: url, headers: @headers, payload: Oj.dump(payload)) end end |
#upload_file(workspace_name:, relative_path:, package:) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/faastruby/api.rb', line 128 def upload_file(workspace_name:, relative_path:, package:) url = "#{@api_url}/workspaces/#{workspace_name}/static/sync" payload = {package: File.new(package, 'rb')} payload[:relative_path] = relative_path execute_request do parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, payload: payload, headers: @credentials) end end |