Jerome
Jerome

Reputation: 6189

setting timeout for response for API connection

A Rails controller action has a condition whereby, when met, connects to an API

Class GuestController
  if @book.quantity - @book.guests.count == 0
     call_book_api

Now this particular call should have a response time that will differ from other calls. A Timeout needs to be set regarding the response of call_book_api method with some data (be it positive or negative).

@result = HTTParty.post(
  'https://test.co.uk/interface/Book', 
  :body => JSON.parse(@book).to_json,
  headers: {
    'Content-Type' => 'application/json', 
    'Accept' => 'application/json',
    'Host' => 'test.co.uk',
    'Connection' => 'Keep-Alive'
  }
)    

How can this be efficiently defined at the action level?

Upvotes: 0

Views: 2322

Answers (1)

Rohan
Rohan

Reputation: 2727

As per the description mentioned in the post you want to set the timeout when calling third party API using httparty gem.

Use the below mentioned code if it is a get request

 response = HTTParty.get('YOUR_URL', timeout: 10)

If the it is a post type of request then use the below mentioned code:

 response = HTTParty.post('YOUR_URL', body: params, timeout: 10)

Upvotes: 2

Related Questions