Skip to content

Commit

Permalink
Merge pull request #7 from CareerArcGroup/feature/add-timeout-options
Browse files Browse the repository at this point in the history
Expose timeout options
  • Loading branch information
pote authored Jun 16, 2022
2 parents fe7fd68 + 86a8d09 commit 227dc98
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/pexels/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def collections
@collections ||= Pexels::Client::Collections.new(self)
end

def request(path, method: 'GET', params: {})
request = Request.new(api_key, path, method, params)
def request(path, method: 'GET', params: {}, options: {})
request = Request.new(api_key, path, method, params, options)
request.call.tap do |response|
@ratelimit_remaining = response.ratelimit_remaining
end
Expand Down
18 changes: 15 additions & 3 deletions lib/pexels/client/collections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,47 @@ def initialize(client)
@client = client
end

def all(per_page: 15, page: 1)
def all(per_page: 15, page: 1, timeout: { open: nil, read: nil })
response = @client.request(
"#{Pexels.api_version}/collections",
params: {
per_page: per_page,
page: page
},
options: {
open_timeout: timeout[:open],
read_timeout: timeout[:read]
})

Pexels::CollectionSet.new(response)
end

def featured(per_page: 15, page: 1)
def featured(per_page: 15, page: 1, timeout: { open: nil, read: nil })
response = @client.request(
"#{Pexels.api_version}/collections/featured",
params: {
per_page: per_page,
page: page
},
options: {
open_timeout: timeout[:open],
read_timeout: timeout[:read]
})

Pexels::CollectionSet.new(response)
end

def [](id, type: nil, per_page: 15, page: 1)
def [](id, type: nil, per_page: 15, page: 1, timeout: { open: nil, read: nil })
response = @client.request(
"#{Pexels.api_version}/collections/#{id}",
params: {
per_page: per_page,
page: page,
type: type
},
options: {
open_timeout: timeout[:open],
read_timeout: timeout[:read]
})

Pexels::CollectionMediaSet.new(response)
Expand Down
21 changes: 16 additions & 5 deletions lib/pexels/client/photos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ def initialize(client)
@client = client
end

def [](id)
response = @client.request("#{Pexels.api_version}/photos/#{id}")
def [](id, timeout: { open: nil, read: nil })
response = @client.request(
"#{Pexels.api_version}/photos/#{id}",
options: { open_timeout: timeout[:open], read_timeout: timeout[:read] }
)
Pexels::Photo.new(response.body)
end
alias_method :find, :[]

def search(query, per_page: 15, page: 1, locale: 'en-US', orientation: nil, size: nil, color: nil)
def search(query, per_page: 15, page: 1, locale: 'en-US', orientation: nil, size: nil, color: nil, timeout: { open: nil, read: nil })
validate_search_params(orientation, size, color)

response = @client.request(
Expand All @@ -26,18 +29,26 @@ def search(query, per_page: 15, page: 1, locale: 'en-US', orientation: nil, size
orientation: orientation,
size: size,
color: color
}.compact
}.compact,
options: {
open_timeout: timeout[:open],
read_timeout: timeout[:read]
}
)

Pexels::PhotoSet.new(response)
end

def curated(per_page: 15, page: 1)
def curated(per_page: 15, page: 1, timeout: { open: nil, read: nil })
response = @client.request(
"#{Pexels.api_version}/curated",
params: {
per_page: per_page,
page: page
},
options: {
open_timeout: timeout[:open],
read_timeout: timeout[:read]
}
)

Expand Down
8 changes: 5 additions & 3 deletions lib/pexels/client/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
module Pexels
class Client
class Request
attr_reader :api_key, :path, :method, :params
attr_reader :api_key, :path, :method, :params, :options

def initialize(api_key, path, method, params)
def initialize(api_key, path, method, params, options)
@api_key = api_key
@path = path
@method = method
@params = params
@options = options
end

def call
Expand All @@ -28,7 +29,8 @@ def execute
method,
url,
params: params,
headers: headers
headers: headers,
options: options
)
end

Expand Down
26 changes: 20 additions & 6 deletions lib/pexels/client/videos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ def initialize(client)
@client = client
end

def [](id)
response = @client.request("/videos/videos/#{id}")
def [](id, timeout: { open: nil, read: nil })
response = @client.request(
"/videos/videos/#{id}",
options: {
open_timeout: timeout[:open],
read_timeout: timeout[:read]
}
)
Pexels::Video.new(response.body)
end
alias_method :find, :[]

def search(query, per_page: 15, page: 1, orientation: nil, size: nil)
def search(query, per_page: 15, page: 1, orientation: nil, size: nil, timeout: { open: nil, read: nil })
validate_search_params(orientation, size)

