public async Task <JsonResult> SaveProfileAvatar([FromForm] AssetFormVM uploading) { _logger.LogInformation("ProfileController.SaveProfileAvatar - Service starts."); var errors = uploading.CheckFile(); if (errors.Count != 0) { var messages = uploading.GenerateErrorMessages(errors); return(new JsonResult(new { Result = RESULTS.FAILED, Message = messages })); } var response = await _waterService.SendSaveAvatarRequestToWater(uploading); if (response == null) { return(new JsonResult(new { Result = RESULTS.FAILED, Message = "An error occurred while we're processing your request. Please try again." })); } if (response.Error) { return(new JsonResult(new { Result = RESULTS.FAILED, response.ErrorMessage })); } return(await UpdateHidrogenianAvatarInternally(uploading.HidrogenianId, uploading.ApiKey, response.Result)); }
public async Task <AvatarResultVM> SendSaveAvatarRequestToWater(AssetFormVM uploading) { _logger.LogInformation("WaterService.SendSaveAvatarRequest - Service starts."); _waterRequest.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(HidroConstants.CONTENT_TYPES["form"])); var formData = new MultipartFormDataContent(); var formImage = new StreamContent(uploading.File.OpenReadStream()); formImage.Headers.ContentType = MediaTypeHeaderValue.Parse(uploading.File.ContentType); formData.Add(formImage, "image", uploading.File.FileName); formData.Add(new StringContent(uploading.HidrogenianId.ToString()), "hidrogenianId"); formData.Add(new StringContent(uploading.ApiKey), "apikey"); var response = await _waterRequest.PostAsync("avatar/save-avatar", formData); if (!response.IsSuccessStatusCode) { return(null); } AvatarResultVM result; try { result = JsonConvert.DeserializeObject <AvatarResultVM>(await response.Content.ReadAsStringAsync()); } catch (Exception e) { _logger.LogError("WaterService.SendSaveAvatarRequest - Error: " + e); var data = await response.Content.ReadAsStringAsync(); _logger.LogError("JSON Data: " + data); return(null); } return(result); }