Esempio n. 1
0
        /// <summary>
        /// This method creates a new short-living token to be used by client side with the options specified.
        /// </summary>
        /// <param name="opts"> Token options.</param>
        /// <returns>A <c>GetTokenResponse</c> instance that contains the new token created.</returns>
        /// <exception cref="AuthException">Thrown when SETHEALTH_KEY or SETHEALTH_SECRET
        /// is/are wrong and the authentification fails.</exception>

        public async Task <GetTokenResponse> GetToken(GetTokenOptions opts)
        {
            try
            {
                var json = JsonConvert.SerializeObject(new GetTokenRequestOptions
                {
                    id        = this.key,
                    secret    = this.secret,
                    expiresIn = opts.expiresIn,
                    testMode  = opts.testMode,
                    userId    = opts.userId
                });
                var body   = new StringContent(json, Encoding.UTF8, "application/json");
                var client = new HttpClient();

                var response = await client.PostAsync(HOST + "/token", body);

                string result = response.Content.ReadAsStringAsync().Result;
                // if (!response.IsSuccessStatusCode)
                // {
                //     throw new AuthException("Bad auth");
                // }
                var tokenResponse = JsonConvert.DeserializeObject <GetTokenResponse>(result);
                if (tokenResponse.token == null)
                {
                    throw new AuthException("Bad response");
                }
                return(tokenResponse);
            }
            catch (Exception e)
            {
                throw new AuthException("Bad request", e);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// This method creates a new short-living token to be used by client side.
        /// </summary>
        /// <returns>A <c>GetTokenResponse</c> instance that contains the new token created.</returns>
        /// <exception cref="AuthException">Thrown when SETHEALTH_KEY or SETHEALTH_SECRET
        /// is/are wrong and the authentification fails.</exception>

        public async Task <GetTokenResponse> GetToken()
        {
            GetTokenOptions opts = new GetTokenOptions("", 0, false);

            return(await GetToken(opts));
        }