Class: Fastly::Client
- Inherits:
-
Object
- Object
- Fastly::Client
- Defined in:
- lib/fastly/client.rb
Overview
The UserAgent to communicate with the API
Constant Summary collapse
- DEFAULT_URL =
:nodoc: all
'https://api.fastly.com'.freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#cookie ⇒ Object
Returns the value of attribute cookie.
-
#customer ⇒ Object
Returns the value of attribute customer.
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#password ⇒ Object
Returns the value of attribute password.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #authed? ⇒ Boolean
- #delete(path, params = {}) ⇒ Object
-
#fully_authed? ⇒ Boolean
Some methods require full username and password rather than just auth token.
- #get(path, params = {}) ⇒ Object
- #get_stats(path, params = {}) ⇒ Object
- #http ⇒ Object
-
#initialize(opts) ⇒ Client
constructor
A new instance of Client.
- #post(path, params = {}) ⇒ Object
- #purge(url, params = {}) ⇒ Object
- #put(path, params = {}) ⇒ Object
- #require_key! ⇒ Object
- #require_key? ⇒ Boolean
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of Client.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fastly/client.rb', line 16 def initialize(opts) @api_key = opts.fetch(:api_key, nil) @base_url = opts.fetch(:base_url, DEFAULT_URL) @customer = opts.fetch(:customer, nil) @oldpurge = opts.fetch(:use_old_purge_method, false) @password = opts.fetch(:password, nil) @user = opts.fetch(:user, nil) @debug = opts.fetch(:debug, nil) @thread_http_client = if defined?(Concurrent::ThreadLocalVar) Concurrent::ThreadLocalVar.new { build_http_client } end return self unless fully_authed? warn("DEPRECATION WARNING: Username/password authentication is deprecated and will not be available starting September 2020; please migrate to API tokens as soon as possible.") # If full auth creds (user/pass) then log in and set a cookie resp = http.post( '/login', make_params(user: user, password: password), {'Content-Type' => 'application/x-www-form-urlencoded'} ) if resp.kind_of?(Net::HTTPSuccess) = resp['Set-Cookie'] else fail , "Invalid auth credentials. Check username/password." end self end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
14 15 16 |
# File 'lib/fastly/client.rb', line 14 def api_key @api_key end |
#base_url ⇒ Object
Returns the value of attribute base_url.
14 15 16 |
# File 'lib/fastly/client.rb', line 14 def base_url @base_url end |
#cookie ⇒ Object
Returns the value of attribute cookie.
14 15 16 |
# File 'lib/fastly/client.rb', line 14 def end |
#customer ⇒ Object
Returns the value of attribute customer.
14 15 16 |
# File 'lib/fastly/client.rb', line 14 def customer @customer end |
#debug ⇒ Object
Returns the value of attribute debug.
14 15 16 |
# File 'lib/fastly/client.rb', line 14 def debug @debug end |
#password ⇒ Object
Returns the value of attribute password.
14 15 16 |
# File 'lib/fastly/client.rb', line 14 def password @password end |
#user ⇒ Object
Returns the value of attribute user.
14 15 16 |
# File 'lib/fastly/client.rb', line 14 def user @user end |
Instance Method Details
#authed? ⇒ Boolean
58 59 60 |
# File 'lib/fastly/client.rb', line 58 def authed? !api_key.nil? || fully_authed? end |
#delete(path, params = {}) ⇒ Object
95 96 97 98 99 100 |
# File 'lib/fastly/client.rb', line 95 def delete(path, params = {}) extras = params.delete(:headers) || {} include_auth = params.key?(:include_auth) ? params.delete(:include_auth) : true resp = http.delete(path, headers(extras, include_auth)) resp.kind_of?(Net::HTTPSuccess) end |
#fully_authed? ⇒ Boolean
Some methods require full username and password rather than just auth token
63 64 65 |
# File 'lib/fastly/client.rb', line 63 def fully_authed? !(user.nil? || password.nil?) end |
#get(path, params = {}) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/fastly/client.rb', line 67 def get(path, params = {}) extras = params.delete(:headers) || {} include_auth = params.key?(:include_auth) ? params.delete(:include_auth) : true path += "?#{make_params(params)}" unless params.empty? resp = http.get(path, headers(extras, include_auth)) fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess) JSON.parse(resp.body) end |
#get_stats(path, params = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/fastly/client.rb', line 76 def get_stats(path, params = {}) resp = get(path, params) # return meta data, not just the actual stats data if resp['status'] == 'success' resp else fail Error, resp['msg'] end end |
#http ⇒ Object
119 120 121 122 123 124 |
# File 'lib/fastly/client.rb', line 119 def http return @thread_http_client.value if @thread_http_client return Thread.current[:fastly_net_http] if Thread.current[:fastly_net_http] Thread.current[:fastly_net_http] = build_http_client end |
#post(path, params = {}) ⇒ Object
87 88 89 |
# File 'lib/fastly/client.rb', line 87 def post(path, params = {}) post_and_put(:post, path, params) end |
#purge(url, params = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/fastly/client.rb', line 102 def purge(url, params = {}) return post("/purge/#{url}", params) if @oldpurge extras = params.delete(:headers) || {} uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) if uri.is_a? URI::HTTPS http.use_ssl = true end resp = http.request Net::HTTP::Purge.new(uri.request_uri, headers(extras)) fail Error, resp.body unless resp.kind_of?(Net::HTTPSuccess) JSON.parse(resp.body) end |
#put(path, params = {}) ⇒ Object
91 92 93 |
# File 'lib/fastly/client.rb', line 91 def put(path, params = {}) post_and_put(:put, path, params) end |
#require_key! ⇒ Object
49 50 51 52 |
# File 'lib/fastly/client.rb', line 49 def require_key! raise Fastly::KeyAuthRequired.new("This request requires an API key") if api_key.nil? @require_key = true end |
#require_key? ⇒ Boolean
54 55 56 |
# File 'lib/fastly/client.rb', line 54 def require_key? !!@require_key end |