Esempio n. 1
0
 public async Task<AsyncResult<string>> SaveAsync(Product product)
 {
     var token = authStore.LoadToken();
     using (var client = new BearerHttpClient(token.AccessToken))
     {
         var data = JsonConvert.SerializeObject(product);
         using (var content = new StringContent(data, Encoding.UTF8, "application/json"))
         {
             if (product.Id == null)
             {
                 var uri = new Uri("api/products/", UriKind.Relative);
                 var resp = await client.PostAsync(uri, content);
                 if (resp.StatusCode == HttpStatusCode.Created)
                 {
                     var id = await resp.Content.ReadAsStringAsync();
                     return new AsyncResult<string> { Result = id, Succeed = true };
                 }
             }
             else
             {
                 var uri = new Uri(string.Concat("api/products/", product.Id), UriKind.Relative);
                 var resp = await client.PutAsync(uri, content);
                 if (resp.StatusCode == HttpStatusCode.OK)
                 {
                     return new AsyncResult<string> { Result = product.Id, Succeed = true };
                 }
             }
         }
     }
     return new AsyncResult<string>();
 }
Esempio n. 2
0
 public async Task<AsyncResult> CreateUser(User user)
 {
     var result = new AsyncResult();
     var token = authStore.LoadToken();
     using (var client = new BearerHttpClient(token.AccessToken))
     {
         var data = JsonConvert.SerializeObject(user);
         using (var content = new StringContent(data, Encoding.UTF8, "application/json"))
         {
             var uri = new Uri("api/users", UriKind.Relative);
             using (var resp = await client.PostAsync(uri, content))
             {
                 if (resp.StatusCode == HttpStatusCode.Created)
                 {
                     result.Succeed = true;
                     return result;
                 }
                 if (resp.StatusCode == HttpStatusCode.InternalServerError)
                 {
                     result.ErrorMessage = "Произошла ошибка на сервере";
                 }
                 else if (resp.StatusCode == HttpStatusCode.BadRequest)
                 {
                     var err = await resp.Content.ReadAsStringAsync();
                     result.ErrorMessage = TranslateError(err);
                 }
                 return result;
             }
         }
     }
 }
Esempio n. 3
0
 public async Task<AsyncResult<Product[]>> GetManyAsync(List<string> ids)
 {
     var token = authStore.LoadToken();
     using (var client = new BearerHttpClient(token.AccessToken))
     {
         var data = JsonConvert.SerializeObject(ids);
         using (var content = new StringContent(data, Encoding.UTF8, "application/json"))
         {
             var uri = new Uri("api/products/getMany", UriKind.Relative);
             using (var resp = await client.PostAsync(uri, content))
             {
                 var str = await resp.Content.ReadAsStringAsync();
                 var res = JsonConvert.DeserializeObject<Product[]>(str);
                 return new AsyncResult<Product[]> { Result = res, Succeed = true };
             }
         }
     }
 }
Esempio n. 4
0
 public async Task<AsyncResult<string>> Create(Stream stream, string fileName, string contentType)
 {
     var token = authStore.LoadToken();
     using (var client = new BearerHttpClient(token.AccessToken))
     using (var multipart = new MultipartFormDataContent())
     using (var content = new StreamContent(stream))
     {
         multipart.Add(content, contentType, fileName);
         var uri = new Uri("api/files", UriKind.Relative);
         var resp = await client.PostAsync(uri, multipart);
         if (resp.StatusCode == HttpStatusCode.Created)
         {
             var id = await resp.Content.ReadAsStringAsync();
             return new AsyncResult<string> { Result = id, Succeed = true };
         }
     }
     return new AsyncResult<string>();
 }
Esempio n. 5
0
        public async Task<AsyncResult> ChangePasswordAsync(string login, string oldPassword, string newPassword)
        {
            var result = new AsyncResult();
            var token = authStore.LoadToken();
            using (var client = new BearerHttpClient(token.AccessToken))
            {
                var data = new Dictionary<string, string>
                {
                    {"oldPassword", oldPassword},
                    {"newPassword", newPassword},
                };

                var uri = string.Format("/api/users/{0}/changePassword", login);

                using (var content = new FormUrlEncodedContent(data))
                using (var resp = await client.PostAsync(uri, content))
                {
                    if (resp.StatusCode == HttpStatusCode.OK)
                    {
                        result.Succeed = true;
                        return result;
                    }

                    if (resp.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        result.ErrorMessage = "Произошла ошибка на сервере";
                    }
                    else if (resp.StatusCode == HttpStatusCode.BadRequest)
                    {
                        var err = await resp.Content.ReadAsStringAsync();
                        result.ErrorMessage = TranslateError(err);
                    }
                    return result;
                }
            }
        }
Esempio n. 6
0
 public async Task<AsyncResult> AttachFile(string productId, string fileId)
 {
     var token = authStore.LoadToken();
     using (var client = new BearerHttpClient(token.AccessToken))
     {
         var uriString = string.Format("api/products/{0}/files", productId);
         var uri = new Uri(uriString, UriKind.Relative);
         var data = new Dictionary<string, string> { {"fileId", fileId} };
         using (var content = new FormUrlEncodedContent(data))
         {
             var resp = await client.PostAsync(uri, content);
             if (resp.StatusCode == HttpStatusCode.Created)
             {
                 return new AsyncResult { Succeed = true };
             }
         }
     }
     return new AsyncResult { Succeed = false };
 }