Search in sources :

Example 1 with Subscriber

use of uk.nhs.digital.common.forms.model.Subscriber in project hippo by NHS-digital-website.

the class SubscriptionResource method subscribe.

public String subscribe(final String emailAddress, final List<String> topicCodes) {
    final Subscriber subscriber = SubscriberFactory.create(emailAddress, topicCodes);
    try {
        final ResourceServiceBroker broker = CrispHstServices.getDefaultResourceServiceBroker(HstServices.getComponentManager());
        final Map<String, Object> pathVars = new HashMap<>();
        pathVars.put("account", ACCOUNT);
        final ExchangeHint exchangeHint = ExchangeHintBuilder.create().methodName("POST").requestHeader("Content-Type", "application/xml").requestBody(XML_MAPPER.writeValueAsString(subscriber)).build();
        final Resource resource = broker.resolve("govDeliveryApi", URL, pathVars, exchangeHint);
        return (String) ((Resource) resource.getValueMap().get("to-param")).getDefaultValue();
    } catch (final JsonProcessingException e) {
        LOGGER.error("Encountered exception when serializing XML.", e);
    } catch (final ResourceException e) {
        LOGGER.error("Encountered communication exception when calling the API.", e);
    } catch (final Exception e) {
        LOGGER.error("Encountered unexpected exception when subscribing user.", e);
    }
    return null;
}
Also used : Subscriber(uk.nhs.digital.common.forms.model.Subscriber) HashMap(java.util.HashMap) ExchangeHint(org.onehippo.cms7.crisp.api.exchange.ExchangeHint) Resource(org.onehippo.cms7.crisp.api.resource.Resource) ResourceException(org.onehippo.cms7.crisp.api.resource.ResourceException) ResourceServiceBroker(org.onehippo.cms7.crisp.api.broker.ResourceServiceBroker) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ResourceException(org.onehippo.cms7.crisp.api.resource.ResourceException)

Example 2 with Subscriber

use of uk.nhs.digital.common.forms.model.Subscriber in project hippo by NHS-digital-website.

the class SubscriberFactoryTest method should_create_subscriber_with_topics.

@Test
public void should_create_subscriber_with_topics() {
    final String emailAddress = "test@test.com";
    final List<String> topicCodes = Arrays.asList("code1", "code2");
    final Subscriber subscriber = SubscriberFactory.create(emailAddress, topicCodes);
    final Topics topics = subscriber.getTopics();
    assertNotNull(subscriber);
    assertNotNull(topics);
    assertNotNull(topics.getTopics());
    assertEquals(emailAddress, subscriber.getEmail());
    assertEquals(2, topics.getTopics().size());
    assertEquals("code1", topics.getTopics().get(0).getCode());
    assertEquals("code2", topics.getTopics().get(1).getCode());
}
Also used : Topics(uk.nhs.digital.common.forms.model.Topics) Subscriber(uk.nhs.digital.common.forms.model.Subscriber) Test(org.junit.Test)

Aggregations

Subscriber (uk.nhs.digital.common.forms.model.Subscriber)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1 ResourceServiceBroker (org.onehippo.cms7.crisp.api.broker.ResourceServiceBroker)1 ExchangeHint (org.onehippo.cms7.crisp.api.exchange.ExchangeHint)1 Resource (org.onehippo.cms7.crisp.api.resource.Resource)1 ResourceException (org.onehippo.cms7.crisp.api.resource.ResourceException)1 Topics (uk.nhs.digital.common.forms.model.Topics)1