Module: HyperResource::Modules::HTTP

Included in:
HyperResource
Defined in:
lib/hyper_resource/modules/http.rb,
lib/hyper_resource/modules/http/wrap_errors.rb

Defined Under Namespace

Modules: WrapErrors

Constant Summary collapse

MAX_COORDINATOR_RETRIES =

A (high) limit to the number of retries a coordinator can ask for. This is to avoid breaking things if we have a buggy coordinator that retries things over and over again.

16
CONTENT_TYPE_HEADERS =
{
  'Content-Type' => 'application/json; charset=utf-8'
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.http_clientObject (readonly)

Returns the value of attribute http_client.



19
20
21
# File 'lib/hyper_resource/modules/http.rb', line 19

def http_client
  @http_client
end

Class Method Details

.initialize_http_client!Object



21
22
23
24
25
26
27
28
29
# File 'lib/hyper_resource/modules/http.rb', line 21

def initialize_http_client!
  @http_client = HTTPClient.new.tap do |c|
    c.cookie_manager = nil
    c.connect_timeout = 30
    c.send_timeout = 45
    c.keep_alive_timeout = 15
    c.ssl_config.set_default_paths
  end
end

Instance Method Details

#configure_clientObject

Allow the application to configure the timeout



39
40
41
# File 'lib/hyper_resource/modules/http.rb', line 39

def configure_client
  HTTP.http_client.receive_timeout = Aptible::Resource.configuration.timeout
end

#create(*args) ⇒ Object

By default, calls post with the given arguments. Override to change this behavior.



58
59
60
# File 'lib/hyper_resource/modules/http.rb', line 58

def create(*args)
  post(*args)
end

#deleteObject

DELETEs this resource’s href, and returns the response resource.



113
114
115
116
117
# File 'lib/hyper_resource/modules/http.rb', line 113

def delete
  execute_request('DELETE') do |uri, headers|
    HTTP.http_client.delete(uri, header: headers)
  end
end

#getObject

Loads and returns the resource pointed to by href. The returned resource will be blessed into its “proper” class, if self.class.namespace != nil.



46
47
48
49
50
51
52
53
54
# File 'lib/hyper_resource/modules/http.rb', line 46

def get
  execute_request('GET') do |uri, headers|
    HTTP.http_client.get(
      uri,
      follow_redirect: true,
      header: headers
    )
  end
end

#patch(attrs = nil) ⇒ Object

PATCHes this resource’s changed attributes to this resource’s href, and returns the response resource. If attributes are given, patch uses those instead.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hyper_resource/modules/http.rb', line 100

def patch(attrs = nil)
  attrs ||= attributes.changed_attributes

  execute_request('PATCH') do |uri, headers|
    HTTP.http_client.patch(
      uri,
      body: adapter.serialize(attrs),
      header: headers.merge(CONTENT_TYPE_HEADERS)
    )
  end
end

#post(attrs = nil) ⇒ Object

POSTs the given attributes to this resource’s href, and returns the response resource.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hyper_resource/modules/http.rb', line 64

def post(attrs = nil)
  attrs ||= attributes

  execute_request('POST') do |uri, headers|
    HTTP.http_client.post(
      uri,
      body: adapter.serialize(attrs),
      header: headers.merge(CONTENT_TYPE_HEADERS)
    )
  end
end

#put(attrs = nil) ⇒ Object

PUTs this resource’s attributes to this resource’s href, and returns the response resource. If attributes are given, put uses those instead.



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hyper_resource/modules/http.rb', line 85

def put(attrs = nil)
  attrs ||= attributes

  execute_request('PUT') do |uri, headers|
    HTTP.http_client.put(
      uri,
      body: adapter.serialize(attrs),
      header: headers.merge(CONTENT_TYPE_HEADERS)
    )
  end
end

#update(*args) ⇒ Object

By default, calls put with the given arguments. Override to change this behavior.



78
79
80
# File 'lib/hyper_resource/modules/http.rb', line 78

def update(*args)
  put(*args)
end