Class: School21::BaseApi

Inherits:
Object
  • Object
show all
Includes:
CoreLibrary
Defined in:
lib/school21/api/base_api.rb

Constant Summary collapse

SINGLE_AUTH_PARTICIPANT =
:global

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_configuration) ⇒ BaseApi

Returns a new instance of BaseApi.



26
27
28
29
# File 'lib/school21/api/base_api.rb', line 26

def initialize(global_configuration)
  @global_configuration = global_configuration
  @api_call = ApiCall.new(@global_configuration)
end

Class Method Details

.base_uri(server) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/school21/api/base_api.rb', line 9

def self.base_uri(server)
  case server
  when :api_v1
    'https://edu-api.21-school.ru/services/21-school/api/v1'
  when :auth
    'https://auth.sberclass.ru/auth/realms/EduPowerKeycloak/protocol/openid-connect'
  end
end

.response_convertor(api_response) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/school21/api/base_api.rb', line 18

def self.response_convertor(api_response)
  api_response.data.deep_transform_keys! do |key|
    key.underscore.to_sym
  end

  api_response
end

Instance Method Details

#authenticated_requestObject



53
54
55
56
57
# File 'lib/school21/api/base_api.rb', line 53

def authenticated_request(...)
  auth_participant = CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT)

  new_request_builder(...).auth(auth_participant)
end

#execute_request(new_request) ⇒ Object



59
60
61
62
63
64
# File 'lib/school21/api/base_api.rb', line 59

def execute_request(new_request)
  new_api_call_builder
    .request(new_request)
    .response(new_response_handler)
    .execute
end

#new_api_call_builderObject



31
# File 'lib/school21/api/base_api.rb', line 31

def new_api_call_builder = @api_call.new_builder

#new_parameter(value, key: nil) ⇒ Object



47
48
49
50
51
# File 'lib/school21/api/base_api.rb', line 47

def new_parameter(value, key: nil)
  Parameter.new
           .key(key)
           .value(value)
end

#new_request_builder(http_method, path, server) ⇒ Object



40
41
42
43
44
45
# File 'lib/school21/api/base_api.rb', line 40

def new_request_builder(http_method, path, server)
  RequestBuilder.new
                .http_method(http_method)
                .path(path)
                .server(server)
end

#new_response_handlerObject



33
34
35
36
37
38
# File 'lib/school21/api/base_api.rb', line 33

def new_response_handler
  ResponseHandler.new
                 .deserializer(ApiHelper.method(:json_deserialize))
                 .is_api_response(true)
                 .convertor(self.class.method(:response_convertor))
end