Class: DistributedPress::V1::Client
- Inherits:
-
Object
- Object
- DistributedPress::V1::Client
- Includes:
- HTTParty
- Defined in:
- lib/distributed_press/v1/client.rb,
lib/distributed_press/v1/client/auth.rb,
lib/distributed_press/v1/client/site.rb,
lib/distributed_press/v1/client/admin.rb,
lib/distributed_press/v1/client/publisher.rb
Overview
Distributed Press APIv1 client
Defined Under Namespace
Classes: Admin, Auth, Publisher, Site
Instance Attribute Summary collapse
-
#token ⇒ DistributedPress::V1:Token
readonly
Auth token.
-
#url ⇒ String
readonly
API URL.
Instance Method Summary collapse
-
#delete(endpoint:) ⇒ Boolean
DELETE request, they return 200 when deletion is confirmed.
-
#exchange_token! ⇒ Object
Exchanges a token for a new one if expired.
-
#get(endpoint:) ⇒ Hash
GET request.
-
#headers ⇒ Hash
Default headers.
-
#initialize(url: 'https://api.distributed.press', token: nil, logger: nil) ⇒ Client
constructor
Initializes a client with a API address and token.
-
#patch(endpoint:, schema:) ⇒ Object
PATCH request.
-
#post(endpoint:, schema:) ⇒ Hash
POST request.
-
#put(endpoint:, io:, boundary:, timeout: 600) ⇒ Hash
PUT request.
Constructor Details
#initialize(url: 'https://api.distributed.press', token: nil, logger: nil) ⇒ Client
Initializes a client with a API address and token. Depending on the token capabilities we’ll be able to do some actions or not.
Automatically gets a new token if it’s about to expire
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/distributed_press/v1/client.rb', line 35 def initialize(url: 'https://api.distributed.press', token: nil, logger: nil) self.class.[:base_uri] = @url = HTTParty.normalize_base_uri(url) self.class.[:logger] = logger if logger self.class.[:log_format] = :logstash @token = case token when Token then token else Token.new(token: token.to_s) end return if @token.forever? raise TokenExpiredError if @token.expired? exchange_token! if @token.expires_in_x_seconds < 60 end |
Instance Attribute Details
#token ⇒ DistributedPress::V1:Token (readonly)
Auth token
25 26 27 |
# File 'lib/distributed_press/v1/client.rb', line 25 def token @token end |
#url ⇒ String (readonly)
API URL
21 22 23 |
# File 'lib/distributed_press/v1/client.rb', line 21 def url @url end |
Instance Method Details
#delete(endpoint:) ⇒ Boolean
DELETE request, they return 200 when deletion is confirmed.
103 104 105 106 107 |
# File 'lib/distributed_press/v1/client.rb', line 103 def delete(endpoint:) self.class.delete(endpoint, headers: headers).tap do |response| process_response_errors! endpoint, response end.ok? end |
#exchange_token! ⇒ Object
Exchanges a token for a new one if expired. Modifies the token used for initialization. If you need the token for subsequent requests you can chain them.
112 113 114 115 116 117 118 119 |
# File 'lib/distributed_press/v1/client.rb', line 112 def exchange_token! raise TokenCapabilityMissingError, 'Exchanging tokens requires refresh capability' unless token.refresh? new_token_payload = Schemas::NewTokenPayload.new.call(capabilities: token.capabilities) @token = Token.new(token: post(endpoint: '/v1/auth/exchange', schema: new_token_payload).body) nil end |
#get(endpoint:) ⇒ Hash
GET request
57 58 59 60 61 |
# File 'lib/distributed_press/v1/client.rb', line 57 def get(endpoint:) self.class.get(endpoint, headers: headers).tap do |response| process_response_errors! endpoint, response end end |
#headers ⇒ Hash
Default headers
124 125 126 127 128 129 130 131 |
# File 'lib/distributed_press/v1/client.rb', line 124 def headers @headers ||= { 'User-Agent' => "DistributedPress/#{DistributedPress::VERSION}", 'Accept' => 'application/json', 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{token}" } end |
#patch(endpoint:, schema:) ⇒ Object
PATCH request
96 |
# File 'lib/distributed_press/v1/client.rb', line 96 def patch(endpoint:, schema:); end |
#post(endpoint:, schema:) ⇒ Hash
POST request
68 69 70 71 72 |
# File 'lib/distributed_press/v1/client.rb', line 68 def post(endpoint:, schema:) self.class.post(endpoint, body: schema.to_h.to_json, headers: headers).tap do |response| process_response_errors! endpoint, response end end |
#put(endpoint:, io:, boundary:, timeout: 600) ⇒ Hash
PUT request
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/distributed_press/v1/client.rb', line 81 def put(endpoint:, io:, boundary:, timeout: 600) multipart_headers = headers.dup multipart_headers['Content-Type'] = "multipart/form-data; boundary=#{boundary}" multipart_headers['Transfer-Encoding'] = 'chunked' self.class.put(endpoint, body_stream: io, headers: multipart_headers, max_retries: 0, timeout: timeout).tap do |response| process_response_errors! endpoint, response end end |