lib/spaceship/client.rb in spaceship-0.15.1 vs lib/spaceship/client.rb in spaceship-0.15.2
- old
+ new
@@ -1,8 +1,9 @@
require 'faraday' # HTTP Client
require 'logger'
require 'faraday_middleware'
+require 'faraday-cookie_jar'
require 'spaceship/ui'
require 'spaceship/helper/plist_middleware'
require 'spaceship/helper/net_http_generic_request'
if ENV["DEBUG"]
@@ -12,13 +13,13 @@
end
module Spaceship
class Client
PROTOCOL_VERSION = "QH65B2"
+ USER_AGENT = "Spaceship #{Spaceship::VERSION}"
attr_reader :client
- attr_accessor :cookie
# The user that is currently logged in
attr_accessor :user
# The logger in which all requests are logged
@@ -65,10 +66,11 @@
def initialize
@client = Faraday.new(self.class.hostname) do |c|
c.response :json, content_type: /\bjson$/
c.response :xml, content_type: /\bxml$/
c.response :plist, content_type: /\bplist$/
+ c.use :cookie_jar
c.adapter Faraday.default_adapter
if ENV['DEBUG']
# for debugging only
# This enables tracking of networking requests using Charles Web Proxy
@@ -167,15 +169,10 @@
puts "Please run this tool again to apply the new password"
end
end
end
- # @return (Bool) Do we have a valid session?
- def session?
- !!@cookie
- end
-
def with_retry(tries = 5, &block)
return block.call
rescue Faraday::Error::TimeoutError, AppleTimeoutError => ex # New Faraday version: Faraday::TimeoutError => ex
unless (tries -= 1).zero?
logger.warn("Timeout received: '#{ex.message}'. Retrying after 3 seconds (remaining: #{tries})...")
@@ -202,14 +199,11 @@
def csrf_tokens
@csrf_tokens || {}
end
def request(method, url_or_path = nil, params = nil, headers = {}, &block)
- if session?
- headers.merge!({ 'Cookie' => cookie })
- headers.merge!(csrf_tokens)
- end
- headers.merge!({ 'User-Agent' => 'spaceship' })
+ headers.merge!(csrf_tokens)
+ headers.merge!({ 'User-Agent' => USER_AGENT })
# Before encoding the parameters, log them
log_request(method, url_or_path, params)
# form-encode the params only if there are params, and the block is not supplied.