Example #1
0
        public async Task <ProjectAdo> UpdateItemAsync(ProjectAdo item)
        {
            try
            {
                await base.AddAuthHeaderAsync();

                var url = $"{ApiPart}";
                //var add = Mapper.Map<ProjectAdo>(item);
                var httpcontent = new StringContent(JsonSerializer.Serialize(item, _options), Encoding.UTF8, "application/json");
                var apiData     = await HttpClientBase.PutAsync(url, httpcontent).ConfigureAwait(false);

                if (apiData.IsSuccessStatusCode)
                {
                    var content = await apiData.Content.ReadAsStringAsync();

                    var ser = JsonSerializer.Deserialize <ProjectAdo>(content, _options);
                    return(ser);
                }
                else
                {
                    throw new Exception(base.GetErrorText(apiData));
                }
            }
            catch (Exception ex)
            {
                base.HandleException(ex);
                throw;
            }
            //return false;
        }
Example #2
0
        public async Task <TModel> RestUpdate <TModel, TDto>(TModel model, CancellationToken cancelToken = default) where TModel : class
        {
            if (model == null)
            {
                throw new ArgumentException($"'{nameof(model)}' cannot be null", nameof(model));
            }

            try
            {
                await AddAuthHeaderAsync();

                var url         = $"{ApiPart}";
                var updateDto   = Mapper.Map <TDto>(model);
                var httpcontent = new StringContent(JsonSerializer.Serialize(updateDto, _options), Encoding.UTF8, "application/json");
                var apiData     = await HttpClientBase.PutAsync(url, httpcontent, cancelToken).ConfigureAwait(false);

                if (apiData.IsSuccessStatusCode)
                {
                    var stream = await apiData.Content.ReadAsStreamAsync();

                    if (stream.Length == 0)
                    {
                        return(null);
                    }
                    var ser = await JsonSerializer.DeserializeAsync <TDto>(stream, _options, cancelToken);

                    if (ser == null)
                    {
                        return(null);
                    }

                    var result = Mapper.Map <TModel>(ser);
                    var con    = await ConvertModelToLocalTimeBase(result);

                    return(con);
                }
                else
                {
                    throw new Exception(GetErrorText(apiData));
                }
            }
            catch (Exception ex)
            {
                HandleException(ex, cancelToken);
                return(null);
            }
        }
Example #3
0
        public async Task<ResourceGroupModel> CreateRgAsync(ResourceGroupModel item, CancellationToken cancelToken = default)
        {
            try
            {
                await base.AddAuthHeaderAsync();

                var add = Mapper.Map<ResourceGroupCreateDto>(item.ResourceGroup);
                add.Name = null;
                var httpcontent = new StringContent(JsonSerializer.Serialize(add, _options), Encoding.UTF8, "application/json");

                //PUT https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}?api-version=2020-06-01
                var url = $"https://management.azure.com/subscriptions/{item.SubcriptionId}/resourcegroups/{item.NewRgName}?api-version=2020-06-01";
                var apiData = await HttpClientBase.PutAsync(url, httpcontent, cancelToken).ConfigureAwait(false);

                if (apiData.IsSuccessStatusCode)
                {
                    var content = await apiData.Content.ReadAsStringAsync();
                    var ser = JsonSerializer.Deserialize<ResourceGroupDto>(content, _options);
                    var result = Mapper.Map<ResourceGroup>(ser);
                    item.ResourceGroup = result;
                    item.IsNewRg = false;
                    item.NewRgName = null;
                    return item;

                    //var model = Mapper.Map<ResourceGroupModel>(result);
                    //return model;
                }
                else
                {
                    var content = await apiData.Content.ReadAsStringAsync();
                    throw new Exception(content);
                }
            }
            catch (Exception ex)
            {
                base.HandleException(ex);
                throw;
            }
        }