response = @client.request(
Expand All @@ -24,18 +30,26 @@ def search(query, per_page: 15, page: 1, orientation: nil, size: nil)
page: page,
orientation: orientation,
size: size
}.compact
}.compact,
options: {
open_timeout: timeout[:open],
read_timeout: timeout[:read]
}
)

Pexels::VideoSet.new(response)
end

def popular(per_page: 15, page: 1)
def popular(per_page: 15, page: 1, timeout: { open: nil, read: nil })
response = @client.request(
'/videos/popular',
params: {
per_page: per_page,
page: page,
page: page
},
options: {
open_timeout: timeout[:open],
read_timeout: timeout[:read]
}
)

Expand Down
48 changes: 48 additions & 0 deletions test/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,52 @@ def test_invalid_get_collection
end
assert error.message, 'Not Found'
end

def test_get_collection_open_timeout
error = assert_raises Pexels::APIError do
@client.collections[@collection.id, timeout: { open: 0.0000001 }]
end

assert_equal 'execution expired', error.message
end

def test_get_collection_read_timeout
error = assert_raises Pexels::APIError do
@client.collections[@collection.id, timeout: { read: 0.0000001 }]
end

assert_equal 'Net::ReadTimeout', error.message
end

def test_all_open_timeout
error = assert_raises Pexels::APIError do
@client.collections.all(timeout: { open: 0.0000001 })
end

assert_equal 'execution expired', error.message
end

def test_all_read_timeout
error = assert_raises Pexels::APIError do
@client.collections.all(timeout: { read: 0.0000001 })
end

assert_equal 'Net::ReadTimeout', error.message
end

def test_featured_open_timeout
error = assert_raises Pexels::APIError do
@client.collections.featured(timeout: { open: 0.0000001 })
end

assert_equal 'execution expired', error.message
end

def test_featured_read_timeout
error = assert_raises Pexels::APIError do
@client.collections.featured(timeout: { read: 0.0000001 })
end

assert_equal 'Net::ReadTimeout', error.message
end
end
48 changes: 48 additions & 0 deletions test/photo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,52 @@ def test_search_filters
assert_kind_of Pexels::PhotoSet, search_result
assert search_result.photos.any?
end

def test_get_photo_open_timeout
error = assert_raises Pexels::APIError do
@client.photos[@photo.id, timeout: { open: 0.0000001 }]
end

assert_equal 'execution expired', error.message
end

def test_get_photo_read_timeout
error = assert_raises Pexels::APIError do
@client.photos[@photo.id, timeout: { read: 0.0000001 }]
end

assert_equal 'Net::ReadTimeout', error.message
end

def test_search_open_timeout
error = assert_raises Pexels::APIError do
@client.photos.search('test', timeout: { open: 0.0000001 })
end

assert_equal 'execution expired', error.message
end

def test_search_read_timeout
error = assert_raises Pexels::APIError do
@client.photos.search('test', timeout: { read: 0.0000001 })
end

assert_equal 'Net::ReadTimeout', error.message
end

def test_curated_open_timeout
error = assert_raises Pexels::APIError do
@client.photos.curated(timeout: { open: 0.0000001 })
end

assert_equal 'execution expired', error.message
end

def test_curated_read_timeout
error = assert_raises Pexels::APIError do
@client.photos.curated(timeout: { read: 0.0000001 })
end

assert_equal 'Net::ReadTimeout', error.message
end
end
48 changes: 48 additions & 0 deletions test/video_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,52 @@ def test_search_filters
assert_kind_of Pexels::VideoSet, search_result
assert search_result.videos.any?
end

def test_get_video_open_timeout
error = assert_raises Pexels::APIError do
@client.videos[@video.id, timeout: { open: 0.0000001 }]
end

assert_equal 'execution expired', error.message
end

def test_get_video_read_timeout
error = assert_raises Pexels::APIError do
@client.videos[@video.id, timeout: { read: 0.0000001 }]
end

assert_equal 'Net::ReadTimeout', error.message
end

def test_search_open_timeout
error = assert_raises Pexels::APIError do
@client.videos.search('cat', timeout: { open: 0.0000001 })
end

assert_equal 'execution expired', error.message
end

def test_search_read_timeout
error = assert_raises Pexels::APIError do
@client.videos.search('cat', timeout: { read: 0.0000001 })
end

assert_equal 'Net::ReadTimeout', error.message
end

def test_featured_open_timeout
error = assert_raises Pexels::APIError do
@client.videos.popular(timeout: { open: 0.0000001 })
end

assert_equal 'execution expired', error.message
end

def test_featured_read_timeout
error = assert_raises Pexels::APIError do
@client.videos.popular(timeout: { read: 0.0000001 })
end

assert_equal 'Net::ReadTimeout', error.message
end
end

0 comments on commit 227dc98

Please sign in to comment.