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;
}
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());
}
Aggregations