Class: Bearcat::Client

Inherits:
Footrest::Client
  • Object
show all
Defined in:
lib/bearcat/client.rb,
lib/bearcat/client/tabs.rb,
lib/bearcat/client/files.rb,
lib/bearcat/client/pages.rb,
lib/bearcat/client/roles.rb,
lib/bearcat/client/users.rb,
lib/bearcat/client/groups.rb,
lib/bearcat/client/logins.rb,
lib/bearcat/client/rubric.rb,
lib/bearcat/client/search.rb,
lib/bearcat/client_module.rb,
lib/bearcat/client/courses.rb,
lib/bearcat/client/folders.rb,
lib/bearcat/client/modules.rb,
lib/bearcat/client/o_auth2.rb,
lib/bearcat/client/quizzes.rb,
lib/bearcat/client/reports.rb,
lib/bearcat/client/accounts.rb,
lib/bearcat/client/graph_ql.rb,
lib/bearcat/client/outcomes.rb,
lib/bearcat/client/sections.rb,
lib/bearcat/client/analytics.rb,
lib/bearcat/client/progresses.rb,
lib/bearcat/client/assignments.rb,
lib/bearcat/client/conferences.rb,
lib/bearcat/client/discussions.rb,
lib/bearcat/client/enrollments.rb,
lib/bearcat/client/file_helper.rb,
lib/bearcat/client/new_quizzes.rb,
lib/bearcat/client/sis_imports.rb,
lib/bearcat/client/submissions.rb,
lib/bearcat/client/canvas_files.rb,
lib/bearcat/client/module_items.rb,
lib/bearcat/client/conversations.rb,
lib/bearcat/client/external_tools.rb,
lib/bearcat/client/outcome_groups.rb,
lib/bearcat/client/account_reports.rb,
lib/bearcat/client/calendar_events.rb,
lib/bearcat/client/content_exports.rb,
lib/bearcat/client/outcome_imports.rb,
lib/bearcat/client/group_categories.rb,
lib/bearcat/client/assignment_groups.rb,
lib/bearcat/client/blueprint_courses.rb,
lib/bearcat/client/group_memberships.rb,
lib/bearcat/client/learning_outcomes.rb,
lib/bearcat/client/rubric_assessment.rb,
lib/bearcat/client/content_migrations.rb,
lib/bearcat/client/rubric_association.rb,
lib/bearcat/client/account_notifications.rb,
lib/bearcat/client/custom_gradebook_columns.rb

Defined Under Namespace

Modules: AccountNotifications, AccountReports, Accounts, Analytics, AssignmentGroups, Assignments, BlueprintCourses, CalendarEvents, CanvasFiles, ClientModule, Conferences, ContentExports, ContentMigrations, Conversations, Courses, CustomGradebookColumns, Discussions, Enrollments, ExternalTools, FileHelper, Files, Folders, GraphQL, GroupCategories, GroupMemberships, Groups, LearningOutcomes, Logins, ModuleItems, Modules, NewQuizzes, OAuth2, OutcomeGroups, OutcomeImports, Outcomes, Pages, Progresses, Quizzes, Reports, Roles, Rubric, RubricAssessment, RubricAssociation, Search, Sections, SisImports, Submissions, Tabs, Users

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cache_on_self(key, &block) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/bearcat/client.rb', line 92

def self.cache_on_self(key, &block)
  @cache ||= {}
  if Rails.env.development? || Rails.env.test?
    block.call
  else
    @cache[key] ||= block.call
  end
end

.registered_endpointsObject



23
24
25
26
27
28
# File 'lib/bearcat/client.rb', line 23

def self.registered_endpoints
  @registered_endpoints ||= @added_modules.reduce({}) do |h, m|
    h.merge!(m._registered_endpoints) rescue h
  end
  @registered_endpoints
end

Instance Method Details

#cache_on_class(key, &block) ⇒ Object



101
102
103
# File 'lib/bearcat/client.rb', line 101

def cache_on_class(key, &block)
  self.class.cache_on_self(key, &block)
end

#request(method, &block) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/bearcat/client.rb', line 62

def request(method, &block)
  response = retry_request do
    rate_limited_request do
      connection.send(method, &block)
    end
  end
  ApiArray.process_response(response, self)
end

#retry_requestObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/bearcat/client.rb', line 30

def retry_request
  response = nil
  retries_count = 0
  begin
    response = yield
  rescue => e
    # Handle 5xx server errors with retry logic
    if e.respond_to?(:status) && [500, 501, 502, 503, 504].include?(e.status)
      # Retry if we haven't exhausted max_retries
      if config[:max_retries].present? && retries_count != -1 && retries_count < config[:max_retries]
        retries_count += 1
        sleep(config[:retry_delay] || 0) if (config[:retry_delay] || 0) > 0
        retry
      end

      # Retries exhausted or max_retries reached, call callback if present
      # This is called when: retries are exhausted OR we never retried but max_retries is configured
      if config[:on_retries_exceeded].present? && retries_count != -1
        result = config[:on_retries_exceeded].call(e)
        # If callback returns :retry, allow one final retry after modifying parameters
        if result == :retry
          retries_count = -1
          retry
        end
      end
    end

    raise
  end
  response
end

#set_connection(config) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/bearcat/client.rb', line 71

def set_connection(config)
  super
  connection.builder.insert(Footrest::RaiseFootrestErrors, ExtendedRaiseFootrestErrors)
  connection.builder.delete(Footrest::RaiseFootrestErrors)

  connection.builder.insert(Faraday::Request::UrlEncoded, LegacyContentTypeMiddleware)
  connection.builder.delete(Faraday::Request::UrlEncoded)
end

#upload_io(path_or_io, content_type) ⇒ Object

Builds a multipart file part for uploads, bridging Faraday 1.x and 2.x. Faraday 2.x dropped Faraday::UploadIO in favor of Faraday::Multipart::FilePart (from the faraday-multipart gem). Both accept the same (path_or_io, content_type) signature.



84
85
86
87
88
89
90
# File 'lib/bearcat/client.rb', line 84

def upload_io(path_or_io, content_type)
  if defined?(Faraday::Multipart::FilePart)
    Faraday::Multipart::FilePart.new(path_or_io, content_type)
  else
    Faraday::UploadIO.new(path_or_io, content_type)
  end
end