Class: KeycloakRack::HTTPClient Private

Inherits:
Object
  • Object
show all
Defined in:
lib/keycloak_rack/http_client.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Note:

Adapted from monadic HTTP client in another project

Instance Method Summary collapse

Instance Method Details

#call(request) ⇒ Dry::Monads::Success(Net::HTTPSuccess), Dry::Monads::Failure(Symbol, String, Net::HTTPResponse)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • request (Net::HTTPRequest)

Returns:

  • (Dry::Monads::Success(Net::HTTPSuccess))

    on a successful request

  • (Dry::Monads::Failure(Symbol, String, Net::HTTPResponse))

    on a failure



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/keycloak_rack/http_client.rb', line 37

def call(request)
  # :nocov:
  return Failure[:invalid_request, "Not a request: #{request.inspect}", nil] unless request.kind_of?(Net::HTTPRequest)

  uri = request.uri

  use_ssl = uri.scheme != "http"
  # :nocov:

  Net::HTTP.start(uri.host, uri.port, use_ssl: use_ssl, cert_store: x509_store) do |http|
    response = http.request request

    # :nocov:
    case response
    when Net::HTTPSuccess then Success response
    when Net::HTTPBadRequest then Failure[:bad_request, "Bad Request", response]
    when Net::HTTPUnauthorized then Failure[:unauthorized, "Unauthorized", response]
    when Net::HTTPForbidden then Failure[:forbidden, "Forbidden", response]
    when Net::HTTPNotFound then Failure[:not_found, "Not Found: #{uri}", response]
    when Net::HTTPGatewayTimeout then Failure[:gateway_timeout, "Gateway Timeout", response]
    when Net::HTTPClientError then Failure[:client_error, "Client Error: HTTP #{response.code}", response]
    when Net::HTTPServerError then Failure[:server_error, "Server Error: HTTP #{response.code}", response]
    else
      Failure[:unknown_error, "Unknown Error", response]
    end
    # :nocov:
  end
end

#get(realm_id, path) ⇒ Dry::Monads::Success(Net::HTTPSuccess), Dry::Monads::Failure(Symbol, String, Net::HTTPResponse)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • realm_id (String)
  • path (String)

Returns:

  • (Dry::Monads::Success(Net::HTTPSuccess))

    on a successful request

  • (Dry::Monads::Failure(Symbol, String, Net::HTTPResponse))

    on a failure



15
16
17
18
19
20
21
# File 'lib/keycloak_rack/http_client.rb', line 15

def get(realm_id, path)
  uri = build_uri realm_id, path

  request = Net::HTTP::Get.new(uri)

  call request
end

#get_json(realm_id, path) ⇒ Dry::Monads::Success({ Symbol => Object }), ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • realm_id (String)
  • path (String)

Returns:

  • (Dry::Monads::Success({ Symbol => Object }))

    on a successful request

  • (Dry::Monads::Failure(:invalid_response, String, Net::HTTPResponse))

    if the JSON fails to parse

  • (Dry::Monads::Failure(Symbol, String, Net::HTTPResponse))

    on a failure



28
29
30
31
32
# File 'lib/keycloak_rack/http_client.rb', line 28

def get_json(realm_id, path)
  response = yield get realm_id, path

  parse_json response
end