Class: ITCAnalytics::Interfaces::Gateways::ItunesConnect

Inherits:
Object
  • Object
show all
Defined in:
lib/itc_analytics/interfaces/gateways/itunesconnect.rb

Instance Method Summary collapse

Constructor Details

#initialize(http_controller:, apple_widget_key:) ⇒ ItunesConnect

Returns a new instance of ItunesConnect.



8
9
10
11
# File 'lib/itc_analytics/interfaces/gateways/itunesconnect.rb', line 8

def initialize(http_controller:, apple_widget_key:)
	@http_controller = http_controller
	@apple_widget_key = apple_widget_key
end

Instance Method Details



13
14
15
16
17
18
19
20
21
22
# File 'lib/itc_analytics/interfaces/gateways/itunesconnect.rb', line 13

def (username:, password:)
	result = @http_controller.post(url: "https://idmsa.apple.com/appleauth/auth/signin",
		headers: Hash["Content-Type" => "application/json", "X-Apple-Widget-Key" => @apple_widget_key],
		post_body: "{\"accountName\": \"" << username << "\",\"password\": \"" << password << "\",\"rememberMe\": false}")

	if result['error'] == false && result['headers']['set-cookie'].find { |e| /myacinfo=.+?;/ =~ e }
		return result['headers']['set-cookie'].find { |e| /(myacinfo=.+?;)/ =~ e }.scan(/(myacinfo=.+?;)/).last.first
	end
	raise Exceptions::ItunesRequestFailure, "failed to get myacinfo apple may have changed login routine"
end

#execute_metrics_query(query:) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/itc_analytics/interfaces/gateways/itunesconnect.rb', line 63

def execute_metrics_query(query:)
  url = "https://analytics.itunes.apple.com/analytics/api/v1/data/time-series"
  post_body = query.assemble_body.to_json
  
  result = @http_controller.post(url: url, 
    headers: get_headers([query.session., query.session.itctx_cookie]),
    post_body: post_body)

  return result_body(result)
end

#get_api_url(url:, session_cookies:) ⇒ Object



53
54
55
56
# File 'lib/itc_analytics/interfaces/gateways/itunesconnect.rb', line 53

def get_api_url(url:, session_cookies:)
	result = @http_controller.get(url: url, headers: get_headers(session_cookies))
	return result_body(result)
end

#get_applications_json(session_cookies:) ⇒ Object



58
59
60
61
# File 'lib/itc_analytics/interfaces/gateways/itunesconnect.rb', line 58

def get_applications_json(session_cookies:) 
	url = 'https://analytics.itunes.apple.com/analytics/api/v1/app-info/app'
	return get_api_url(url: url, session_cookies: session_cookies)
end

#get_headers(get_cookies) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/itc_analytics/interfaces/gateways/itunesconnect.rb', line 34

def get_headers(get_cookies) 
	Hash["Content-Type" => "application/json;charset=UTF-8",
		 "Accept" => "application/json, text/plain, */*",
		 "Origin" => "https://analytics.itunes.apple.com",
		 "X-Requested-By" => "analytics.itunes.apple.com",
		 "Referer" => "https://analytics.itunes.apple.com/",
		 "Cookie" => get_cookies]
end


24
25
26
27
28
29
30
31
32
# File 'lib/itc_analytics/interfaces/gateways/itunesconnect.rb', line 24

def get_itctx_cookie(account_info_cookie:) 
	result = @http_controller.get(url: "https://olympus.itunes.apple.com/v1/session",
		headers: Hash["Cookie" => ])

	if result['error'] == false && result['headers']['set-cookie'].find { |e| /itctx=.+?;/ =~ e }
		return result['headers']['set-cookie'].find { |e| /(itctx=.+?;)/ =~ e }.scan(/(itctx=.+?Only)/).last.first
	end
	raise Exceptions::ItunesRequestFailure, "failed to get itctx cookie apple may have changed login routine" 
end

#result_body(result) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/itc_analytics/interfaces/gateways/itunesconnect.rb', line 43

def result_body(result) 
  if result["error"] 
    if result["code"] == 401
      raise Exceptions::ItunesUnauthorized, "Unauthorized call to iTunesConnect #{result['message']}"
    end
    raise Exceptions::ItunesRequestFailure,  "iTunesConnect not responding to #{result['message']}"
  end
  return result["body"]
end