Class: D2L::Valence::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/d2l/valence/request.rb

Overview

Request

Class for authenticated calls to the D2L Valence API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_context:, http_method:, route:, route_params: {}, query_params: {}) ⇒ Request

API routes

See D2L::Valence::UserContext.api_call for details on creating routes and route_params

Parameters:

  • user_context (D2L::Valance::UserContext)

    the user context created after authentication

  • http_method (String)

    the HTTP Method for the call (i.e. PUT, GET, POST, DELETE)

  • route (String)

    the API method route (e.g. /d2l/api/lp/:version/users/whoami)

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

    the parameters for the API method route (option)

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

    the query parameters for the method call



19
20
21
22
23
24
25
26
27
28
# File 'lib/d2l/valence/request.rb', line 19

def initialize(user_context:, http_method:, route:, route_params: {}, query_params: {})
  @user_context = user_context
  @app_context = user_context.app_context
  @http_method = http_method.upcase
  @route = route
  @route_params = route_params
  @query_params = query_params

  raise "HTTP Method #{@http_method} is unsupported" unless %w(GET PUT POST DELETE).include? @http_method
end

Instance Attribute Details

#http_methodObject (readonly)

Returns the value of attribute http_method.



6
7
8
# File 'lib/d2l/valence/request.rb', line 6

def http_method
  @http_method
end

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/d2l/valence/request.rb', line 6

def response
  @response
end

#user_contextObject (readonly)

Returns the value of attribute user_context.



6
7
8
# File 'lib/d2l/valence/request.rb', line 6

def user_context
  @user_context
end

Instance Method Details

#authenticated_uriURI::Generic

Generates an authenticated URI for a the Valence API method

Returns:

  • (URI::Generic)

    URI for the authenticated method call



33
34
35
36
37
38
# File 'lib/d2l/valence/request.rb', line 33

def authenticated_uri
  @app_context.brightspace_host.to_uri(
    path: path,
    query: query
  )
end

#executeD2L::Valence::Response

Sends the authenticated call on the Valence API

Returns:



43
44
45
46
47
48
49
# File 'lib/d2l/valence/request.rb', line 43

def execute
  raise "HTTP Method #{@http_method} is not implemented" if params.nil?

  @response = execute_call
  @user_context.server_skew = @response.server_skew
  @response
end

#pathString

Generates the final path for the authenticated call

Returns:

  • (String)

    path for the authenticated call



54
55
56
57
58
59
60
# File 'lib/d2l/valence/request.rb', line 54

def path
  return @path unless @path.nil?

  substitute_keys_with(@route_params)
  substitute_keys_with(known_params)
  @path = @route
end