Class: BridgeAPI::Client

Inherits:
Footrest::Client
  • Object
show all
Defined in:
lib/bridge_api/client.rb,
lib/bridge_api/client/role.rb,
lib/bridge_api/client/user.rb,
lib/bridge_api/client/group.rb,
lib/bridge_api/client/account.rb,
lib/bridge_api/client/manager.rb,
lib/bridge_api/client/program.rb,
lib/bridge_api/client/data_dump.rb,
lib/bridge_api/client/enrollment.rb,
lib/bridge_api/client/affiliation.rb,
lib/bridge_api/client/live_course.rb,
lib/bridge_api/client/sub_account.rb,
lib/bridge_api/client/clone_object.rb,
lib/bridge_api/client/custom_field.rb,
lib/bridge_api/client/course_template.rb,
lib/bridge_api/client/program_enrollment.rb,
lib/bridge_api/client/live_course_session.rb,
lib/bridge_api/client/live_course_enrollment.rb

Defined Under Namespace

Modules: Account, Affiliation, CloneObject, CourseTemplate, CustomField, DataDump, Enrollment, Group, LiveCourse, LiveCourseEnrollment, LiveCourseSession, Manager, Program, ProgramEnrollment, Role, SubAccount, User

Constant Summary collapse

DATA_DUMP_DOWNLOAD_PATH =
'/data_dumps/download'.freeze
DATA_DUMP_PATH =
'/data_dumps'.freeze
COURSE_TEMPLATE_PATH =
'/course_templates'.freeze
ENROLLMENT_PATH =
'/enrollments'.freeze
LTI_TOOLS_PATH =
'/lti_tools'.freeze
PROGRAM_PATH =
'/programs'.freeze
PROGRAM_ENROLLMENT_PATH =
'/learners'.freeze
USER_PATH =
'/users'.freeze
GROUPS_PATH =
'/groups'.freeze
MANAGER_PATH =
'/managers'.freeze
ADMIN_PATH =
'/admin'.freeze
AUTHOR_PATH =
'/author'.freeze
LEARNER_PATH =
'/learner'.freeze
CUSTOM_FIELD_PATH =
'/custom_fields'.freeze
SUB_ACCOUNT_PATH =
'/sub_accounts'.freeze
SUPPORT_PATH =
'/support'.freeze
ACCOUNT_PATH =
'/accounts'.freeze
CLONE_OBJECTS_PATH =
'/clone_objects'.freeze
API_VERSION =
1
API_PATH =
'/api'.freeze
ROLE_PATH =
'/roles'.freeze
AFFILIATED_SUBACCOUNTS =
'/affiliated_sub_accounts'.freeze
BATCH_PATH =
'/batch'.freeze
LIVE_COURSES_PATH =
'/live_courses'.freeze
SESSIONS_PATH =
'/sessions'.freeze
PUBLISH_PATH =
'/publish'.freeze
WEB_CONFERENCE_PATH =
'/web_conference'.freeze
RESTORE_PATH =
'/restore'.freeze
RESULT_MAPPING =
{}

Instance Method Summary collapse

Instance Method Details

#apply_rate_limits(response) ⇒ Object



70
71
72
73
74
# File 'lib/bridge_api/client.rb', line 70

def apply_rate_limits(response)
  limit = response.headers['x-rate-limit-remaining']
  return if limit.nil?
  self.current_limit = limit.to_i
end

#current_limitObject



76
77
78
# File 'lib/bridge_api/client.rb', line 76

def current_limit
  BridgeAPI.rate_limits[config[:api_key]]
end

#current_limit=(value) ⇒ Object



80
81
82
# File 'lib/bridge_api/client.rb', line 80

def current_limit=(value)
  BridgeAPI.rate_limits[config[:api_key]] = value
end

#enforce_rate_limitsObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bridge_api/client.rb', line 57

def enforce_rate_limits
  return unless BridgeAPI.enforce_rate_limits && !current_limit.nil?
  return unless current_limit < BridgeAPI::rate_limit_min
  tts = ((BridgeAPI.rate_limit_min - current_limit) / 5).ceil
  tts = BridgeAPI.min_sleep_seconds if tts < BridgeAPI.min_sleep_seconds
  tts = BridgeAPI.max_sleep_seconds if tts > BridgeAPI.max_sleep_seconds
  message = "Bridge API rate limit minimum #{BridgeAPI.rate_limit_min} reached for key: '#{config[:api_key]}'. "\
    "Sleeping for #{tts} second(s) to catch up ~zzZZ~. "\
    "Limit Remaining: #{current_limit}"
  BridgeAPI.logger.debug(message)
  sleep(tts)
end

#request(method, &block) ⇒ Object

Override Footrest request for ApiArray support



50
51
52
53
54
55
# File 'lib/bridge_api/client.rb', line 50

def request(method, &block)
  enforce_rate_limits
  response = connection.send(method, &block)
  apply_rate_limits(response)
  ApiArray.process_response(response, self, RESULT_MAPPING)
end

#set_connection(config) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bridge_api/client.rb', line 84

def set_connection(config)
  config[:logger] = config[:logging] if config[:logging]
  @connection = Faraday.new(url: config[:prefix]) do |faraday|
    faraday.request                     :multipart
    faraday.request                     :url_encoded
    if config[:logger] == true
      faraday.response :logger
    elsif config[:logger]
      faraday.use Faraday::Response::Logger, config[:logger]
    end
    faraday.use                         Footrest::FollowRedirects, limit: 5 unless config[:follow_redirects] == false
    faraday.adapter                     Faraday.default_adapter
    faraday.use                         Footrest::ParseJson, content_type: /\bjson$/
    faraday.use                         Footrest::RaiseFootrestErrors
    faraday.use                         Footrest::Pagination
    faraday.headers[:accept]          = 'application/json'
    faraday.headers[:authorization]   = "Bearer #{config[:token]}" if config[:token]
    faraday.headers[:user_agent]      = 'Footrest'
    if config[:api_key] && config[:api_secret]
      faraday.headers[:authorization] =   'Basic ' + Base64.strict_encode64("#{config[:api_key]}:#{config[:api_secret]}")
    elsif config[:token]
      faraday.headers[:authorization] =   "Bearer #{config[:token]}"
    else
      raise 'No api authorization provided'
    end
  end
end