Пример #1
0
        public async Task <bool> ValidateSession(string userId, string deviceId, string sessionId, CancellationToken cancellationToken = default(CancellationToken))
        {
            var url = $"api/users/{userId}/devices/{deviceId}/session/validate";

            var resp = await Client.PutAsJsonAsync(url, sessionId, cancellationToken);

            if (resp.StatusCode == HttpStatusCode.NotFound)
            {
                return(false);
            }

            if (!resp.IsSuccessStatusCode)
            {
                throw ApiClientException.FromResponse(resp);
            }

            var result = await resp.Content.ReadAsAsync <string>();

            if (result != "ok")
            {
                throw new ApiClientException($"Api call failed with an invalid response {result}");
            }

            return(true);
        }
Пример #2
0
        public async Task <IEnumerable <HostedGame> > GetGameList()
        {
            var client = Client;
            var resp   = await client.GetAsync("api/game");

            if (resp.IsSuccessStatusCode)
            {
                return(await resp.Content.ReadAsAsync <IEnumerable <HostedGame> >());;
            }
            throw ApiClientException.FromResponse(resp);
        }
Пример #3
0
        public async Task SetGameList(string apiKey, IEnumerable <HostedGame> games)
        {
            var gms    = games.Sanitized().ToArray();
            var client = Client;
            var resp   = await client.PutAsJsonAsync("api/game?apiKey=" + apiKey, gms);

            if (!resp.IsSuccessStatusCode)
            {
                throw ApiClientException.FromResponse(resp);
            }
        }
Пример #4
0
        public async Task ClearSession(string userId, string deviceId, string sessionId)
        {
            var url = $"api/users/{userId}/devices/{deviceId}/session";

            var resp = await Client.PostAsJsonAsync(url, sessionId);

            if (!resp.IsSuccessStatusCode)
            {
                throw ApiClientException.FromResponse(resp);
            }
        }
Пример #5
0
        public async Task <IEnumerable <ApiUser> > UsersFromUserIds(IEnumerable <string> userIds, CancellationToken cancellationToken = default(CancellationToken))
        {
            var client = Client;
            var resp   = await client.PostAsJsonAsync("api/user/fromuserids", userIds, cancellationToken);

            if (!resp.IsSuccessStatusCode)
            {
                throw ApiClientException.FromResponse(resp);
            }

            return(await resp.Content.ReadAsAsync <ApiUser[]>());
        }
Пример #6
0
        public async Task <bool> IsGameServerRunning(string username, string password)
        {
            var client = Client;
            var resp   = await
                         client.GetAsync(
                "api/servicestatus/gameserverrunning?username="******"&password="
                + HttpUtility.UrlEncode(password));

            if (!resp.IsSuccessStatusCode)
            {
                throw ApiClientException.FromResponse(resp);
            }

            return(await resp.Content.ReadAsAsync <bool>());
        }
Пример #7
0
        public async Task <CreateSessionResult> CreateSession(string username, string password, string deviceId)
        {
            var client = Client;

            var data = new CreateSession()
            {
                Username = username,
                Password = password,
                DeviceId = deviceId
            };

            var resp = await client.PostAsJsonAsync("api/sessions", data);

            if (!resp.IsSuccessStatusCode)
            {
                throw ApiClientException.FromResponse(resp);
            }

            var result = await resp.Content.ReadAsAsync <CreateSessionResult>();

            return(result);
        }
Пример #8
0
        public static ApiClientException FromResponse(HttpResponseMessage message)
        {
            var ex = new ApiClientException($"Http request failed with code '{(int)message.StatusCode}'");

            return(ex);
        }