Example #1
0
        /// <summary>
        /// Asynchronously throws the baasic client exception.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <param name="response">The response.</param>
        /// <returns></returns>
        protected async Task ThrowExceptionAsync(object content, HttpResponseMessage response)
        {
            string requestInfo = "";

            if (content != null)
            {
                if (content.GetType().GetTypeInfo().IsValueType)
                {
                    requestInfo = content.ToString();
                }
                else
                {
                    requestInfo = JsonConvert.SerializeObject(content);
                }
            }
            BaasicErrorResponse responseObject = null;

            try
            {
                responseObject = await ReadContentAsync <BaasicErrorResponse>(response);
            }
            catch { }

            throw new BaasicClientException((int)response.StatusCode, $"{response.ReasonPhrase}. Request:\"{requestInfo}\" Response:\"{await response.Content.ReadAsStringAsync()}\"", responseObject?.Error, responseObject?.ErrorCode ?? 0);
        }
Example #2
0
        /// <summary>
        /// Creates the asynchronous.
        /// </summary>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="tokenOptions">The token options.</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public virtual async Task <IAuthenticationToken> CreateAsync(string username, string password, TokenOptions tokenOptions = null)
        {
            using (var client = BaasicPlatformClientFactory.Create(Configuration))
            {
                if (tokenOptions == null)
                {
                    tokenOptions = new TokenOptions();
                }

                var request = new HttpRequestMessage(HttpMethod.Post, client.GetApiUrl(string.Format("{0}?{1}", this.ModuleRelativePath, tokenOptions.GetOptionsCSV())))
                {
                    Content = new FormUrlEncodedContent(new KeyValuePair <string, string>[] {
                        new KeyValuePair <string, string>("grant_type", "password"),
                        new KeyValuePair <string, string>("username", username),
                        new KeyValuePair <string, string>("password", password)
                    })
                };

                IAuthenticationToken token    = null;
                IAuthenticationToken oldToken = this.Configuration.TokenHandler.Get();
                this.Configuration.TokenHandler.Clear();

                try
                {
                    var response = await client.SendAsync(request);

                    var responseContent = await response.Content.ReadAsStringAsync();

                    BaasicErrorResponse responseObject = null;
                    try
                    {
                        responseObject = await client.ReadContentAsync <BaasicErrorResponse>(response);
                    }
                    catch { }

                    if (response.StatusCode.Equals(HttpStatusCode.OK) ||
                        response.StatusCode.Equals(HttpStatusCode.Created))
                    {
                        token = this.ReadToken(JsonFormatter.Deserialize <Newtonsoft.Json.Linq.JObject>(responseContent));
                    }
                    else
                    {
                        throw new BaasicClientException((int)response.StatusCode, "Unable to create new token.", responseObject?.Error, responseObject?.ErrorCode ?? 0, new Exception(responseContent));
                    }
                }
                catch (Exception ex)
                {
                    this.SaveToken(oldToken);
                    throw ex;
                }

                this.SaveToken(token);

                return(token);
            }
        }