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
- #delele_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
- #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
def list_workspace_functions(workspace_name) url = “#@api_url/workspaces/#workspace_name/functions” parse RestClient.get(url, @headers){|response, request, result| response } end.
- #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
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/faastruby/api.rb', line 87 def confirm_account(confirmation_token) url = "#{@api_url}/users/confirm" payload = { 'code' => confirmation_token } parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, headers: @base_headers, payload: Oj.dump(payload)) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#create_workspace(workspace_name:, email: nil, provider: nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/faastruby/api.rb', line 16 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 parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, payload: Oj.dump(payload), headers: @headers) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#delele_file(workspace_name:, relative_path:) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/faastruby/api.rb', line 176 def delele_file(workspace_name:, relative_path:) url = "#{@api_url}/workspaces/#{workspace_name}/static/sync" payload = { relative_path: relative_path } parse RestClient::Request.execute(method: :delete, timeout: @timeout, url: url, headers: @credentials, payload: payload) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#delete_from_workspace(function_name:, workspace_name:) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/faastruby/api.rb', line 233 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
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/faastruby/api.rb', line 217 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 parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, payload: payload, headers: @credentials) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#destroy_workspace(workspace_name) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/faastruby/api.rb', line 31 def destroy_workspace(workspace_name) url = "#{@api_url}/workspaces/#{workspace_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 |
#error(errors, code) ⇒ Object
305 306 307 |
# File 'lib/faastruby/api.rb', line 305 def error(errors, code) @struct.new(nil, nil, errors, code) end |
#get_static_metadata(workspace_name) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/faastruby/api.rb', line 150 def (workspace_name) url = "#{@api_url}/workspaces/#{workspace_name}/static/metadata" parse RestClient::Request.execute(method: :get, 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 |
#get_workspace_info(workspace_name) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/faastruby/api.rb', line 192 def get_workspace_info(workspace_name) url = "#{@api_url}/workspaces/#{workspace_name}" parse RestClient::Request.execute(method: :get, 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 |
#login(email:, password:) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/faastruby/api.rb', line 118 def login(email:, password:) url = "#{@api_url}/users/login" payload = { 'email' => email, 'password' => password } parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, headers: @base_headers, payload: Oj.dump(payload)) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#logout(api_key:, api_secret:, all: false) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/faastruby/api.rb', line 102 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 } parse RestClient::Request.execute(method: :delete, timeout: @timeout, url: url, headers: headers, payload: Oj.dump(payload)) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#migrate_to_account(workspace_name:, api_key:, api_secret:) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/faastruby/api.rb', line 134 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 } parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, headers: @headers, payload: Oj.dump(payload)) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#parse(response) ⇒ Object
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/faastruby/api.rb', line 281 def parse(response) begin body = Oj.load(response.body) unless [500, 408].include?(response.code) rescue Oj::ParseError => e 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
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/faastruby/api.rb', line 204 def refresh_credentials(workspace_name) url = "#{@api_url}/workspaces/#{workspace_name}/credentials" payload = {} parse RestClient::Request.execute(method: :put, timeout: @timeout, url: url, payload: payload, headers: @credentials) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#run(function_name:, workspace_name:, payload:, method:, headers: {}, time: false, query: nil) ⇒ Object
def list_workspace_functions(workspace_name)
url = "#{@api_url}/workspaces/#{workspace_name}/functions"
parse RestClient.get(url, @headers){|response, request, result| response }
end
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/faastruby/api.rb', line 250 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 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 rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else return err.response end end |
#send_confirmation_code(email) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/faastruby/api.rb', line 72 def send_confirmation_code(email) url = "#{@api_url}/users/confirm" payload = { 'email' => email } parse RestClient::Request.execute(method: :patch, timeout: @timeout, url: url, headers: @base_headers, payload: Oj.dump(payload)) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#signup(email:, password:) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/faastruby/api.rb', line 56 def signup(email:, password:) url = "#{@api_url}/users/signup" payload = { 'email' => email, 'password' => password } parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, headers: @base_headers, payload: Oj.dump(payload)) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#update_function_context(function_name:, workspace_name:, payload:) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/faastruby/api.rb', line 267 def update_function_context(function_name:, workspace_name:, payload:) # payload is a string url = "#{@api_url}/workspaces/#{workspace_name}/functions/#{function_name}" parse RestClient::Request.execute(method: :patch, timeout: @timeout, url: url, payload: Oj.dump(payload), headers: @headers) # parse RestClient.patch(url, Oj.dump(payload), @headers){|response, request, result| response } rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#update_runners(workspace_name:, runners_max:) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/faastruby/api.rb', line 43 def update_runners(workspace_name:, runners_max:) url = "#{@api_url}/workspaces/#{workspace_name}/runners" payload = {'runners_max' => runners_max} parse RestClient::Request.execute(method: :patch, timeout: @timeout, url: url, headers: @headers, payload: Oj.dump(payload)) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |
#upload_file(workspace_name:, relative_path:, package:) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/faastruby/api.rb', line 162 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 parse RestClient::Request.execute(method: :post, timeout: @timeout, url: url, payload: payload, headers: @credentials) rescue RestClient::ExceptionWithResponse => err case err.http_code when 301, 302, 307 err.response.follow_redirection else parse err.response end end |