Skip to content

Commit

Permalink
Fix GOG token refresh when there's no internet connection
Browse files Browse the repository at this point in the history
Former-commit-id: 40d7849
  • Loading branch information
tkashkin committed Sep 29, 2018
1 parent fbd99e9 commit cc7dfa7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
17 changes: 14 additions & 3 deletions src/data/sources/gog/GOG.vala
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,15 @@ namespace GameHub.Data.Sources.GOG
debug("[Auth] Refreshing GOG access token with refresh token: %s", user_refresh_token);

var url = @"https://auth.gog.com/token?client_id=$(CLIENT_ID)&client_secret=$(CLIENT_SECRET)&grant_type=refresh_token&refresh_token=$(user_refresh_token)";
var root = (yield Parser.parse_remote_json_file_async(url)).get_object();
var root_node = yield Parser.parse_remote_json_file_async(url);
var root = root_node != null && root_node.get_node_type() == Json.NodeType.OBJECT ? root_node.get_object() : null;

if(root == null)
{
token_needs_refresh = false;
return false;
}

user_token = root.get_string_member("access_token");
user_refresh_token = root.get_string_member("refresh_token");
user_id = root.get_string_member("user_id");
Expand All @@ -160,7 +168,7 @@ namespace GameHub.Data.Sources.GOG

public override async ArrayList<Game> load_games(Utils.FutureResult2<Game, bool>? game_loaded=null, Utils.Future? cache_loaded=null)
{
if(user_id == null || user_token == null || _games.size > 0)
if(((user_id == null || user_token == null) && token_needs_refresh) || _games.size > 0)
{
return _games;
}
Expand Down Expand Up @@ -199,7 +207,10 @@ namespace GameHub.Data.Sources.GOG
while(page <= pages)
{
var url = @"https://embed.gog.com/account/getFilteredProducts?mediaType=1&page=$(page)";
var root = Parser.parse_remote_json_file(url, "GET", user_token).get_object();
var root_node = Parser.parse_remote_json_file(url, "GET", user_token);
var root = root_node != null && root_node.get_node_type() == Json.NodeType.OBJECT ? root_node.get_object() : null;

if(root == null) break;

page = (int) root.get_int_member("page");
pages = (int) root.get_int_member("totalPages");
Expand Down
12 changes: 9 additions & 3 deletions src/utils/Parser.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace GameHub.Utils
{
public class Parser
{
private static Session? session = null;

public static string load_file(string path, string file="")
{
var f = FSUtils.file(path, file);
Expand All @@ -22,8 +24,14 @@ namespace GameHub.Utils
return data;
}

private static Message prepare_message(string url, string method="GET", string? auth=null, HashMap<string, string>? headers=null, HashMap<string, string>? data=null)
private static Message? prepare_message(string url, string method="GET", string? auth=null, HashMap<string, string>? headers=null, HashMap<string, string>? data=null)
{
if(session == null)
{
session = new Session();
session.timeout = 5;
}

var message = new Message(method, url);

if(auth != null)
Expand Down Expand Up @@ -54,7 +62,6 @@ namespace GameHub.Utils

public static string load_remote_file(string url, string method="GET", string? auth=null, HashMap<string, string>? headers=null, HashMap<string, string>? data=null)
{
var session = new Session();
var message = prepare_message(url, method, auth, headers, data);

var status = session.send_message(message);
Expand All @@ -65,7 +72,6 @@ namespace GameHub.Utils
public static async string load_remote_file_async(string url, string method="GET", string? auth=null, HashMap<string, string>? headers=null, HashMap<string, string>? data=null)
{
var result = "";
var session = new Session();
var message = prepare_message(url, method, auth, headers, data);

session.queue_message(message, (s, m) => {
Expand Down

0 comments on commit cc7dfa7

Please sign in to comment.