Skip to content

Commit

Permalink
Exception when rate limit is exceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny committed Feb 20, 2024
1 parent 738db02 commit 7f08e87
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/thingiverse/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def access_token=(token)
def get_token
auth_response = self.class.post(auth_url, :query => {:client_id => @client_id, :client_secret => @client_secret, :code => @code})

raise ResponseError.new(auth_response) unless auth_response.success?
raise ResponseError.from(auth_response) unless auth_response.success?

response = CGI::parse(auth_response.parsed_response)

Expand Down
2 changes: 1 addition & 1 deletion lib/thingiverse/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(response, object)
@response = response
@object = object

raise ResponseError.new(@response) unless @response.success?
raise ResponseError.from(@response) unless @response.success?

@objects = @response.parsed_response.collect do |attrs|
@object.new attrs
Expand Down
2 changes: 2 additions & 0 deletions lib/thingiverse/rate_limit_exceeded_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class RateLimitExceededError < ResponseError
end
9 changes: 9 additions & 0 deletions lib/thingiverse/response_error.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
module Thingiverse
class ResponseError < StandardError
def self.from(response)
case response.code.to_i
when 420, 429
RateLimitExceededError.new(response)
else
new(response)
end
end

def initialize(response)
@response = response
end
Expand Down
2 changes: 1 addition & 1 deletion lib/thingiverse/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Tags

def self.find(tag_name)
response = Thingiverse::Connection.get("/tags/#{tag_name}")
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?
self.new response.parsed_response
end

Expand Down
16 changes: 8 additions & 8 deletions lib/thingiverse/things.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Things

def user
response = Thingiverse::Connection.get("/users/#{creator['name']}")
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?
Thingiverse::Users.new response.parsed_response
end

Expand All @@ -26,7 +26,7 @@ def ancestors(query = {})

def tags
response = Thingiverse::Connection.get(tags_url)
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?
response.parsed_response.collect do |attrs|
Thingiverse::Tags.new attrs
end
Expand All @@ -37,7 +37,7 @@ def save
thing = Thingiverse::Things.create(@attributes)
else
response = Thingiverse::Connection.patch("/things/#{id}", :body => @attributes.to_json)
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?

thing = Thingiverse::Things.new(response.parsed_response)
end
Expand All @@ -63,7 +63,7 @@ def upload(file_or_string, thingiverse_filename=nil)
raise ArgumentError, "Unable to determine filename" if thingiverse_filename.to_s == ""

response = Thingiverse::Connection.post("/things/#{id}/files", :body => {:filename => thingiverse_filename}.to_json)
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?

parsed_response = JSON.parse(response.body)
action = parsed_response["action"]
Expand Down Expand Up @@ -96,7 +96,7 @@ def upload(file_or_string, thingiverse_filename=nil)
if c.response_code == 303
# finalize it
response = Thingiverse::Connection.post(query['success_action_redirect'])
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?
Thingiverse::Files.new(response.parsed_response)
else
raise "#{c.response_code}: #{c.body_str}"
Expand All @@ -108,7 +108,7 @@ def publish
raise "Cannot publish until thing is saved"
else
response = Thingiverse::Connection.post("/things/#{id}/publish")
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?

thing = Thingiverse::Things.new(response.parsed_response)
end
Expand All @@ -120,7 +120,7 @@ def publish

def self.find(thing_id)
response = Thingiverse::Connection.get("/things/#{thing_id}")
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?
self.new response.parsed_response
end

Expand All @@ -132,7 +132,7 @@ def self.create(params)
thing = self.new(params)

response = Thingiverse::Connection.post('/things', :body => thing.attributes.to_json)
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?

self.new(response.parsed_response)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/thingiverse/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Users

def self.find(user_name)
response = Thingiverse::Connection.get("/users/#{user_name}")
raise ResponseError.new(response) unless response.success?
raise ResponseError.from(response) unless response.success?
self.new response.parsed_response
end
end
Expand Down

0 comments on commit 7f08e87

Please sign in to comment.