-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.rb
90 lines (71 loc) · 1.75 KB
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# frozen_string_literal: true
require 'jwt'
require_relative 'jwt_helper'
require 'httparty'
require_relative 'http'
require 'active_support'
require 'byebug'
# Get products
class StoreKitAPI
attr_reader :private_key, :issuer_id, :endpoint, :key_id, :response
ALGORITHM = 'ES256'
HOST = 'https://api.appstoreconnect.apple.com%<endpoint>s'
UnauthenticatedError = Class.new(StandardError)
ForbiddenError = Class.new(StandardError)
def initialize(private_key:, issuer_id:, endpoint:, key_id:)
@issuer_id = issuer_id
@endpoint = endpoint
@private_key = OpenSSL::PKey.read(private_key)
@key_id = key_id
end
def call
puts "JWT: #{jwt}"
puts "JWT Payload: #{payload}"
puts "JWT Headers: #{headers}"
@response = request!
puts decoded_response
end
private
def decoded_response
response
end
def request!
url = format(HOST, endpoint: endpoint)
result = HTTP.get(url, headers: { 'Authorization' => "Bearer #{jwt}" })
raise UnauthenticatedError if result.code == 401
raise ForbiddenError if result.code == 403
save_to_file!(result) if result['content-type'] == 'application/a-gzip'
result.parsed_response
end
def save_to_file!(data)
filename = File.join(Dir.pwd, 'downloads', "report-#{rand(999)}.gzip")
File.open(filename, 'w') do |file|
file.write(data)
end
end
def jwt
JWT.encode(
payload,
private_key,
ALGORITHM,
headers
)
end
def headers
{ kid: key_id, typ: 'JWT' }
end
def payload
{
iss: issuer_id,
iat: timestamp,
exp: timestamp(1200),
aud: 'appstoreconnect-v1',
scope: [
"GET #{endpoint}"
]
}
end
def timestamp(seconds = 0)
Time.now.to_i + seconds
end
end