public async Task Save(Payload <IPayload> payload) { _logger.LogInformation($"Saving apartment for rent data located in: {payload.Apartment.City}"); var apartmentContent = new StringContent(JsonConvert.SerializeObject(payload), System.Text.Encoding.UTF8, "application/json"); HttpResponseMessage response; string url = Uris.GetUrl(_apartmentUrl, payload.InObject.GetType().Name); if (payload.Apartment.Id > 0) { response = await _httpClient.PutAsync(url, apartmentContent); } else { response = await _httpClient.PostAsync(url, apartmentContent); } _logger.LogInformation($"Response of saving apartment, Status Code: {response.StatusCode}"); response.EnsureSuccessStatusCode(); }
public async Task UploadImage(IFormFile file, Guid requestId) { if (file == null) { return; } var rule = new IsFileNotNull().And(new IsFileSizeSuitable(_options)).And(new IsFileExtntionSuitable()).And(new IsFileSignatureSuitable()); if (!rule.IsSatisfiedBy(file)) { throw new ArgumentException("Data saved, but file size should less than 2Mb and type should be [JPG, JPEG, PNG]."); } string url = Uris.GetUrl(_apartmentUrl, "Pic"); var imgData = new MultipartFormDataContent { { new StreamContent(file.OpenReadStream()), "imgfile", file.FileName } }; _httpClient.DefaultRequestHeaders.Add("x-requestid", requestId.ToString()); var response = await _httpClient.PostAsync(url, imgData); _logger.LogDebug("[UploadImage] -> response code {StatusCode}", response.StatusCode); response.EnsureSuccessStatusCode(); }