All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.playfab.PlayFabAuthenticationAPI Maven / Gradle / Ivy

Go to download

PlayFab is the unified backend platform for games — everything you need to build and operate your game, all in one place, so you can focus on creating and delivering a great player experience.

There is a newer version: 0.122.201027
Show newest version
package com.playfab;

import com.playfab.internal.*;
import com.playfab.PlayFabAuthenticationModels.*;
import com.playfab.PlayFabErrors.*;
import com.playfab.PlayFabSettings;
import java.util.concurrent.*;
import java.util.*;
import com.google.gson.*;
import com.google.gson.reflect.*;

    /**
     * The Authentication APIs provide a convenient way to convert classic authentication responses into entity authentication
     * models. These APIs will provide you with the entity authentication token needed for subsequent Entity API calls. Manage
     * API keys for authenticating any entity.
     */
public class PlayFabAuthenticationAPI {
    private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").create();

    /**
     * Activates the given API key. Active keys may be used for authentication.
     * @param request ActivateAPIKeyRequest
     * @return Async Task will return ActivateAPIKeyResponse
     */
    @SuppressWarnings("unchecked")
    public static FutureTask> ActivateKeyAsync(final ActivateAPIKeyRequest request) {
        return new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateActivateKeyAsync(request);
            }
        });
    }

    /**
     * Activates the given API key. Active keys may be used for authentication.
     * @param request ActivateAPIKeyRequest
     * @return ActivateAPIKeyResponse
     */
    @SuppressWarnings("unchecked")
    public static PlayFabResult ActivateKey(final ActivateAPIKeyRequest request) {
        FutureTask> task = new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateActivateKeyAsync(request);
            }
        });
        try {
            task.run();
            return task.get();
        } catch(Exception e) {
            return null;
        }
    }

    /** Activates the given API key. Active keys may be used for authentication. */
    @SuppressWarnings("unchecked")
    private static PlayFabResult privateActivateKeyAsync(final ActivateAPIKeyRequest request) throws Exception {
        if (PlayFabSettings.EntityToken == null) throw new Exception ("Must call GetEntityToken before you can use the Entity API");

        FutureTask task = PlayFabHTTP.doPost(PlayFabSettings.GetURL("/APIKey/ActivateKey"), request, "X-EntityToken", PlayFabSettings.EntityToken);
        task.run();
        Object httpResult = task.get();
        if (httpResult instanceof PlayFabError) {
            PlayFabError error = (PlayFabError)httpResult;
            if (PlayFabSettings.GlobalErrorHandler != null)
                PlayFabSettings.GlobalErrorHandler.callback(error);
            PlayFabResult result = new PlayFabResult();
            result.Error = error;
            return result;
        }
        String resultRawJson = (String) httpResult;

        PlayFabJsonSuccess resultData = gson.fromJson(resultRawJson, new TypeToken>(){}.getType());
        ActivateAPIKeyResponse result = resultData.data;

        PlayFabResult pfResult = new PlayFabResult();
        pfResult.Result = result;
        return pfResult;
    }

    /**
     * Creates an API key for the given entity.
     * @param request CreateAPIKeyRequest
     * @return Async Task will return CreateAPIKeyResponse
     */
    @SuppressWarnings("unchecked")
    public static FutureTask> CreateKeyAsync(final CreateAPIKeyRequest request) {
        return new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateCreateKeyAsync(request);
            }
        });
    }

    /**
     * Creates an API key for the given entity.
     * @param request CreateAPIKeyRequest
     * @return CreateAPIKeyResponse
     */
    @SuppressWarnings("unchecked")
    public static PlayFabResult CreateKey(final CreateAPIKeyRequest request) {
        FutureTask> task = new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateCreateKeyAsync(request);
            }
        });
        try {
            task.run();
            return task.get();
        } catch(Exception e) {
            return null;
        }
    }

    /** Creates an API key for the given entity. */
    @SuppressWarnings("unchecked")
    private static PlayFabResult privateCreateKeyAsync(final CreateAPIKeyRequest request) throws Exception {
        if (PlayFabSettings.EntityToken == null) throw new Exception ("Must call GetEntityToken before you can use the Entity API");

        FutureTask task = PlayFabHTTP.doPost(PlayFabSettings.GetURL("/APIKey/CreateKey"), request, "X-EntityToken", PlayFabSettings.EntityToken);
        task.run();
        Object httpResult = task.get();
        if (httpResult instanceof PlayFabError) {
            PlayFabError error = (PlayFabError)httpResult;
            if (PlayFabSettings.GlobalErrorHandler != null)
                PlayFabSettings.GlobalErrorHandler.callback(error);
            PlayFabResult result = new PlayFabResult();
            result.Error = error;
            return result;
        }
        String resultRawJson = (String) httpResult;

        PlayFabJsonSuccess resultData = gson.fromJson(resultRawJson, new TypeToken>(){}.getType());
        CreateAPIKeyResponse result = resultData.data;

        PlayFabResult pfResult = new PlayFabResult();
        pfResult.Result = result;
        return pfResult;
    }

    /**
     * Deactivates the given API key, causing subsequent authentication attempts with it to fail.Deactivating a key is a way to
     * verify that the key is not in use before deleting it.
     * @param request DeactivateAPIKeyRequest
     * @return Async Task will return DeactivateAPIKeyResponse
     */
    @SuppressWarnings("unchecked")
    public static FutureTask> DeactivateKeyAsync(final DeactivateAPIKeyRequest request) {
        return new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateDeactivateKeyAsync(request);
            }
        });
    }

    /**
     * Deactivates the given API key, causing subsequent authentication attempts with it to fail.Deactivating a key is a way to
     * verify that the key is not in use before deleting it.
     * @param request DeactivateAPIKeyRequest
     * @return DeactivateAPIKeyResponse
     */
    @SuppressWarnings("unchecked")
    public static PlayFabResult DeactivateKey(final DeactivateAPIKeyRequest request) {
        FutureTask> task = new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateDeactivateKeyAsync(request);
            }
        });
        try {
            task.run();
            return task.get();
        } catch(Exception e) {
            return null;
        }
    }

    /**
     * Deactivates the given API key, causing subsequent authentication attempts with it to fail.Deactivating a key is a way to
     * verify that the key is not in use before deleting it.
     */
    @SuppressWarnings("unchecked")
    private static PlayFabResult privateDeactivateKeyAsync(final DeactivateAPIKeyRequest request) throws Exception {
        if (PlayFabSettings.EntityToken == null) throw new Exception ("Must call GetEntityToken before you can use the Entity API");

        FutureTask task = PlayFabHTTP.doPost(PlayFabSettings.GetURL("/APIKey/DeactivateKey"), request, "X-EntityToken", PlayFabSettings.EntityToken);
        task.run();
        Object httpResult = task.get();
        if (httpResult instanceof PlayFabError) {
            PlayFabError error = (PlayFabError)httpResult;
            if (PlayFabSettings.GlobalErrorHandler != null)
                PlayFabSettings.GlobalErrorHandler.callback(error);
            PlayFabResult result = new PlayFabResult();
            result.Error = error;
            return result;
        }
        String resultRawJson = (String) httpResult;

        PlayFabJsonSuccess resultData = gson.fromJson(resultRawJson, new TypeToken>(){}.getType());
        DeactivateAPIKeyResponse result = resultData.data;

        PlayFabResult pfResult = new PlayFabResult();
        pfResult.Result = result;
        return pfResult;
    }

    /**
     * Deletes the given API key.
     * @param request DeleteAPIKeyRequest
     * @return Async Task will return DeleteAPIKeyResponse
     */
    @SuppressWarnings("unchecked")
    public static FutureTask> DeleteKeyAsync(final DeleteAPIKeyRequest request) {
        return new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateDeleteKeyAsync(request);
            }
        });
    }

    /**
     * Deletes the given API key.
     * @param request DeleteAPIKeyRequest
     * @return DeleteAPIKeyResponse
     */
    @SuppressWarnings("unchecked")
    public static PlayFabResult DeleteKey(final DeleteAPIKeyRequest request) {
        FutureTask> task = new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateDeleteKeyAsync(request);
            }
        });
        try {
            task.run();
            return task.get();
        } catch(Exception e) {
            return null;
        }
    }

    /** Deletes the given API key. */
    @SuppressWarnings("unchecked")
    private static PlayFabResult privateDeleteKeyAsync(final DeleteAPIKeyRequest request) throws Exception {
        if (PlayFabSettings.EntityToken == null) throw new Exception ("Must call GetEntityToken before you can use the Entity API");

        FutureTask task = PlayFabHTTP.doPost(PlayFabSettings.GetURL("/APIKey/DeleteKey"), request, "X-EntityToken", PlayFabSettings.EntityToken);
        task.run();
        Object httpResult = task.get();
        if (httpResult instanceof PlayFabError) {
            PlayFabError error = (PlayFabError)httpResult;
            if (PlayFabSettings.GlobalErrorHandler != null)
                PlayFabSettings.GlobalErrorHandler.callback(error);
            PlayFabResult result = new PlayFabResult();
            result.Error = error;
            return result;
        }
        String resultRawJson = (String) httpResult;

        PlayFabJsonSuccess resultData = gson.fromJson(resultRawJson, new TypeToken>(){}.getType());
        DeleteAPIKeyResponse result = resultData.data;

        PlayFabResult pfResult = new PlayFabResult();
        pfResult.Result = result;
        return pfResult;
    }

    /**
     * Method to exchange a legacy AuthenticationTicket or title SecretKey for an Entity Token or to refresh a still valid
     * Entity Token.
     * @param request GetEntityTokenRequest
     * @return Async Task will return GetEntityTokenResponse
     */
    @SuppressWarnings("unchecked")
    public static FutureTask> GetEntityTokenAsync(final GetEntityTokenRequest request) {
        return new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateGetEntityTokenAsync(request);
            }
        });
    }

    /**
     * Method to exchange a legacy AuthenticationTicket or title SecretKey for an Entity Token or to refresh a still valid
     * Entity Token.
     * @param request GetEntityTokenRequest
     * @return GetEntityTokenResponse
     */
    @SuppressWarnings("unchecked")
    public static PlayFabResult GetEntityToken(final GetEntityTokenRequest request) {
        FutureTask> task = new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateGetEntityTokenAsync(request);
            }
        });
        try {
            task.run();
            return task.get();
        } catch(Exception e) {
            return null;
        }
    }

    /**
     * Method to exchange a legacy AuthenticationTicket or title SecretKey for an Entity Token or to refresh a still valid
     * Entity Token.
     */
    @SuppressWarnings("unchecked")
    private static PlayFabResult privateGetEntityTokenAsync(final GetEntityTokenRequest request) throws Exception {
        String authKey = null, authValue = null;
        if (PlayFabSettings.EntityToken != null) { authKey = "X-EntityToken"; authValue = PlayFabSettings.EntityToken; }
        else if (PlayFabSettings.ClientSessionTicket != null) { authKey = "X-Authorization"; authValue = PlayFabSettings.ClientSessionTicket; }
        else if (PlayFabSettings.DeveloperSecretKey != null) { authKey = "X-SecretKey"; authValue = PlayFabSettings.DeveloperSecretKey; }

        FutureTask task = PlayFabHTTP.doPost(PlayFabSettings.GetURL("/Authentication/GetEntityToken"), request, authKey, authValue);
        task.run();
        Object httpResult = task.get();
        if (httpResult instanceof PlayFabError) {
            PlayFabError error = (PlayFabError)httpResult;
            if (PlayFabSettings.GlobalErrorHandler != null)
                PlayFabSettings.GlobalErrorHandler.callback(error);
            PlayFabResult result = new PlayFabResult();
            result.Error = error;
            return result;
        }
        String resultRawJson = (String) httpResult;

        PlayFabJsonSuccess resultData = gson.fromJson(resultRawJson, new TypeToken>(){}.getType());
        GetEntityTokenResponse result = resultData.data;
        PlayFabSettings.EntityToken = result.EntityToken != null ? result.EntityToken : PlayFabSettings.EntityToken;

        PlayFabResult pfResult = new PlayFabResult();
        pfResult.Result = result;
        return pfResult;
    }

    /**
     * Gets the API keys associated with the given entity.
     * @param request GetAPIKeysRequest
     * @return Async Task will return GetAPIKeysResponse
     */
    @SuppressWarnings("unchecked")
    public static FutureTask> GetKeysAsync(final GetAPIKeysRequest request) {
        return new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateGetKeysAsync(request);
            }
        });
    }

    /**
     * Gets the API keys associated with the given entity.
     * @param request GetAPIKeysRequest
     * @return GetAPIKeysResponse
     */
    @SuppressWarnings("unchecked")
    public static PlayFabResult GetKeys(final GetAPIKeysRequest request) {
        FutureTask> task = new FutureTask(new Callable>() {
            public PlayFabResult call() throws Exception {
                return privateGetKeysAsync(request);
            }
        });
        try {
            task.run();
            return task.get();
        } catch(Exception e) {
            return null;
        }
    }

    /** Gets the API keys associated with the given entity. */
    @SuppressWarnings("unchecked")
    private static PlayFabResult privateGetKeysAsync(final GetAPIKeysRequest request) throws Exception {
        if (PlayFabSettings.EntityToken == null) throw new Exception ("Must call GetEntityToken before you can use the Entity API");

        FutureTask task = PlayFabHTTP.doPost(PlayFabSettings.GetURL("/APIKey/GetKeys"), request, "X-EntityToken", PlayFabSettings.EntityToken);
        task.run();
        Object httpResult = task.get();
        if (httpResult instanceof PlayFabError) {
            PlayFabError error = (PlayFabError)httpResult;
            if (PlayFabSettings.GlobalErrorHandler != null)
                PlayFabSettings.GlobalErrorHandler.callback(error);
            PlayFabResult result = new PlayFabResult();
            result.Error = error;
            return result;
        }
        String resultRawJson = (String) httpResult;

        PlayFabJsonSuccess resultData = gson.fromJson(resultRawJson, new TypeToken>(){}.getType());
        GetAPIKeysResponse result = resultData.data;

        PlayFabResult pfResult = new PlayFabResult();
        pfResult.Result = result;
        return pfResult;
    }
}