Exemplo n.º 1
0
        /// <summary>
        /// Creates a new Currency for the specified user, given a valid API token
        /// <para/>Only the code of the new currency and its ratio to the referential currency are required properties.
        /// <para/>Default values for other properties:<para/>
        ///     referential:        false,
        ///     position:           last + 1000,
        /// </summary>
        /// <param name="authUser">The e-mail address of the user to whom the account should be added</param>
        /// <param name="authToken">A valid API token (linked to the e-mail of the specified user)</param>
        /// <param name="currencyToCreate">Account object that specifies the properties of the account to be created</param>
        /// <returns>Returns the unique Id of the newly created account</returns>
        public static async Task <string> CreateNewAsync(string authUser, string authToken, WalletCurrency currencyToCreate)
        {
            var postConformObject = new
            {
                code = currencyToCreate.Code,
                ratioToReferential = currencyToCreate.RatioToReferential,
                referential        = currencyToCreate.Referential,
                position           = currencyToCreate.Position
            };

            var urlSpecifier = "currency";
            var contentType  = "application/json";
            var contentJson  = JsonConvert.SerializeObject(postConformObject);
            var content      = new StringContent(contentJson, Encoding.Default, contentType);

            using (var client = new HttpClient {
                BaseAddress = new Uri(Constants.BaseUrl)
            })
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Add(Constants.TokenHeaderKey, authToken);
                client.DefaultRequestHeaders.Add(Constants.UserHeaderKey, authUser);

                var response = await client.PostAsync(urlSpecifier, content);

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

                return(JsonConvert.DeserializeObject <dynamic>(responseJson).id.ToString());
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates the specified currency. Properties that are null won't be changed.
        /// </summary>
        /// <param name="authUser">The e-mail address of the user to whom the currency should be added</param>
        /// <param name="authToken">A valid API token (linked to the e-mail of the specified user)</param>
        /// <param name="currencyToUpdate">Currency object that specifie the properties of the currency to be updated</param>
        /// <returns>Returns the unique Id of the newly created currency</returns>
        public static async Task <string> UpdateAsync(string authUser, string authToken, WalletCurrency currencyToUpdate)
        {
            if (currencyToUpdate.Code == null)
            {
                throw new ArgumentException("Code of updated currency object can't be null");
            }

            if (currencyToUpdate.Id == null)
            {
                throw new ArgumentException("Id of updated currency object can't be null");
            }

            var putConformObject = new
            {
                code = currencyToUpdate.Code,
                ratioToReferential = currencyToUpdate.RatioToReferential,
                referential        = currencyToUpdate.Referential,
                position           = currencyToUpdate.Position
            };

            var urlSpecifier = $"currency/{currencyToUpdate.Id}";
            var contentType  = "application/json";
            var contentJson  = JsonConvert.SerializeObject(putConformObject);
            var content      = new StringContent(contentJson, Encoding.Default, contentType);

            using (var client = new HttpClient {
                BaseAddress = new Uri(Constants.BaseUrl)
            })
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Add(Constants.TokenHeaderKey, authToken);
                client.DefaultRequestHeaders.Add(Constants.UserHeaderKey, authUser);

                var response = await client.PutAsync(urlSpecifier, content);

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

                return(JsonConvert.DeserializeObject <dynamic>(responseJson).id.ToString());
            }
        }