ruby-on-railsrubyapiskyscanner

How to retrieve Session key from Skyscanner API post request - Ruby


In my app I want to fetch Live Price by Flight Details for this I have used SkyScanner API. I have read the documentation before obtained data I have to create Live Pricing Service Session. Which can be created by post request to api and then it provide SessionKey by using this SessionKey and apiKey I can retrived the data. So How can I get Sessionkey as I understood it must be provided by API Server.

Here is my try:

require 'json'
require 'net/http'
require 'uri'


  post_params = { 
    :apiKey => "[API_KEY]",
    :country => "GB",
    :currency => "GBP",
    :locale => "en-GB",
    :adults =>1,
    :children => 0,
    :infants => 0,
    :originplace => '11235',
    :destinationplace => '13554',
    :outbounddate => '2015-05-19',
    :inbounddate => '2015-05-26',
    :locationschema => 'Default',
    :cabinclass => 'Economy',
    :groupPricing => true
  }


sessionkey_request = Net::HTTP.post_form(URI.parse('http://partners.api.skyscanner.net/apiservices/pricing/v1.0'), post_params )
get_data= "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apiKey=[API_KEY]"
puts sessionkey_request.inspect
temp = Net::HTTP.get_response(URI.parse(get_data)).body
# puts temp

In console I get

<Net::HTTPCreated 201 Created readbody=true> # sessionkey_request.inspect

Not getting SessionKey in response without it I can not retrieved data. Please guide me where I do mistake. I appreciate for solution.

Fore more details and live result Check Demo by API

Note: I have check gem 'skyscanner ' but it doesn't provide any method for Live Price. It provide Browse Cache methods.


Solution

  • As per the doc:

    A successful response contains no content. The URL to poll the booking details is specified in the Location header of the response

    so try this:

    sessionkey_request["location"]
    

    I have test it on my system and it returns me:

    http://partners.api.skyscanner.net/apiservices/pricing/v1.0/8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D
    

    last part is your sessionKey which you can use for GET request. If you want only last part(sessionKey) then you can retrieved it by:

     > url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D"
     > url.split('/').last
     => "8e28260becd3441ca4e865396e224e7d_ecilpojl_EC71481935CEBB7EAF661BC24940D01D"