com.threewks.thundr.mailgun.MailgunImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of thundr-mailgun Show documentation
Show all versions of thundr-mailgun Show documentation
A thundr module for interacting with Mailgun APIs
/*
* This file is a component of thundr, a software library from 3wks.
* Read more: http://www.3wks.com.au/thundr
* Copyright (C) 2013 3wks,
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.threewks.thundr.mailgun;
import java.util.Map;
import com.atomicleopard.expressive.Expressive;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.threewks.thundr.http.HttpSupport;
import com.threewks.thundr.http.service.HttpRequest;
import com.threewks.thundr.http.service.HttpResponse;
import com.threewks.thundr.http.service.HttpService;
public class MailgunImpl implements Mailgun {
private static final String MailgunApiFormat = "https://api.mailgun.net/v2/";
private HttpService httpService;
private String domain;
private String apiUrl;
private String apiKey;
private GsonBuilder gsonBuilder;
public MailgunImpl(HttpService httpService, String mailgunDomain, String mailgunApiKey) {
this.httpService = httpService;
this.domain = mailgunDomain;
this.apiKey = mailgunApiKey;
this.apiUrl = MailgunApiFormat + mailgunDomain + "/";
this.gsonBuilder = new GsonBuilder();
}
@Override
public String getDomain() {
return domain;
}
@Override
public Log log(Integer skip, Integer limit) {
Map params = Expressive.map();
if (skip != null) {
params.put("skip", skip);
}
if (limit != null) {
params.put("limit", limit);
}
return getOrPost(Service.Log, Log.class, params, true);
}
@Override
public MessageSend sendEmail(String from, String to, String replyTo, String subject, String htmlBody) {
Map params = Expressive.map();
params.put("from", from);
params.put("to", to);
if (replyTo != null) {
params.put("h:Reply-To", replyTo);
}
params.put("subject", subject);
params.put("html", htmlBody);
return getOrPost(Service.Messages, MessageSend.class, params, false);
}
// package protected for stubbing out in tests
T getOrPost(Service service, Class type, Map params, boolean getOrPost) {
String serviceUrl = apiUrl + service.service();
HttpRequest request = httpService.request(serviceUrl);
request.parameters(params);
request.authorize("api", this.apiKey);
Gson gson = gsonBuilder.create();
HttpResponse httpResponse = getOrPost ? request.get() : request.post();
int status = httpResponse.getStatus();
if (status != 200) {
throw new MailgunException("Failed to %s %s: response code %d (%s)", getOrPost ? "GET" : "POST", serviceUrl, status, HttpSupport.getReasonForHttpStatus(status));
}
String response = httpResponse.getBody();
T typedResponse = gson.fromJson(response, type);
return typedResponse;
}
}