restiny 2.0.0 → 2.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9250ce97e7698687ee6cc64362dc0ff0cfe222115a544f60290dbccbfa4255b
4
- data.tar.gz: a33846e9b1a017a1a357f0ea51aeaa74f71d296d02f61f1a1da68a89bcf90498
3
+ metadata.gz: 2d5b81f9aadd44def19fb9af625b26692992457edaf57eeba97b46896f26e032
4
+ data.tar.gz: f058584408a167934ae67f9e8c160f75e542f80558446ee312fa2ce34b763ace
5
5
  SHA512:
6
- metadata.gz: 06d7bfcdd5a4263a4ac1db64ab4c55bf6986378fe5fa1a592616a2d03662f15f3cce91398b40a2fe3a03e2950c9eb8005bc64202980d1c4d41c4be54c82014d7
7
- data.tar.gz: 7ec6297618ebaa28d3ef6f1ba5b96761a7beb04911201c2f80dd63aa671b48a072d0df9d4e26525ec962379245cbed3ff2d404b8d78efc541233eb0d9d8e12a4
6
+ metadata.gz: 5b737a79ec4728c34106b701db09f654c9d642cfd62b3ff311501a70afcf954f07ad92f0d1282c9a59686bffef568ef2b1fb5466e97cd267b389c1634c28b742
7
+ data.tar.gz: 99c7c3c4519a6a539a11a2970f18a38443ee4a9587b6df980cb74b03e425f1ce134e48c075af0d5b8d29339e086310eda6c44ac352e0b4daf80997923fded98b
@@ -7,11 +7,10 @@ module Faraday
7
7
 
8
8
  class Api < Middleware
9
9
  def on_complete(env)
10
- return if env["response_body"].empty?
10
+ return if env["response_body"].empty? || !env["response_body"].dig("ErrorCode")
11
11
 
12
- payload = JSON.parse(env["response_body"])
13
- if payload["ErrorCode"] == 1
14
- env[:body] = payload.dig("Response")
12
+ if env["response_body"]["ErrorCode"] == 1
13
+ env[:body] = env["response_body"].dig("Response")
15
14
  return
16
15
  end
17
16
 
@@ -25,9 +24,7 @@ module Faraday
25
24
  ::Restiny::Error
26
25
  end
27
26
 
28
- raise klass.new(payload["Message"], payload["ErrorStatus"])
29
- rescue JSON::ParserError
30
- raise ::Restiny::ResponseError.new("Unable to parse API response")
27
+ raise klass.new(env["response_body"]["Message"], env["response_body"]["ErrorStatus"])
31
28
  end
32
29
  end
33
30
  end
@@ -0,0 +1,21 @@
1
+ require "faraday"
2
+ require "restiny/errors"
3
+
4
+ module Faraday
5
+ module Restiny
6
+ Faraday::Response.register_middleware(destiny_auth: "Faraday::Restiny::Auth")
7
+
8
+ class Auth < Middleware
9
+ def on_complete(env)
10
+ return if env["response_body"].empty? || env["url"].to_s !~ /oauth/
11
+
12
+ if env["response_body"]["error"]
13
+ raise ::Restiny::AuthenticationError.new(
14
+ env["response_body"]["error_description"],
15
+ env["response_body"]["error"]
16
+ )
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Restiny
4
- VERSION = "2.0.0"
4
+ VERSION = "2.0.1"
5
5
  end
data/lib/restiny.rb CHANGED
@@ -10,6 +10,7 @@ require "restiny/manifest"
10
10
  require "faraday"
11
11
  require "faraday/follow_redirects"
12
12
  require "faraday/destiny/api"
13
+ require "faraday/destiny/auth"
13
14
  require "securerandom"
14
15
 
15
16
  module Restiny
@@ -30,7 +31,7 @@ module Restiny
30
31
  params = { response_type: "code", client_id: @oauth_client_id, state: @oauth_state }
31
32
  params[:redirect_url] = redirect_url unless redirect_url.nil?
32
33
 
33
- connection.build_url(BUNGIE_URL + "/en/oauth/authorize", params).to_s
34
+ auth_connection.build_url(BUNGIE_URL + "/en/oauth/authorize/", params).to_s
34
35
  end
35
36
 
36
37
  def request_access_token(code:, redirect_url: nil)
@@ -39,17 +40,13 @@ module Restiny
39
40
  params = { code: code, grant_type: "authorization_code", client_id: @oauth_client_id }
40
41
  params[:redirect_url] = redirect_url unless redirect_url.nil?
41
42
 
42
- connection.post(
43
- "app/oauth/token",
44
- params,
45
- "Content-Type" => "application/x-www-form-urlencoded"
46
- ).body
43
+ auth_post("app/oauth/token/", params)
47
44
  end
48
45
 
49
46
  # Manifest methods
50
47
 
51
48
  def get_manifest_url(locale: "en")
52
- result = connection.get("Destiny2/Manifest/").body.dig("mobileWorldContentPaths", locale)
49
+ result = api_get("Destiny2/Manifest/").dig("mobileWorldContentPaths", locale)
53
50
  BUNGIE_URL + result
54
51
  end
55
52
 
@@ -71,7 +68,7 @@ module Restiny
71
68
  url += type_url if type_url
