-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
376 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/com/faforever/api/mautic/MauticApiErrorHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.faforever.api.mautic; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.DefaultResponseErrorHandler; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
@Component | ||
public class MauticApiErrorHandler extends DefaultResponseErrorHandler { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public MauticApiErrorHandler(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
public void handleError(ClientHttpResponse response) throws IOException { | ||
try (InputStream inputStream = response.getBody()) { | ||
MauticErrorResponse errorResponse = objectMapper.readValue(inputStream, MauticErrorResponse.class); | ||
throw new MauticApiException(errorResponse.getErrors()); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/faforever/api/mautic/MauticApiException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.faforever.api.mautic; | ||
|
||
import com.faforever.api.mautic.MauticErrorResponse.Error; | ||
import lombok.Getter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
public class MauticApiException extends RuntimeException { | ||
private final List<Error> errors; | ||
|
||
public MauticApiException(List<Error> errors) { | ||
super(errors.toString()); | ||
this.errors = errors; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/faforever/api/mautic/MauticApiMessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.faforever.api.mautic; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.SneakyThrows; | ||
import org.springframework.http.HttpInputMessage; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.AbstractHttpMessageConverter; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.inject.Inject; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class MauticApiMessageConverter extends AbstractHttpMessageConverter<Object> { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Inject | ||
public MauticApiMessageConverter(ObjectMapper objectMapper) { | ||
super(MediaType.APPLICATION_JSON); | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
@Override | ||
protected boolean supports(Class<?> clazz) { | ||
return true; | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
@SuppressWarnings("unchecked") | ||
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) { | ||
try(InputStream inputStream = inputMessage.getBody()) { | ||
return objectMapper.readValue(inputStream, Map.class); | ||
} | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
protected void writeInternal(Object o, HttpOutputMessage outputMessage) { | ||
outputMessage.getBody().write(objectMapper.writeValueAsBytes(o)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/faforever/api/mautic/MauticErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.faforever.api.mautic; | ||
|
||
import lombok.Data; | ||
import lombok.ToString; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Data | ||
public class MauticErrorResponse { | ||
private List<Error> errors; | ||
|
||
@Data | ||
@ToString | ||
public static class Error { | ||
private int code; | ||
private String message; | ||
private Map<String, Object> details; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.faforever.api.mautic; | ||
|
||
import com.faforever.api.config.FafApiProperties; | ||
import com.faforever.api.config.FafApiProperties.Mautic; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.ResponseErrorHandler; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import javax.inject.Inject; | ||
import java.time.OffsetDateTime; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Provides access to a Mautic (Open Source Marketing Automation) instance via REST API. | ||
*/ | ||
@Service | ||
public class MauticService { | ||
|
||
private final RestTemplate restOperations; | ||
|
||
@Inject | ||
public MauticService(MauticApiMessageConverter mauticApiMessageConverter, | ||
ResponseErrorHandler mauticApiErrorHandler, FafApiProperties properties) { | ||
this(mauticApiMessageConverter, mauticApiErrorHandler, properties, new RestTemplateBuilder()); | ||
} | ||
|
||
@VisibleForTesting | ||
MauticService(MauticApiMessageConverter mauticApiMessageConverter, | ||
ResponseErrorHandler mauticApiErrorHandler, FafApiProperties properties, | ||
RestTemplateBuilder restTemplateBuilder) { | ||
|
||
Mautic mauticProperties = properties.getMautic(); | ||
|
||
restTemplateBuilder | ||
.additionalMessageConverters(mauticApiMessageConverter) | ||
.errorHandler(mauticApiErrorHandler) | ||
.rootUri(mauticProperties.getBaseUrl()); | ||
|
||
// TODO use as soon as this is solved: https://github.com/mautic/mautic/issues/5743 | ||
// ClientCredentialsResourceDetails details = new ClientCredentialsResourceDetails(); | ||
// details.setClientId(mauticProperties.getClientId()); | ||
// details.setClientSecret(mauticProperties.getClientSecret()); | ||
// details.setClientAuthenticationScheme(AuthenticationScheme.header); | ||
// details.setAccessTokenUri(mauticProperties.getAccessTokenUrl()); | ||
// restOperations = restTemplateBuilder.configure(new OAuth2RestTemplate(details)); | ||
|
||
// TODO for new, client ID needs to be a username and client secret the user's password. | ||
restTemplateBuilder = restTemplateBuilder.basicAuthorization(mauticProperties.getClientId(), mauticProperties.getClientSecret()); | ||
|
||
restOperations = restTemplateBuilder.build(); | ||
} | ||
|
||
@Async | ||
public CompletableFuture<Object> createOrUpdateContact(String email, String fafUserId, String fafUserName, String ipAddress, OffsetDateTime lastActive) { | ||
Map<String, Object> body = new HashMap<>(); | ||
|
||
// These are Mautic default fields | ||
Optional.ofNullable(email).ifPresent(s -> body.put("email", email)); | ||
Optional.ofNullable(ipAddress).ifPresent(s -> body.put("ipAddress", ipAddress)); | ||
Optional.ofNullable(lastActive).ifPresent(s -> body.put("lastActive", lastActive)); | ||
|
||
// These are Mautic "custom fields" that need to be created explicitly. | ||
Optional.ofNullable(fafUserId).ifPresent(s -> body.put("faf_user_id", fafUserId)); | ||
Optional.ofNullable(fafUserName).ifPresent(s -> body.put("faf_username", fafUserName)); | ||
|
||
return CompletableFuture.completedFuture(restOperations.postForObject("/contacts/new", body, Object.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.