<commit_before>package googleapi ;

#if cpp
import cpp.Lib;
#elseif neko
import neko.Lib;
#end

#if (android && openfl)
import openfl.utils.JNI;
#end

using tink.CoreApi;

class GoogleApi 
{
	public static inline var SCOPE_GAMES:String = "https://www.googleapis.com/auth/games";
	
	public static var debugCallback:String->Void;
	public static var ready:Surprise<Bool, Error> = init();
	
	private static var tokenCache:Map<String, Surprise<String, Error>> = new Map();
	
	
	
	public static function getToken(scope:String):Surprise<String, Error>
	{
		if (!tokenCache.exists(scope))
		{
			tokenCache[scope] = ready.flatMap(function(ready)
			{
				try
				{
					ready.sure();
					return Future.async(function(handler)		 
					{
						var tokenHandler = function(s:String) 
						{
							if (s.indexOf("failed") != -1)
								handler(Failure(new Error(s)));
							else
								handler(Success(s));
						};

						
						//tokenHandler = function(s:String) if (debugCallback != null) debugCallback(s);

						#if (android && openfl)
						googleapi_get_token({handler:tokenHandler}, scope);
						#else
						debugCallback("call cpp get_token");
						googleapi_get_token(tokenHandler, scope);
						#end
					});
				}
				catch (e:Error) // ready error (e.g. user does not authorize)
					return Future.sync(Failure(e));
			});
		}
		return tokenCache[scope];
	}
	
	public static function invalidateToken(token:String):Void
	{
#if (android && openfl)
		for (scope in tokenCache.keys())
		{
			tokenCache[scope].handle(function(t) try if (t.sure() == token) invalidateTokenByScope(scope) catch (e:Error) {});
		}
#end
	}
	
	public static function invalidateTokenByScope(scope:String):Void
	{
		if (tokenCache.exists(scope))
		{
			tokenCache[scope].handle(function(t)
			{
				try
				{
					var token = t.sure();
					googleapi_invalidate_token(token);
				}
				catch (e:Error)
				{
					
				}
				
				tokenCache.remove(scope);
			});
		}
	}
	
	private static function init():Surprise<Bool, Error>
	{
		return Future.async(function(handler)
		{
			var accountNameHandler = function(s:String) /* s is accountname if success*/ handler(s.indexOf("failed") != -1 ? Failure(new Error(s)) : Success(true));
			var debugHandler = function(s:String) if (debugCallback != null) debugCallback(s);

			#if (android && openfl)
			var googleapi_init = JNI.createStaticMethod ("org.haxe.extension.GoogleApi", "init", "(Lorg/haxe/lime/HaxeObject;Lorg/haxe/lime/HaxeObject;)V");			
			googleapi_init({handler:accountNameHandler}, {handler:debugHandler});
			#else
			var googleapi_init = Lib.load("googleapi", "googleapi_init", 2);
			googleapi_init(accountNameHandler, debugHandler);
			#end
		});
	}
	
	
	#if (android && openfl)
	private static var googleapi_get_token = JNI.createStaticMethod ("org.haxe.extension.GoogleApi", "getToken", "(Lorg/haxe/lime/HaxeObject;Ljava/lang/String;)V");
	private static var googleapi_invalidate_token = JNI.createStaticMethod ("org.haxe.extension.GoogleApi", "invalidateToken", "(Ljava/lang/String;)V");
	#else
	private static var googleapi_get_token = Lib.load("googleapi", "googleapi_get_token", 2);
	private static var googleapi_invalidate_token = null;	
	#end
}
<commit_msg>Remove debug stuff<commit_after>package googleapi ;

#if cpp
import cpp.Lib;
#elseif neko
import neko.Lib;
#end

#if (android && openfl)
import openfl.utils.JNI;
#end

using tink.CoreApi;

class GoogleApi 
{
	public static inline var SCOPE_GAMES:String = "https://www.googleapis.com/auth/games";
	
	public static var debugCallback:String->Void;
	public static var ready:Surprise<Bool, Error> = init();
	
	private static var tokenCache:Map<String, Surprise<String, Error>> = new Map();
	
	
	
	public static function getToken(scope:String):Surprise<String, Error>
	{
		if (!tokenCache.exists(scope))
		{
			tokenCache[scope] = ready.flatMap(function(ready)
			{
				try
				{
					ready.sure();
					return Future.async(function(handler)		 
					{
						var tokenHandler = function(s:String) 
						{
							if (s.indexOf("failed") != -1)
								handler(Failure(new Error(s)));
							else
								handler(Success(s));
						};

						#if (android && openfl)
						googleapi_get_token({handler:tokenHandler}, scope);
						#else
						debugCallback("call cpp get_token");
						googleapi_get_token(tokenHandler, scope);
						#end
					});
				}
				catch (e:Error) // ready error (e.g. user does not authorize)
					return Future.sync(Failure(e));
			});
		}
		return tokenCache[scope];
	}
	
	public static function invalidateToken(token:String):Void
	{
#if (android && openfl)
		for (scope in tokenCache.keys())
		{
			tokenCache[scope].handle(function(t) try if (t.sure() == token) invalidateTokenByScope(scope) catch (e:Error) {});
		}
#end
	}
	
	public static function invalidateTokenByScope(scope:String):Void
	{
		if (tokenCache.exists(scope))
		{
			tokenCache[scope].handle(function(t)
			{
				try
				{
					var token = t.sure();
					googleapi_invalidate_token(token);
				}
				catch (e:Error)
				{
					
				}
				
				tokenCache.remove(scope);
			});
		}
	}
	
	private static function init():Surprise<Bool, Error>
	{
		return Future.async(function(handler)
		{
			var accountNameHandler = function(s:String) /* s is accountname if success*/ handler(s.indexOf("failed") != -1 ? Failure(new Error(s)) : Success(true));
			var debugHandler = function(s:String) if (debugCallback != null) debugCallback(s);

			#if (android && openfl)
			var googleapi_init = JNI.createStaticMethod ("org.haxe.extension.GoogleApi", "init", "(Lorg/haxe/lime/HaxeObject;Lorg/haxe/lime/HaxeObject;)V");			
			googleapi_init({handler:accountNameHandler}, {handler:debugHandler});
			#else
			var googleapi_init = Lib.load("googleapi", "googleapi_init", 2);
			googleapi_init(accountNameHandler, debugHandler);
			#end
		});
	}
	
	
	#if (android && openfl)
	private static var googleapi_get_token = JNI.createStaticMethod ("org.haxe.extension.GoogleApi", "getToken", "(Lorg/haxe/lime/HaxeObject;Ljava/lang/String;)V");
	private static var googleapi_invalidate_token = JNI.createStaticMethod ("org.haxe.extension.GoogleApi", "invalidateToken", "(Ljava/lang/String;)V");
	#else
	private static var googleapi_get_token = Lib.load("googleapi", "googleapi_get_token", 2);
	private static var googleapi_invalidate_token = null;	
	#end
}

{'fraction_non_alphanumeric': 0.13854660347551342, 'fraction_numerical': 0.001263823064770932, 'mean_word_length': 3.8662567255956954, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7622064', 'n_tokens_mistral': 2540, 'n_tokens_neox': 2106, 'n_words': 424}