psn-client 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -0
- data/CLAUDE.md +24 -14
- data/README.md +131 -5
- data/lib/psn/client/account.rb +4 -2
- data/lib/psn/client/auth.rb +137 -37
- data/lib/psn/client/errors.rb +103 -0
- data/lib/psn/client/pagination.rb +1 -1
- data/lib/psn/client/request.rb +59 -18
- data/lib/psn/client/session.rb +57 -0
- data/lib/psn/client/tokens.rb +53 -0
- data/lib/psn/client/trophies.rb +65 -39
- data/lib/psn/client/users.rb +5 -3
- data/lib/psn/client/version.rb +1 -1
- data/lib/psn/client.rb +3 -8
- data/sig/psn/client/account.rbs +1 -1
- data/sig/psn/client/auth.rbs +25 -5
- data/sig/psn/client/errors.rbs +48 -0
- data/sig/psn/client/request.rbs +10 -3
- data/sig/psn/client/session.rbs +19 -0
- data/sig/psn/client/tokens.rbs +21 -0
- data/sig/psn/client/trophies.rbs +19 -8
- data/sig/psn/client/users.rbs +1 -1
- data/sig/psn/client.rbs +0 -6
- metadata +7 -1
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'openssl'
|
|
5
|
+
require 'time'
|
|
6
|
+
require 'zlib'
|
|
7
|
+
|
|
8
|
+
module PSN
|
|
9
|
+
module Client
|
|
10
|
+
# Base class for every error the gem raises. When the failure came from an HTTP
|
|
11
|
+
# response, +status+, +headers+ and +body+ hold its details; otherwise they are
|
|
12
|
+
# nil/empty. The underlying exception, if any, is available through +cause+.
|
|
13
|
+
class Error < StandardError
|
|
14
|
+
attr_reader :status, :headers, :body
|
|
15
|
+
|
|
16
|
+
# Keyword arguments for +new+ taken from a Net::HTTPResponse or an OAuth2::Response;
|
|
17
|
+
# header names are lowercased. Anything else yields no details.
|
|
18
|
+
def self.details_from(response)
|
|
19
|
+
if response.respond_to?(:each_header)
|
|
20
|
+
{ status: Integer(response.code, 10), headers: response.each_header.to_h, body: response.body }
|
|
21
|
+
elsif response.respond_to?(:status)
|
|
22
|
+
{ status: response.status, headers: response.headers.to_h.transform_keys(&:downcase), body: response.body }
|
|
23
|
+
else
|
|
24
|
+
{}
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize(message = nil, status: nil, headers: {}, body: nil)
|
|
29
|
+
super(message)
|
|
30
|
+
@status = status
|
|
31
|
+
@headers = headers
|
|
32
|
+
@body = body
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Authentication failed: an expired or invalid NPSSO, a rejected token exchange,
|
|
37
|
+
# or an API response of HTTP 401. +error_code+ is Sony's OAuth error code
|
|
38
|
+
# (for example "login_required"), when one was given.
|
|
39
|
+
class AuthenticationError < Error
|
|
40
|
+
attr_reader :error_code
|
|
41
|
+
|
|
42
|
+
def initialize(message = nil, error_code: nil, **details)
|
|
43
|
+
super(message, **details)
|
|
44
|
+
@error_code = error_code
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# HTTP 403 caused by the target user's privacy settings.
|
|
49
|
+
class PrivacyError < Error; end
|
|
50
|
+
|
|
51
|
+
# HTTP 429. +retry_at+ is when Sony says requests will be accepted again, taken
|
|
52
|
+
# from X-RateLimit-Next-Available or Retry-After, or nil when neither is usable.
|
|
53
|
+
# A relative Retry-After is resolved against the time the error was created.
|
|
54
|
+
class RateLimitError < Error
|
|
55
|
+
attr_reader :retry_at
|
|
56
|
+
|
|
57
|
+
def initialize(message = nil, **details)
|
|
58
|
+
super
|
|
59
|
+
@retry_at = parse_retry_at(Time.now)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def parse_retry_at(now)
|
|
65
|
+
next_available_time(headers['x-ratelimit-next-available']) || retry_after_time(headers['retry-after'], now)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def next_available_time(value)
|
|
69
|
+
Time.at(Integer(value, 10)) if value&.match?(/\A\d+\z/)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def retry_after_time(value, now)
|
|
73
|
+
return unless value
|
|
74
|
+
|
|
75
|
+
value.match?(/\A\d+\z/) ? now + Integer(value, 10) : Time.httpdate(value)
|
|
76
|
+
rescue ArgumentError
|
|
77
|
+
nil
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# A GraphQL response carried +errors+ (raised even on HTTP 200). +data+ holds any
|
|
82
|
+
# partial result Sony returned alongside them.
|
|
83
|
+
class GraphQLError < Error
|
|
84
|
+
attr_reader :errors, :data
|
|
85
|
+
|
|
86
|
+
def initialize(message = nil, errors: [], data: nil, **details)
|
|
87
|
+
super(message, **details)
|
|
88
|
+
@errors = errors
|
|
89
|
+
@data = data
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# The request never produced a usable response: DNS, connection, TLS or timeout
|
|
94
|
+
# failure, or a response that could not be read (malformed headers, corrupt gzip body).
|
|
95
|
+
class TransportError < Error
|
|
96
|
+
NETWORK_ERRORS = [IOError, SocketError, SystemCallError, Timeout::Error, OpenSSL::SSL::SSLError,
|
|
97
|
+
Net::ProtocolError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Zlib::Error].freeze
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# A successful response (or token exchange) whose body was not valid JSON.
|
|
101
|
+
class ParseError < Error; end
|
|
102
|
+
end
|
|
103
|
+
end
|
data/lib/psn/client/request.rb
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'net/http'
|
|
4
4
|
require 'json'
|
|
5
|
+
require_relative 'errors'
|
|
5
6
|
require_relative 'version'
|
|
6
7
|
|
|
7
8
|
module PSN
|
|
@@ -22,14 +23,31 @@ module PSN
|
|
|
22
23
|
# request identifies the gem instead.
|
|
23
24
|
USER_AGENT = "psn-client/#{VERSION}".freeze
|
|
24
25
|
|
|
25
|
-
|
|
26
|
+
# Returns the injected request object, or builds one from the access token and
|
|
27
|
+
# options (see #initialize). Options cannot be combined with an injected request,
|
|
28
|
+
# which carries its own configuration.
|
|
29
|
+
def self.for(access_token, request = nil, **options)
|
|
30
|
+
if request
|
|
31
|
+
return request if options.empty?
|
|
32
|
+
|
|
33
|
+
raise ArgumentError, "#{options.keys.join(', ')} cannot be combined with an injected request"
|
|
34
|
+
end
|
|
35
|
+
raise ArgumentError, 'an access token or a request object is required' if access_token.nil?
|
|
36
|
+
|
|
37
|
+
new(access_token, **options)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Timeouts are in seconds; nil keeps Net::HTTP's default.
|
|
41
|
+
def initialize(access_token, open_timeout: nil, read_timeout: nil, write_timeout: nil)
|
|
26
42
|
@access_token = access_token
|
|
43
|
+
@http_options = { use_ssl: true, open_timeout: open_timeout, read_timeout: read_timeout,
|
|
44
|
+
write_timeout: write_timeout }.compact
|
|
27
45
|
end
|
|
28
46
|
|
|
29
47
|
def get(path, host: :mobile, params: {}, headers: {})
|
|
30
48
|
uri = build_uri(host, path, params)
|
|
31
49
|
|
|
32
|
-
perform(uri, authorize(Net::HTTP::Get.new(uri), headers))
|
|
50
|
+
perform(uri, authorize(Net::HTTP::Get.new(uri), headers)).first
|
|
33
51
|
end
|
|
34
52
|
|
|
35
53
|
def post(path, body, host: :mobile, headers: {})
|
|
@@ -38,7 +56,7 @@ module PSN
|
|
|
38
56
|
request['Content-Type'] = 'application/json'
|
|
39
57
|
request.body = JSON.generate(body)
|
|
40
58
|
|
|
41
|
-
perform(uri, request)
|
|
59
|
+
perform(uri, request).first
|
|
42
60
|
end
|
|
43
61
|
|
|
44
62
|
def graphql(operation_name, variables, sha256_hash, host: :mobile, headers: {})
|
|
@@ -47,9 +65,10 @@ module PSN
|
|
|
47
65
|
'variables' => JSON.generate(variables),
|
|
48
66
|
'extensions' => JSON.generate('persistedQuery' => { 'version' => 1, 'sha256Hash' => sha256_hash })
|
|
49
67
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
68
|
+
uri = build_uri(host, GRAPHQL_PATH, params)
|
|
69
|
+
request = authorize(Net::HTTP::Get.new(uri), { 'Apollo-Require-Preflight' => 'true' }.merge(headers))
|
|
70
|
+
result, response = perform(uri, request)
|
|
71
|
+
check_graphql_result(result, response)
|
|
53
72
|
result
|
|
54
73
|
end
|
|
55
74
|
|
|
@@ -62,12 +81,19 @@ module PSN
|
|
|
62
81
|
request
|
|
63
82
|
end
|
|
64
83
|
|
|
65
|
-
def raise_graphql_errors(result)
|
|
84
|
+
def raise_graphql_errors(result, response)
|
|
66
85
|
errors = result['errors'] if result.is_a?(Hash)
|
|
67
86
|
return unless errors.is_a?(Array) && !errors.empty?
|
|
68
87
|
|
|
69
88
|
messages = errors.filter_map { |error| error.is_a?(Hash) ? error['message'] : error.to_s }
|
|
70
|
-
raise
|
|
89
|
+
raise GraphQLError.new("PSN GraphQL error: #{messages.join('; ')}",
|
|
90
|
+
errors: errors, data: result['data'], **Error.details_from(response))
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def check_graphql_result(result, response)
|
|
94
|
+
raise ParseError.new('PSN GraphQL response was empty', **Error.details_from(response)) if result.nil?
|
|
95
|
+
|
|
96
|
+
raise_graphql_errors(result, response)
|
|
71
97
|
end
|
|
72
98
|
|
|
73
99
|
def build_uri(host, path, params)
|
|
@@ -79,29 +105,44 @@ module PSN
|
|
|
79
105
|
URI(url)
|
|
80
106
|
end
|
|
81
107
|
|
|
108
|
+
# Returns the parsed body and the raw response.
|
|
82
109
|
def perform(uri, http_request)
|
|
83
|
-
response = Net::HTTP.start(uri.hostname, uri.port,
|
|
110
|
+
response = Net::HTTP.start(uri.hostname, uri.port, **@http_options) do |http|
|
|
84
111
|
http.request(http_request)
|
|
85
112
|
end
|
|
86
113
|
|
|
87
|
-
parse_response(response)
|
|
114
|
+
[parse_response(response), response]
|
|
115
|
+
rescue *TransportError::NETWORK_ERRORS => e
|
|
116
|
+
raise TransportError, "Request to #{uri.hostname} failed: #{e.class}: #{e.message}"
|
|
88
117
|
end
|
|
89
118
|
|
|
90
119
|
def parse_response(response)
|
|
91
|
-
|
|
92
|
-
|
|
120
|
+
raise error_class_for(response).new(failure_message(response), **Error.details_from(response)) \
|
|
121
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
122
|
+
|
|
123
|
+
return if response.body.nil? || response.body.empty?
|
|
124
|
+
|
|
125
|
+
begin
|
|
93
126
|
JSON.parse(response.body)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
127
|
+
rescue JSON::ParserError => e
|
|
128
|
+
raise ParseError.new("Invalid JSON in response: #{e.message}", **Error.details_from(response))
|
|
129
|
+
end
|
|
130
|
+
end
|
|
97
131
|
|
|
98
|
-
|
|
132
|
+
def error_class_for(response)
|
|
133
|
+
case response.code
|
|
134
|
+
when '401' then AuthenticationError
|
|
135
|
+
when '429' then RateLimitError
|
|
136
|
+
when '403' then privacy_error?(response) ? PrivacyError : Error
|
|
137
|
+
else Error
|
|
99
138
|
end
|
|
100
139
|
end
|
|
101
140
|
|
|
102
|
-
def
|
|
103
|
-
|
|
141
|
+
def failure_message(response)
|
|
142
|
+
"Request failed: #{response.code} #{response.message} - #{response.body}"
|
|
143
|
+
end
|
|
104
144
|
|
|
145
|
+
def privacy_error?(response)
|
|
105
146
|
body = JSON.parse(response.body)
|
|
106
147
|
error = body['error'] if body.is_a?(Hash)
|
|
107
148
|
error = error['message'] if error.is_a?(Hash)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'account'
|
|
4
|
+
require_relative 'auth'
|
|
5
|
+
require_relative 'request'
|
|
6
|
+
require_relative 'tokens'
|
|
7
|
+
require_relative 'trophies'
|
|
8
|
+
require_relative 'users'
|
|
9
|
+
|
|
10
|
+
module PSN
|
|
11
|
+
module Client
|
|
12
|
+
# Shares one Request, and so one access token and transport configuration,
|
|
13
|
+
# across the API classes. A session keeps the access token it was built with: once
|
|
14
|
+
# that expires, build another with Session.from_refresh_token.
|
|
15
|
+
class Session
|
|
16
|
+
# +tokens+ is the Tokens the session was built from, or nil when it was built from
|
|
17
|
+
# an access token string or an injected request.
|
|
18
|
+
attr_reader :request, :tokens
|
|
19
|
+
|
|
20
|
+
# Authenticates with the NPSSO and returns a session for the new tokens. Timeouts (in
|
|
21
|
+
# seconds) apply to authentication and to the session's requests; other options are
|
|
22
|
+
# passed to Auth.new.
|
|
23
|
+
def self.authenticate(open_timeout: nil, read_timeout: nil, write_timeout: nil, **auth_options)
|
|
24
|
+
timeouts = { open_timeout: open_timeout, read_timeout: read_timeout, write_timeout: write_timeout }.compact
|
|
25
|
+
new(Auth.tokens(**auth_options, **timeouts), **timeouts)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Trades a saved refresh token for new tokens, without the NPSSO, and returns a session
|
|
29
|
+
# for them. Takes the same options as Session.authenticate.
|
|
30
|
+
def self.from_refresh_token(refresh_token, open_timeout: nil, read_timeout: nil, write_timeout: nil,
|
|
31
|
+
**auth_options)
|
|
32
|
+
timeouts = { open_timeout: open_timeout, read_timeout: read_timeout, write_timeout: write_timeout }.compact
|
|
33
|
+
new(Auth.refresh(refresh_token, **auth_options, **timeouts), **timeouts)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Pass an access token string or a Tokens, optionally with timeouts for the Request
|
|
37
|
+
# built from it, or `request:` to inject a Request (or any object responding to the
|
|
38
|
+
# same methods) instead of building one.
|
|
39
|
+
def initialize(access_token = nil, request: nil, **timeouts)
|
|
40
|
+
@tokens = access_token if access_token.is_a?(Tokens)
|
|
41
|
+
@request = Request.for(@tokens ? @tokens.access_token : access_token, request, **timeouts)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def trophies
|
|
45
|
+
@trophies ||= Trophies.new(request: request)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def users
|
|
49
|
+
@users ||= Users.new(request: request)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def account
|
|
53
|
+
@account ||= Account.new(request: request)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PSN
|
|
4
|
+
module Client
|
|
5
|
+
# The access token and refresh token from one PSN token response, with the time each
|
|
6
|
+
# expires (nil when the response did not say). The token values are secrets, so
|
|
7
|
+
# #inspect and #to_s leave them out.
|
|
8
|
+
class Tokens
|
|
9
|
+
attr_reader :access_token, :expires_at, :refresh_token, :refresh_token_expires_at
|
|
10
|
+
|
|
11
|
+
# Builds Tokens from the oauth2 gem's token. The refresh token's lifetime is counted
|
|
12
|
+
# from +now+, and +fallback_refresh_token+ is kept when the response carries none.
|
|
13
|
+
def self.from_oauth2(oauth_token, fallback_refresh_token: nil, now: Time.now)
|
|
14
|
+
lifetime = Integer(oauth_token.params['refresh_token_expires_in'], exception: false)
|
|
15
|
+
|
|
16
|
+
new(access_token: oauth_token.token,
|
|
17
|
+
expires_at: oauth_token.expires_at && Time.at(oauth_token.expires_at),
|
|
18
|
+
refresh_token: [oauth_token.refresh_token, fallback_refresh_token].find { |token| !token.to_s.empty? },
|
|
19
|
+
refresh_token_expires_at: lifetime && (now + lifetime))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def initialize(access_token:, expires_at: nil, refresh_token: nil, refresh_token_expires_at: nil)
|
|
23
|
+
@access_token = access_token
|
|
24
|
+
@expires_at = expires_at
|
|
25
|
+
@refresh_token = refresh_token
|
|
26
|
+
@refresh_token_expires_at = refresh_token_expires_at
|
|
27
|
+
freeze
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# False when the expiry time is unknown.
|
|
31
|
+
def expired?(now = Time.now)
|
|
32
|
+
past?(expires_at, now)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# False when the expiry time is unknown.
|
|
36
|
+
def refresh_token_expired?(now = Time.now)
|
|
37
|
+
past?(refresh_token_expires_at, now)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def inspect
|
|
41
|
+
"#<#{self.class.name} expires_at=#{expires_at.inspect} " \
|
|
42
|
+
"refresh_token_expires_at=#{refresh_token_expires_at.inspect}>"
|
|
43
|
+
end
|
|
44
|
+
alias to_s inspect
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def past?(time, now)
|
|
49
|
+
!time.nil? && now >= time
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
data/lib/psn/client/trophies.rb
CHANGED
|
@@ -1,67 +1,97 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'request'
|
|
4
|
+
require_relative 'pagination'
|
|
4
5
|
|
|
5
6
|
module PSN
|
|
6
7
|
module Client
|
|
7
8
|
# Handles trophy-related API requests
|
|
8
9
|
class Trophies
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
include Pagination
|
|
11
|
+
|
|
12
|
+
# Sony answers HTTP 400 for a trophy titles limit above 800. The per-title
|
|
13
|
+
# trophy lists accept any limit of at least 1 (probed up to 100,000), so they
|
|
14
|
+
# get no upper bound. Verified against the live API on 2026-11-23.
|
|
15
|
+
TROPHY_TITLES_MAX_LIMIT = 800
|
|
16
|
+
# Sony answers HTTP 400 for anything else in these path segments, including
|
|
17
|
+
# online IDs as the user and lowercase communication IDs (verified live 2026-11-23).
|
|
18
|
+
USER_ID_PATTERN = /\A(me|\d+)\z/
|
|
19
|
+
NP_COMMUNICATION_ID_PATTERN = /\ANPWR\d{5}_\d{2}\z/
|
|
20
|
+
TROPHY_GROUP_ID_PATTERN = /\A(all|default|\d{3})\z/
|
|
21
|
+
|
|
22
|
+
# Pass an access token, or `request:` to inject a Request (or any object
|
|
23
|
+
# responding to the same methods) instead of building one.
|
|
24
|
+
def initialize(access_token = nil, request: nil)
|
|
25
|
+
@request = Request.for(access_token, request)
|
|
11
26
|
end
|
|
12
27
|
|
|
13
|
-
def trophy_titles(user_id: 'me', limit:
|
|
14
|
-
|
|
15
|
-
|
|
28
|
+
def trophy_titles(user_id: 'me', limit: TROPHY_TITLES_MAX_LIMIT, offset: 0)
|
|
29
|
+
validate_page_param!('limit', limit, min: 1, max: TROPHY_TITLES_MAX_LIMIT)
|
|
30
|
+
validate_page_param!('offset', offset, min: 0)
|
|
31
|
+
|
|
32
|
+
@request.get("/trophy/v1/users/#{user(user_id)}/trophyTitles", params: { 'limit' => limit, 'offset' => offset })
|
|
16
33
|
end
|
|
17
34
|
|
|
18
35
|
def trophy_summary(user_id: 'me')
|
|
19
|
-
path = "/trophy/v1/users/#{user_id}/trophySummary"
|
|
36
|
+
path = "/trophy/v1/users/#{user(user_id)}/trophySummary"
|
|
20
37
|
@request.get(path)
|
|
21
38
|
end
|
|
22
39
|
|
|
23
40
|
def title_trophy_groups(np_communication_id:, np_service_name: nil, platform: nil)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
path = "/trophy/v1/npCommunicationIds/#{np_communication_id}/trophyGroups?#{query_string}"
|
|
27
|
-
@request.get(path)
|
|
41
|
+
path = "/trophy/v1/npCommunicationIds/#{title(np_communication_id)}/trophyGroups"
|
|
42
|
+
@request.get(path, params: title_params(np_service_name, platform))
|
|
28
43
|
end
|
|
29
44
|
|
|
30
45
|
def earned_trophy_groups(np_communication_id:, user_id: 'me', np_service_name: nil, platform: nil)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
path = "/trophy/v1/users/#{user_id}/npCommunicationIds/#{np_communication_id}/trophyGroups?#{query_string}"
|
|
34
|
-
@request.get(path)
|
|
46
|
+
path = "/trophy/v1/users/#{user(user_id)}/npCommunicationIds/#{title(np_communication_id)}/trophyGroups"
|
|
47
|
+
@request.get(path, params: title_params(np_service_name, platform))
|
|
35
48
|
end
|
|
36
49
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
)
|
|
43
|
-
|
|
44
|
-
path = "/trophy/v1/npCommunicationIds/#{np_communication_id}/trophyGroups/#{trophy_group_id}/trophies"
|
|
45
|
-
path += "?#{query_string}"
|
|
46
|
-
@request.get(path)
|
|
50
|
+
# `limit:` and `offset:` are optional; without a limit the API returns every trophy.
|
|
51
|
+
def title_trophies(np_communication_id:, trophy_group_id: 'all', np_service_name: nil, platform: nil,
|
|
52
|
+
limit: nil, offset: nil)
|
|
53
|
+
path = "/trophy/v1/npCommunicationIds/#{title(np_communication_id)}" \
|
|
54
|
+
"/trophyGroups/#{group(trophy_group_id)}/trophies"
|
|
55
|
+
@request.get(path, params: title_params(np_service_name, platform, limit: limit, offset: offset))
|
|
47
56
|
end
|
|
48
57
|
|
|
49
58
|
def earned_trophies(np_communication_id:, user_id: 'me', trophy_group_id: 'all', np_service_name: nil,
|
|
50
|
-
platform: nil,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
offset: options[:offset]
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
path = "/trophy/v1/users/#{user_id}/npCommunicationIds/#{np_communication_id}" \
|
|
58
|
-
"/trophyGroups/#{trophy_group_id}/trophies"
|
|
59
|
-
path += "?#{query_string}"
|
|
60
|
-
@request.get(path)
|
|
59
|
+
platform: nil, limit: nil, offset: nil)
|
|
60
|
+
path = "/trophy/v1/users/#{user(user_id)}/npCommunicationIds/#{title(np_communication_id)}" \
|
|
61
|
+
"/trophyGroups/#{group(trophy_group_id)}/trophies"
|
|
62
|
+
@request.get(path, params: title_params(np_service_name, platform, limit: limit, offset: offset))
|
|
61
63
|
end
|
|
62
64
|
|
|
63
65
|
private
|
|
64
66
|
|
|
67
|
+
def user(user_id)
|
|
68
|
+
segment('PSN user ID', user_id, USER_ID_PATTERN)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def title(np_communication_id)
|
|
72
|
+
segment('NP communication ID', np_communication_id, NP_COMMUNICATION_ID_PATTERN)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def group(trophy_group_id)
|
|
76
|
+
segment('trophy group ID', trophy_group_id, TROPHY_GROUP_ID_PATTERN)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Returns the value for interpolation into a path once it matches its pattern.
|
|
80
|
+
def segment(name, value, pattern)
|
|
81
|
+
return value if value.to_s.match?(pattern)
|
|
82
|
+
|
|
83
|
+
raise ArgumentError, "invalid #{name}: #{value.inspect}"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Query parameters for the title-scoped endpoints; anything left nil is omitted.
|
|
87
|
+
def title_params(service_name, platform, limit: nil, offset: nil)
|
|
88
|
+
validate_page_param!('limit', limit, min: 1) unless limit.nil?
|
|
89
|
+
validate_page_param!('offset', offset, min: 0) unless offset.nil?
|
|
90
|
+
|
|
91
|
+
service_name = resolve_service_name(service_name, platform)
|
|
92
|
+
{ 'npServiceName' => service_name, 'limit' => limit, 'offset' => offset }.compact
|
|
93
|
+
end
|
|
94
|
+
|
|
65
95
|
def resolve_service_name(service_name, platform)
|
|
66
96
|
raise ArgumentError, 'Provide either np_service_name or platform, not both' if service_name && platform
|
|
67
97
|
|
|
@@ -73,10 +103,6 @@ module PSN
|
|
|
73
103
|
'trophy2'
|
|
74
104
|
end
|
|
75
105
|
end
|
|
76
|
-
|
|
77
|
-
def build_query_string(**query_params)
|
|
78
|
-
query_params.compact.map { |key, value| "#{key}=#{value}" }.join('&')
|
|
79
|
-
end
|
|
80
106
|
end
|
|
81
107
|
end
|
|
82
108
|
end
|
data/lib/psn/client/users.rb
CHANGED
|
@@ -32,8 +32,10 @@ module PSN
|
|
|
32
32
|
USER_ID_PATTERN = /\A(me|\d+)\z/
|
|
33
33
|
ONLINE_ID_PATTERN = /\A[a-zA-Z0-9_-]{3,16}\z/
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
# Pass an access token, or `request:` to inject a Request (or any object
|
|
36
|
+
# responding to the same methods) instead of building one.
|
|
37
|
+
def initialize(access_token = nil, request: nil)
|
|
38
|
+
@request = Request.for(access_token, request)
|
|
37
39
|
end
|
|
38
40
|
|
|
39
41
|
def search(term)
|
|
@@ -57,7 +59,7 @@ module PSN
|
|
|
57
59
|
|
|
58
60
|
response = @request.get(format(PROFILE2_PATH, online_id), host: :community,
|
|
59
61
|
params: { 'fields' => PROFILE2_FIELDS })
|
|
60
|
-
response
|
|
62
|
+
response&.dig('profile')
|
|
61
63
|
end
|
|
62
64
|
|
|
63
65
|
def profile_by_account_id(account_id:)
|
data/lib/psn/client/version.rb
CHANGED
data/lib/psn/client.rb
CHANGED
|
@@ -2,18 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative 'client/account'
|
|
4
4
|
require_relative 'client/auth'
|
|
5
|
+
require_relative 'client/errors'
|
|
5
6
|
require_relative 'client/pagination'
|
|
6
7
|
require_relative 'client/request'
|
|
8
|
+
require_relative 'client/session'
|
|
9
|
+
require_relative 'client/tokens'
|
|
7
10
|
require_relative 'client/trophies'
|
|
8
11
|
require_relative 'client/users'
|
|
9
12
|
require_relative 'client/version'
|
|
10
13
|
require_relative 'logger'
|
|
11
14
|
require 'net/http'
|
|
12
|
-
|
|
13
|
-
module PSN
|
|
14
|
-
module Client
|
|
15
|
-
class Error < StandardError; end
|
|
16
|
-
class PrivacyError < Error; end
|
|
17
|
-
# Your code goes here...
|
|
18
|
-
end
|
|
19
|
-
end
|
data/sig/psn/client/account.rbs
CHANGED
|
@@ -18,7 +18,7 @@ module PSN
|
|
|
18
18
|
|
|
19
19
|
@request: Request
|
|
20
20
|
|
|
21
|
-
def initialize: (String access_token) -> void
|
|
21
|
+
def initialize: (?String? access_token, ?request: Request?) -> void
|
|
22
22
|
def devices: () -> untyped
|
|
23
23
|
def library: (?limit: Integer) -> untyped
|
|
24
24
|
def purchased_games: (?size: Integer, ?start: Integer) -> untyped
|
data/sig/psn/client/auth.rbs
CHANGED
|
@@ -4,24 +4,44 @@ module PSN
|
|
|
4
4
|
AUTH_URL: String
|
|
5
5
|
DEFAULT_CLIENT_ID: String
|
|
6
6
|
DEFAULT_BASIC_TOKEN: String
|
|
7
|
-
|
|
7
|
+
REDIRECT_URI: String
|
|
8
|
+
SCOPE: String
|
|
9
|
+
TIMEOUT_OPTIONS: Array[Symbol]
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
@npsso: String?
|
|
12
|
+
@client_id: String?
|
|
13
|
+
@basic_token: String?
|
|
14
|
+
@timeouts: Hash[Symbol, Numeric]
|
|
15
|
+
|
|
16
|
+
def self.authenticate: (?npsso: String?, ?client_id: String?, ?basic_token: String?, ?open_timeout: Numeric?, ?read_timeout: Numeric?, ?write_timeout: Numeric?) -> String
|
|
17
|
+
def self.tokens: (?npsso: String?, ?client_id: String?, ?basic_token: String?, ?open_timeout: Numeric?, ?read_timeout: Numeric?, ?write_timeout: Numeric?) -> Tokens
|
|
18
|
+
def self.refresh: (String refresh_token, ?npsso: String?, ?client_id: String?, ?basic_token: String?, ?open_timeout: Numeric?, ?read_timeout: Numeric?, ?write_timeout: Numeric?) -> Tokens
|
|
19
|
+
def initialize: (?npsso: String?, ?client_id: String?, ?basic_token: String?, ?open_timeout: Numeric?, ?read_timeout: Numeric?, ?write_timeout: Numeric?) -> void
|
|
10
20
|
def authenticate: () -> String
|
|
21
|
+
def tokens: () -> Tokens
|
|
22
|
+
def refresh: (String refresh_token) -> Tokens
|
|
11
23
|
|
|
12
24
|
private
|
|
13
25
|
|
|
14
26
|
def fetch_auth_code: () -> String
|
|
27
|
+
def request_auth_code: (URI::Generic) -> Net::HTTPResponse
|
|
15
28
|
def http_for: (URI) -> Net::HTTP
|
|
16
29
|
def request_for: (URI) -> Net::HTTP::Get
|
|
17
30
|
def fetch_token: (String) -> OAuth2::AccessToken
|
|
18
|
-
def
|
|
19
|
-
def
|
|
20
|
-
def
|
|
31
|
+
def fetch_refreshed_token: (String) -> OAuth2::AccessToken
|
|
32
|
+
def request_token: (String) { () -> OAuth2::AccessToken } -> OAuth2::AccessToken
|
|
33
|
+
def token_error: (StandardError, String) -> Error
|
|
34
|
+
def parse_code: (Net::HTTPResponse) -> String
|
|
35
|
+
def fail_with: (String, Net::HTTPResponse, error_code: String?) -> bot
|
|
36
|
+
def response_error: (String, untyped, error_code: String?) -> Error
|
|
37
|
+
def log_and_raise: (Error) -> bot
|
|
38
|
+
def parse_json: (String?) -> Hash[String, untyped]
|
|
21
39
|
def get_error_message: (String?, ?String) -> String
|
|
22
40
|
def client: () -> OAuth2::Client
|
|
23
41
|
def auth_url: () -> String
|
|
24
42
|
def npsso_cookie: () -> String
|
|
43
|
+
def client_id: () -> String
|
|
44
|
+
def basic_token: () -> String
|
|
25
45
|
end
|
|
26
46
|
end
|
|
27
47
|
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
module PSN
|
|
2
|
+
module Client
|
|
3
|
+
class Error < StandardError
|
|
4
|
+
attr_reader status: Integer?
|
|
5
|
+
attr_reader headers: Hash[String, String]
|
|
6
|
+
attr_reader body: String?
|
|
7
|
+
|
|
8
|
+
def self.details_from: (untyped response) -> Hash[Symbol, untyped]
|
|
9
|
+
|
|
10
|
+
def initialize: (?String? message, ?status: Integer?, ?headers: Hash[String, String], ?body: String?) -> void
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class AuthenticationError < Error
|
|
14
|
+
attr_reader error_code: String?
|
|
15
|
+
|
|
16
|
+
def initialize: (?String? message, ?error_code: String?, ?status: Integer?, ?headers: Hash[String, String], ?body: String?) -> void
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
class PrivacyError < Error
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class RateLimitError < Error
|
|
23
|
+
attr_reader retry_at: Time?
|
|
24
|
+
|
|
25
|
+
def initialize: (?String? message, ?status: Integer?, ?headers: Hash[String, String], ?body: String?) -> void
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def parse_retry_at: (Time now) -> Time?
|
|
30
|
+
def next_available_time: (String? value) -> Time?
|
|
31
|
+
def retry_after_time: (String? value, Time now) -> Time?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class GraphQLError < Error
|
|
35
|
+
attr_reader errors: Array[untyped]
|
|
36
|
+
attr_reader data: untyped
|
|
37
|
+
|
|
38
|
+
def initialize: (?String? message, ?errors: Array[untyped], ?data: untyped, ?status: Integer?, ?headers: Hash[String, String], ?body: String?) -> void
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
class TransportError < Error
|
|
42
|
+
NETWORK_ERRORS: Array[Class]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
class ParseError < Error
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|