72
69
  url += "?components=#{components.join(",")}"
73
70
 
74
- connection.get(url).body
71
+ api_get(url)
75
72
  end
76
73
 
77
74
  def get_character_profile(character_id:, membership_id:, membership_type:, components:)
@@ -79,7 +76,7 @@ module Restiny
79
76
  membership_id: membership_id,
80
77
  membership_type: membership_type,
81
78
  components: components,
82
- type_url: "Character/#{character_id}"
79
+ type_url: "Character/#{character_id}/"
83
80
  )
84
81
  end
85
82
 
@@ -88,7 +85,7 @@ module Restiny
88
85
  membership_id: membership_id,
89
86
  membership_type: membership_type,
90
87
  components: components,
91
- type_url: "Item/#{item_id}"
88
+ type_url: "Item/#{item_id}/"
92
89
  )
93
90
  end
94
91
 
@@ -96,8 +93,7 @@ module Restiny
96
93
 
97
94
  def get_user_memberships_by_id(membership_id, membership_type: Platform::ALL)
98
95
  raise Restiny::InvalidParamsError.new("Please provide a membership ID") if membership_id.nil?
99
-
100
- connection.get("User/GetMembershipsById/#{membership_id}/#{membership_type}/").body
96
+ api_get("User/GetMembershipsById/#{membership_id}/#{membership_type}/")
101
97
  end
102
98
 
103
99
  def search_player_by_bungie_name(name, membership_type: Platform::ALL)
@@ -106,19 +102,33 @@ module Restiny
106
102
  raise Restiny::InvalidParamsError.new("You must provide a valid Bungie name")
107
103
  end
108
104
 
109
- connection.post(
105
+ api_post(
110
106
  "Destiny2/SearchDestinyPlayerByBungieName/#{membership_type}/",
111
- displayName: display_name,
112
- displayNameCode: display_name_code
113
- ).body
107
+ params: {
108
+ displayName: display_name,
109
+ displayNameCode: display_name_code
110
+ }
111
+ )
114
112
  end
115
113
 
116
114
  def search_users_by_global_name(name:, page: 0)
117
- connection.post("User/Search/GlobalName/#{page}", displayNamePrefix: name).body
115
+ api_post("User/Search/GlobalName/#{page}/", params: { displayNamePrefix: name })
118
116
  end
119
117
 
120
118
  # General request methods
121
119
 
120
+ def api_get(url, params: {})
121
+ api_connection.get(url, params, token_header).body
122
+ end
123
+
124
+ def api_post(url, params: {})
125
+ api_connection.post(url, params, token_header).body
126
+ end
127
+
128
+ def auth_post(url, params)
129
+ auth_connection.post(url, params, "Content-Type" => "application/x-www-form-urlencoded").body
130
+ end
131
+
122
132
  private
123
133
 
124
134
  def check_oauth_client_id
@@ -126,24 +136,37 @@ module Restiny
126
136
  end
127
137
 
128
138
  def default_headers
129
- {
130
- "User-Agent": "restiny v#{Restiny::VERSION}",
131
- "X-API-KEY": @api_key,
132
- "Content-Type": "application/json"
133
- }
139
+ { "User-Agent": "restiny v#{Restiny::VERSION}" }
134
140
  end
135
141
 
136
- def connection
142
+ def api_connection
137
143
  raise Restiny::InvalidParamsError.new("You need to set an API key") unless @api_key
138
144
 
139
- headers = default_headers
140
- headers["authorization"] = "Bearer #{@oauth_token}" if @oauth_token
145
+ @connection ||=
146
+ Faraday.new(
147
+ url: API_BASE_URL,
148
+ headers: default_headers.merge("X-API-KEY": @api_key)
149
+ ) do |faraday|
150
+ faraday.request :url_encoded
151
+ faraday.request :json
152
+ faraday.response :follow_redirects
153
+ faraday.response :destiny_api
154
+ faraday.response :json
155
+ end
156
+ end
157
+
158
+ def auth_connection
159
+ @auth_connection ||=
160
+ Faraday.new(url: API_BASE_URL, headers: default_headers) do |faraday|
161
+ faraday.request :url_encoded
162
+ faraday.request :json
163
+ faraday.response :follow_redirects
164
+ faraday.response :destiny_auth
165
+ faraday.response :json
166
+ end
167
+ end
141
168
 
142
- Faraday.new(url: API_BASE_URL, headers: headers) do |faraday|
143
- faraday.request :url_encoded
144
- faraday.request :json
145
- faraday.response :follow_redirects
146
- faraday.response :destiny_api
147
- end
169
+ def token_header
170
+ {}.tap { |headers| headers["authorization"] = "Bearer #{@oauth_token}" if @oauth_token }
148
171
  end
149
172
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restiny
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bogan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-30 00:00:00.000000000 Z
11
+ date: 2023-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -158,6 +158,7 @@ extensions: []
158
158
  extra_rdoc_files: []
159
159
  files:
160
160
  - lib/faraday/destiny/api.rb
161
+ - lib/faraday/destiny/auth.rb
161
162
  - lib/restiny.rb
162
163
  - lib/restiny/constants.rb
163
164
  - lib/restiny/errors.rb