Skip to content

Commit

Permalink
refactoring, add sematic logger, add more endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
ikael21 committed Dec 15, 2024
1 parent 226a271 commit fcab5d8
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 112 deletions.
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
PATH
remote: .
specs:
school21_api_sdk (0.3.0)
school21_api_sdk (0.4.0)
activesupport
apimatic_core
apimatic_core_interfaces
apimatic_faraday_client_adapter
base64
semantic_logger

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -131,6 +132,8 @@ GEM
rubocop-rake (0.6.0)
rubocop (~> 1.0)
ruby-progressbar (1.13.0)
semantic_logger (4.16.1)
concurrent-ruby (~> 1.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,22 @@ password = 'your_password_here'

client = School21::Client.configure do |config|
config.credentials = { login: login, password: password }
config.enable_logging = true # false by default

# If you want to log client's requests and responses (turned off by default)
config.enable_logging = true
end

# Request access token from the API server.
# Access token is stored inside client object and reused for API requests.
# If access token is expired client will automatically request a new one.
client.authenticate!
```

- Select the domain specific API that you want to use. This API has all endpoints related to that domain. Here's an example of `Participant API` and a call to `/participants/:login`

```ruby
participants_api = client.participants_api
response = participants_api.participants('[email protected]')
response = participants_api.participant('peer_nickname')

if response.success?
puts response.data
Expand All @@ -57,4 +64,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ikael21/school21_api_sdk.
Bug reports and pull requests are welcome on GitHub at <https://github.com/ikael21/school21_api_sdk>.
1 change: 1 addition & 0 deletions lib/school21.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'apimatic_core'
require 'apimatic_faraday_client_adapter'
require 'semantic_logger'

require 'active_support/all'

Expand Down
5 changes: 1 addition & 4 deletions lib/school21/api/auth_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ def token(login:, password:)
.form_param(new_parameter('password', key: :grant_type))
.form_param(new_parameter('s21-open-api', key: :client_id))

new_api_call_builder
.request(new_request)
.response(new_response_handler)
.execute
execute_request(new_request)
end
end
end
13 changes: 13 additions & 0 deletions lib/school21/api/base_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,18 @@ def new_parameter(value, key: nil)
.key(key)
.value(value)
end

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

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

def execute_request(new_request)
new_api_call_builder
.request(new_request)
.response(new_response_handler)
.execute
end
end
end
41 changes: 11 additions & 30 deletions lib/school21/api/campuses_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,45 @@ module School21
class CampusesApi < BaseApi
def campuses
path = '/campuses'
new_request = new_request_builder(HttpMethod::GET, path, :api_v1)
.auth(CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT))
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

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

def campuses_participants(campus_id, options: {})
def campus_participants(campus_id, options: {})
path = "/campuses/#{campus_id}/participants"
default_options = { limit: 50, offset: 0 }.merge(options)

new_request = new_request_builder(HttpMethod::GET, path, :api_v1)
.auth(CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT))
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

default_options.each do |key, value|
new_request.query_param(new_parameter(value, key:))
end

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

def campuses_coalitions(campus_id, options: {})
def campus_coalitions(campus_id, options: {})
path = "/campuses/#{campus_id}/coalitions"
default_options = { limit: 50, offset: 0 }.merge(options)

new_request = new_request_builder(HttpMethod::GET, path, :api_v1)
.auth(CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT))
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

default_options.each do |key, value|
new_request.query_param(new_parameter(value, key:))
end

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

def campuses_clusters(campus_id, options: {})
def campus_clusters(campus_id, options: {})
path = "/campuses/#{campus_id}/clusters"
default_options = { limit: 50, offset: 0 }.merge(options)

new_request = new_request_builder(HttpMethod::GET, path, :api_v1)
.auth(CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT))
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

default_options.each do |key, value|
new_request.query_param(new_parameter(value, key:))
end

new_api_call_builder
.request(new_request)
.response(new_response_handler)
.execute
execute_request(new_request)
end
end
end
13 changes: 4 additions & 9 deletions lib/school21/api/clusters_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@

module School21
class ClustersApi < BaseApi
def clusters_map(cluster_id, options: {})
def cluster_map(cluster_id, options: {})
path = "/clusters/#{cluster_id}/map"
default_options = { limit: 50, offset: 0, occupied: true }.merge(options)

new_request = new_request_builder(HttpMethod::GET, path, :api_v1)
.auth(CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT))
default_options = { limit: 50, offset: 0 }.merge(options)
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

default_options.each do |key, value|
new_request.query_param(new_parameter(value, key:))
end

new_api_call_builder
.request(new_request)
.response(new_response_handler)
.execute
execute_request(new_request)
end
end
end
28 changes: 13 additions & 15 deletions lib/school21/api/participants_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@

module School21
class ParticipantsApi < BaseApi
def participants(login)
def participant(login)
path = "/participants/#{login}"
new_request = new_request_builder(HttpMethod::GET, path, :api_v1)
.auth(CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT))
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

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

def participants_projects(login, options: {})
def participant_projects(login, options: {})
path = "/participants/#{login}/projects"
default_options = { limit: 10, offset: 0 }.merge(options)

new_request = new_request_builder(HttpMethod::GET, path, :api_v1)
.auth(CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT))
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

default_options.each do |key, value|
new_request.query_param(new_parameter(value, key:))
end

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

def participant_project(login, project_id)
path = "/participants/#{login}/projects/#{project_id}"
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

execute_request(new_request)
end
end
end
21 changes: 6 additions & 15 deletions lib/school21/api/projects_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,23 @@

module School21
class ProjectsApi < BaseApi
def projects(project_id)
def project(project_id)
path = "/projects/#{project_id}"
new_request = new_request_builder(HttpMethod::GET, path, :api_v1)
.auth(CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT))
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

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

def projects_participants(project_id, options: {})
def project_participants(project_id, options: {})
path = "/projects/#{project_id}/participants"
default_options = { limit: 10, offset: 0 }.merge(options)

new_request = new_request_builder(HttpMethod::GET, path, :api_v1)
.auth(CoreLibrary::Single.new(SINGLE_AUTH_PARTICIPANT))
new_request = authenticated_request(HttpMethod::GET, path, :api_v1)

default_options.each do |key, value|
new_request.query_param(new_parameter(value, key:))
end

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

0 comments on commit fcab5d8

Please sign in to comment.