public NewsletterPublisher newInstance(String mailChimpToken) { OkHttpClient okHttpClient = new OkHttpClient.Builder() .addInterceptor(new AuthHeaderInterceptor(mailChimpToken)) .build(); Retrofit retrofit = new Retrofit.Builder() .client(okHttpClient) .baseUrl("https://us5.api.mailchimp.com/3.0/") .addConverterFactory(new ConverterFactory()) .build(); MailChimpWebService webService = retrofit.create(MailChimpWebService.class); NewsletterScheduler newsletterScheduler = new NewsletterScheduler(webService); return new NewsletterPublisher(webService, "187645ff44", newsletterScheduler); } }
public void publish(String html, LocalDateTime atLocalDateTime) { Response<ApiCampaignResult> campaignResultResponse = postCampaign(); if (campaignResultResponse.isSuccessful()) { String id = campaignResultResponse.body().id; putCampaignContent(id, html); scheduleNewsletter(id, atLocalDateTime); } else { ResponseBody responseBody = campaignResultResponse.errorBody(); try { System.err.println(responseBody.string()); } catch (IOException e) { throw new IllegalStateException("We failed to debug.", e); } throw new IllegalStateException("We failed " + campaignResultResponse.code()); } }
@Override public Response handleRequest(Request input, Context context) { String slackToken = input.getSlackToken(); String mailChimpApiKey = input.getMailChimpApiKey(); Scraper scraper = new Scraper.Factory().newInstance(slackToken); HtmlGenerator htmlGenerator = new HtmlGenerator.Factory().newInstance(); NewsletterPublisher newsletterPublisher = new NewsletterPublisher.Factory().newInstance(mailChimpApiKey); ArticleEditor articleEditor = new ArticleEditor.Factory().newInstance(); LocalDateTime start = LocalDateTime.now(); LocalDateTime end = LocalDateTime.now().minusDays(6); Stream<ChannelHistory.Message> messageStream = scraper.scrape(start, end); Stream<Article> articleStream = articleEditor.generateArticle(messageStream); String html = htmlGenerator.generate(articleStream); LocalDateTime atLocalDateTime = LocalDateTime.now().plusDays(1).plusHours(1); newsletterPublisher.publish(html, atLocalDateTime); // Time warp campaigns have to be 24 hours in the future // The scheduling is completely hidden here and is a TODO // - because if someone set up a cron job they cannot *fully* control the time right now JsonObject jsonObject = new JsonObject(); jsonObject.add("message", new JsonPrimitive("complete!")); return new Response(jsonObject.getAsJsonObject().toString()); } }
/** * @param args https://api.slack.com/docs/oauth-test-tokens, https://us5.admin.mailchimp.com/account/api/ * @throws IOException */ public static void main(String[] args) throws IOException { if (args.length == 0 || args.length < 2) { throw new IllegalStateException("You need to pass a Slack token as the first arg and MailChimp API token as the second."); } String slackToken = args[0]; String mailChimpToken = args[1]; Scraper scraper = new Scraper.Factory().newInstance(slackToken); HtmlGenerator htmlGenerator = new HtmlGenerator.Factory().newInstance(); ArticleEditor articleEditor = new ArticleEditor.Factory().newInstance(); NewsletterPublisher newsletterPublisher = new NewsletterPublisher.Factory().newInstance(mailChimpToken); LocalDateTime start = LocalDateTime.now(); LocalDateTime end = LocalDateTime.now().minusDays(6); Stream<ChannelHistory.Message> messageStream = scraper.scrape(start, end); Stream<Article> articleStream = articleEditor.generateArticle(messageStream); String html = htmlGenerator.generate(articleStream); LocalDateTime atLocalDateTime = LocalDateTime.now().plusDays(1).plusHours(1); newsletterPublisher.publish(html, atLocalDateTime); // Time warp campaigns have to be 24 hours in the future // The scheduling is completely hidden here and is a TODO // - because if someone set up a cron job they cannot *fully* control the time right now } }