Class: StrawberryAPI::HttpClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/strawberry_api/http_client.rb

Overview

Class HttpClient provides a way to query a remote HTTP server It shamelessly uses the HTTParty gem for all the dirty job

Author:

Direct Known Subclasses

Client

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ StrawberryAPI::API:User

Logs in and stores authentication session

Parameters:

  • [String] (Hash)

    a customizable set of options



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/strawberry_api/http_client.rb', line 32

def initialize(**options)
  if options.empty?
    username = StrawberryAPI.config.username
    password = StrawberryAPI.config.password
    api_key = StrawberryAPI.config.api_key
  else
    username = options[:username]
    password = options[:password]
    api_key = options[:api_key]
  end

  self.class.base_uri("http://#{StrawberryAPI.config.host}/api/v1")

  if api_key
    self.class.get("/session", headers: {Authorization: "Bearer #{api_key}"}).success?

    @headers = self.class.headers.dup
    @headers[:Authorization] = "Bearer #{api_key}"
  elsif username && password
    session = self.class.post('/session', query: {login: username, password: password})
    authentication_token = session.parse['authentication_token']

    @cookies = self.class.cookies.dup
    @cookies[:_auth_token] = authentication_token
  else
    raise 'Invalid credentials provided'
  end
end

Instance Method Details

#delete(path, mute_failure: false, **options) ⇒ HTTParty::Response

Overrides HTTPary reponse handling

Parameters:

  • path (String)
  • mute_failure (String) (defaults to: false)

    Prevents error raise if request fails

Returns:



130
131
132
# File 'lib/strawberry_api/http_client.rb', line 130

def delete(path, mute_failure: false, **options)
  request(:delete, path, mute_failure: false, **options)
end

#get(path, mute_failure: false, **options) ⇒ HTTParty::Response

Overrides HTTPary reponse handling

Parameters:

  • path (String)
  • mute_failure (String) (defaults to: false)

    Prevents error raise if request fails

Returns:



97
98
99
# File 'lib/strawberry_api/http_client.rb', line 97

def get(path, mute_failure: false, **options)
  request(:get, path, mute_failure: false, **options)
end

#handle_response(response) ⇒ HTTParty::Response

Handles HTTParty reponse

Parameters:

Returns:

Raises:

  • (String)

    Indicates the reponse error code and information



82
83
84
85
86
87
88
# File 'lib/strawberry_api/http_client.rb', line 82

def handle_response(response)
  if response.success?
    response
  else
    raise "#{response.code} #{response['error']}"
  end
end

#post(path, mute_failure: false, **options) ⇒ HTTParty::Response

Overrides HTTPary reponse handling

Parameters:

  • path (String)
  • mute_failure (String) (defaults to: false)

    Prevents error raise if request fails

Returns:



108
109
110
# File 'lib/strawberry_api/http_client.rb', line 108

def post(path, mute_failure: false, **options)
  request(:post, path, mute_failure: false, **options)
end

#put(path, mute_failure: false, **options) ⇒ HTTParty::Response

Overrides HTTPary reponse handling

Parameters:

  • path (String)
  • mute_failure (String) (defaults to: false)

    Prevents error raise if request fails

Returns:



119
120
121
# File 'lib/strawberry_api/http_client.rb', line 119

def put(path, mute_failure: false, **options)
  request(:put, path, mute_failure: false, **options)
end

#request(method, path, mute_failure: false, **options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/strawberry_api/http_client.rb', line 61

def request(method, path, mute_failure: false, **options)
  options[:headers] = @headers
  options[:cookies] = @cookies

  response = self.class.send(method, path, options)

  if mute_failure
    response
  else
    handle_response(response)
  end
end