Exemplo n.º 1
0
        public async Task CreateSession()
        {
            /* Format the _timestamp */
            _timestamp = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
            /* Update the _lastSession */
            _lastSession = DateTime.UtcNow;

            /* Creating the signature from devID, createsession(), authKey and _timestamp*/
            var signature = SecurityUtilities.GetMD5Hash(_devId + "createsession" + _authKey + _timestamp);
            /* Generating the url we going to HTTP GET */
            var url = _paladinsApiUrl + "createsession" + "json" + "/" + _devId + "/" + signature + "/" + _timestamp;

            /* Intialize the httpclient, HTTP GET the url and deserialize the response */
            var client       = new HttpClient();
            var jsonResponse = await client.GetStringAsync(url);

            _currentSession = JsonConvert.DeserializeObject <Session>(jsonResponse);

            /* If session cannot be established, throw an exception */
            if (string.IsNullOrEmpty(_currentSession?.session_id))
            {
                throw new Exception("Session could not be created!");
            }
        }
Exemplo n.º 2
0
        public async Task <T> Request <T>(string apiMethod, params dynamic[] parameters)
        {
            /* Check if for any reason session is not established */
            if (string.IsNullOrEmpty(_currentSession?.session_id))
            {
                await CreateSession();
            }

            /* Check if session is dead */
            if (DateTime.UtcNow.Subtract(_lastSession).TotalMinutes >= 15)
            {
                await CreateSession();
            }

            /* Creating the signature from devID, apiMethod, authKey and _timestamp*/
            var signature = SecurityUtilities.GetMD5Hash(_devId + apiMethod + _authKey + _timestamp);

            /* Generating the url we going to HTTP GET */
            var url = _paladinsApiUrl + apiMethod + "json" + "/" + _devId + "/" + signature + "/" + _currentSession.session_id + "/" + _timestamp;

            /* Add every parameter we pushed into the request */
            foreach (var param in parameters)
            {
                string stringParam = "/";

                /* If the parameter is DateTime the format must be yyyyMMdd */
                if (param is DateTime)
                {
                    stringParam += param.ToString("yyyyMMdd");
                }
                /* If its enum, cast to int and then in string */
                else if (param is QueueType || param is eLanguageCode)
                {
                    stringParam += ((int)param).ToString();
                }
                /* If it is every other just convert into string */
                else
                {
                    stringParam += param.ToString();
                }

                /* Add it to url we going to get */
                url += stringParam;
            }

            /* Intialize the httpclient, HTTP GET the url and deserialize the response */
            var client       = new HttpClient();
            var jsonResponse = await client.GetStringAsync(url);

            var result = JsonConvert.DeserializeObject <T>(jsonResponse);

            /* Variables to hold the exception if any */
            var foundProblem = false;
            var errorMessage = string.Empty;

            /* Player Achievements and Patch Info is the only one that does not contain list,
             * so there is no generictypedefinition and we will get an
             * exception */
            if (result is PlayerAchievements || result is PatchInfo)
            {
                var basic = result as PaladinsResponse;
                if (basic.ret_msg != null)
                {
                    foundProblem = true;
                    errorMessage = basic.ret_msg;
                }
            }
            /* If T is a List, then get the first element */
            else if (typeof(T).GetGenericTypeDefinition() == typeof(List <>))
            {
                var list = (IList)result;
                if (list != null && list.Count > 0)
                {
                    var mine = list[0] as PaladinsResponse;
                    if (mine?.ret_msg != null)
                    {
                        foundProblem = true;
                    }
                }
            }
            else
            {
                var basic = result as PaladinsResponse;
                if (basic?.ret_msg != null)
                {
                    foundProblem = true;
                    errorMessage = basic.ret_msg;
                }
            }

            /* If there is no problem in the response,
             * return the values */
            if (!foundProblem)
            {
                return(result);
            }

            /* If there is a problem in the response, try again.
             * It is only check for dead session
             * the recursion. */
            if (errorMessage.Contains("dailylimit"))
            {
                throw new DailyLimitException();
            }
            if (errorMessage.Contains("Exception while validating developer access"))
            {
                throw new WrongCredentialsException();
            }
            if (errorMessage.Contains("404"))
            {
                throw new NotFoundException();
            }

            await CreateSession();

            return(await Request <T>(apiMethod, parameters));
        }