public static async Task <ProCampaignResponse> PostParticipationAsync( string participationData, ProCampaignSettings settings, bool requestConsumerId = false) { if (settings == null) { throw new ArgumentNullException(nameof(settings), "ProCampaign settings cannot be null"); } var apiUrl = new Uri(settings.ConsumerBaseUrl, settings.ParticipationPath); using (var client = new HttpClient(httpHandler, false)) { client.BaseAddress = apiUrl; client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); if (requestConsumerId) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", $"{StringUtility.Base64Encode($"{settings.ApiKey}:{settings.ApiSecret}")}"); } string query; var queryStrings = requestConsumerId ? new Dictionary <string, string> { { "apiKey", settings.ApiKey }, { "requestConsumerId", "true" } }: new Dictionary <string, string> { { "apiKey", settings.ApiKey } }; using (var content = new FormUrlEncodedContent(queryStrings)) { query = content.ReadAsStringAsync().Result; } var data = new StringContent(participationData, Encoding.UTF8, "application/json"); string result; using (var response = await client.PostAsync($"{apiUrl}?{query}", data)) { using (var content = response.Content) { result = await content.ReadAsStringAsync(); } } return(JsonConvert.DeserializeObject <ProCampaignResponse>(result)); } }
public bool HasAlreadyParticipated(string email) { var emailHash = StringUtility.Md5HashEncode(email.ToLower()); return(_participationRepository.GetByEmailHash(emailHash) != null); }
public async Task <IHttpActionResult> Participate([FromBody] ParticipationViewModel participate) { dynamic expando = new ExpandoObject(); var apiResponse = new ApiResponse { Success = false, Message = "Bad Request", Data = expando }; try { if (participate == null) { expando.Error = new List <string>() { "Body request data should be as documented" }; apiResponse.Message = "Missing or unknown request body"; apiResponse.Data = expando; _logger.LogWarn("Participate validation Error", "Body request not found or unknown"); return(Content(HttpStatusCode.BadRequest, apiResponse)); } if (!ModelState.IsValid) { var allErrors = ModelState.Values.SelectMany(v => v.Errors); var errorList = allErrors.Select(error => error.ErrorMessage); expando.Error = errorList; apiResponse.Message = "Validation error occured"; apiResponse.Data = expando; _logger.LogWarn("Participate validation Error", string.Join(", ", errorList)); return(Content(HttpStatusCode.BadRequest, apiResponse)); } var dto = new ParticipationDto { Id = Guid.NewGuid(), EmailHash = StringUtility.Md5HashEncode(participate.Email.ToLower()), Email = participate.Email, SiteId = _siteService.GetSiteByCulture(participate.Culture)?.Id, RetailerConsent = participate.RetailerConsent, NewsletterOptin = participate.NewsletterOptin }; var crmResponse = await _journeyService.ParticipateAsync(dto, participate.Culture); var site = _siteService.GetSiteByCulture(participate.Culture); expando.Description = "Your participate has been taken into account."; expando.ConsumerId = crmResponse.Item1 ? crmResponse.Item2 : string.Empty; expando.ParticipationId = dto.Id; apiResponse.Success = true; apiResponse.Message = crmResponse.Item1 ? "Participate successfull" : "Your participate has been considered but CRM sync has failed"; apiResponse.Data = expando; _logger.LogTrace(apiResponse.Message, $"Ok: {dto.Id}"); return(Ok(apiResponse)); } catch (Exception e) { expando.Error = e.Message; apiResponse.Success = false; apiResponse.Message = $"Error occured in {e.Source}"; apiResponse.Data = expando; _logger.LogError(e.Message, e); return(Content(HttpStatusCode.InternalServerError, apiResponse)); } }