Exemple #1
0
        public async Task<ActionResult> Index()
        {
            string code = Request.QueryString["code"];

            if (!string.IsNullOrEmpty(code))
            {
                ToshlOAuth toshlOAuth = (ToshlOAuth)Session["toshlOAuth"];

                OAuthToken oAuthToken = await toshlOAuth.GetAccessToken(Request.Url.AbsoluteUri);

                Me me = new Me(oAuthToken.AccessToken);

                User user = await me.Get();

                user.LastName = "tset";

                //User user1 = await me.Put(user);

                ExpenseApi expenseApi = new ExpenseApi(oAuthToken.AccessToken);

                Expense expense = new Expense()
                {
                    Date = "2014-09-06",
                    Amount = 12.34M,
                    Tags = new string[] { "test", "test432" }
                };

                //await expenseApi.Create(expense);
            }

            return View();
        }
Exemple #2
0
        public async Task<Validation<string>> Create(Expense expense)
        {
            IEnumerable<KeyValuePair<string, string>> dict = UrlFormEncoded.Encode(expense);

            HttpRequestItem httpRequestItem = new HttpRequestItem()
            {
                Url = "https://api.toshl.com/expenses/",
                HttpMethod = HttpMethod.Post,
                AuthHeaderValue = new AuthenticationHeaderValue("Bearer", AccessToken),
                HttpContent = new FormUrlEncodedContent(dict)
            };

            HttpResponseMessage httpResponseMessage = await _requestHandler.RequestAsync(httpRequestItem);

            if (httpResponseMessage.IsSuccessStatusCode)
            {
                string id = httpResponseMessage.Headers.GetValues("Location").First();
                id = id.Split('/').Last();

                return new Validation<string>()
                {
                    ErrorMessages = new string[0],
                    ReturnObject = id
                };
            }

            return new Validation<string>()
            {
                ErrorMessages = new string[]
                {
                    httpResponseMessage.ReasonPhrase
                }
            };
        }