Class: DistributedPress::V1::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Parameters:

  • url (String) (defaults to: 'https://api.distributed.press')
  • token (String, Distributed::Press::V1::Token) (defaults to: nil)
  • logger (Logger) (defaults to: nil)

Raises:



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.default_options[:base_uri] = @url = HTTParty.normalize_base_uri(url)
  self.class.default_options[:logger] = logger if logger
  self.class.default_options[: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

#tokenDistributedPress::V1:Token (readonly)

Auth token

Returns:

  • (DistributedPress::V1:Token)


25
26
27
# File 'lib/distributed_press/v1/client.rb', line 25

def token
  @token
end

#urlString (readonly)

API URL

Returns:

  • (String)


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.

Parameters:

  • endpoint (String)
  • schema (Dry::Schema::Result)

Returns:

  • (Boolean)


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

Parameters:

  • endpoint (String)

Returns:

  • (Hash)


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

#headersHash

Default headers

Returns:

  • (Hash)


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

Parameters:

  • endpoint (String)
  • schema (Dry::Schema::Result)


96
# File 'lib/distributed_press/v1/client.rb', line 96

def patch(endpoint:, schema:); end

#post(endpoint:, schema:) ⇒ Hash

POST request

Parameters:

  • endpoint (String)
  • schema (Dry::Schema::Result)

Returns:

  • (Hash)


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

Parameters:

  • endpoint (String)
  • io (IO)
  • boundary (String)
  • timeout (Integer) (defaults to: 600)

Returns:

  • (Hash)


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