Class: School21::BaseApi

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

Constant Summary collapse

PLATFORM_AUTH_PARTICIPANT =
:platform

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseApi

Returns a new instance of BaseApi.



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

def initialize
  @api_call = ApiCall.new(School21.config)
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://platform.21-school.ru/services/21-school/api/v1'
  when :auth
    'https://auth.21-school.ru/auth/realms/EduPowerKeycloak/protocol/openid-connect'
  end
end

.response_convertor(api_response) ⇒ Object



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

def self.response_convertor(api_response)
  return api_response unless api_response.data.present? && api_response.data.is_a?(Hash)

  api_response.data.deep_transform_keys! do |key|
    key.underscore.to_sym
  end

  api_response
end

Instance Method Details

#execute_request(new_request) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/school21/api/base_api.rb', line 62

def execute_request(new_request)
  Authenticator.call

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

#new_api_call_builderObject



32
33
34
# File 'lib/school21/api/base_api.rb', line 32

def new_api_call_builder
  @api_call.new_builder
end

#new_parameter(value, key: nil) ⇒ Object



50
51
52
53
54
# File 'lib/school21/api/base_api.rb', line 50

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

#new_request_builder(http_method, path, server) ⇒ Object



43
44
45
46
47
48
# File 'lib/school21/api/base_api.rb', line 43

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

#new_response_handlerObject



36
37
38
39
40
41
# File 'lib/school21/api/base_api.rb', line 36

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

#request_with_auth_participant(http_method, path, server) ⇒ Object



56
57
58
59
60
# File 'lib/school21/api/base_api.rb', line 56

def request_with_auth_participant(http_method, path, server)
  auth_participant = CoreLibrary::Single.new(PLATFORM_AUTH_PARTICIPANT)

  new_request_builder(http_method, path, server).auth(auth_participant)
end