Class: Astroapi::HTTP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/astroapi/http/client.rb

Overview

HTTP client wrapper using Faraday

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



15
16
17
18
# File 'lib/astroapi/http/client.rb', line 15

def initialize(config)
  @config = config
  @connection = build_connection
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/astroapi/http/client.rb', line 13

def config
  @config
end

Instance Method Details

#delete(path) ⇒ Hash

Make a DELETE request

Parameters:

  • path (String)

    Request path

Returns:

  • (Hash)

    Response body



60
61
62
63
64
65
# File 'lib/astroapi/http/client.rb', line 60

def delete(path)
  response = @connection.delete(path)
  response.body
rescue Faraday::Error => e
  raise Astroapi::Error.from_faraday_error(e)
end

#get(path, params: {}) ⇒ Hash

Make a GET request

Parameters:

  • path (String)

    Request path

  • params (Hash) (defaults to: {})

    Query parameters

Returns:

  • (Hash)

    Response body



24
25
26
27
28
29
# File 'lib/astroapi/http/client.rb', line 24

def get(path, params: {})
  response = @connection.get(path, params)
  response.body
rescue Faraday::Error => e
  raise Astroapi::Error.from_faraday_error(e)
end

#post(path, body: {}) ⇒ Hash

Make a POST request

Parameters:

  • path (String)

    Request path

  • body (Hash) (defaults to: {})

    Request body

Returns:

  • (Hash)

    Response body



35
36
37
38
39
40
41
42
# File 'lib/astroapi/http/client.rb', line 35

def post(path, body: {})
  response = @connection.post(path) do |req|
    req.body = body.to_json
  end
  response.body
rescue Faraday::Error => e
  raise Astroapi::Error.from_faraday_error(e)
end

#put(path, body: {}) ⇒ Hash

Make a PUT request

Parameters:

  • path (String)

    Request path

  • body (Hash) (defaults to: {})

    Request body

Returns:

  • (Hash)

    Response body



48
49
50
51
52
53
54
55
# File 'lib/astroapi/http/client.rb', line 48

def put(path, body: {})
  response = @connection.put(path) do |req|
    req.body = body.to_json
  end
  response.body
rescue Faraday::Error => e
  raise Astroapi::Error.from_faraday_error(e)
end