Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-30 adding a createOrUpdateProvider() method to the ProviderService #31

Merged
merged 4 commits into from
Oct 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2017 Adobe. All rights reserved.
* This file is licensed to you 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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package com.adobe.event.management;

import feign.FeignException;
import feign.Response;
import feign.codec.ErrorDecoder;

public class ConflictErrorDecoder implements ErrorDecoder {

@Override
public Exception decode(String methodKey, Response response) {
FeignException exception = FeignException.errorStatus(methodKey, response);
return (response.status()==409) ? new ConflictException(response, exception) : exception;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2017 Adobe. All rights reserved.
* This file is licensed to you 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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package com.adobe.event.management;

import static com.adobe.util.JacksonUtil.DEFAULT_OBJECT_MAPPER;

import com.adobe.event.management.model.ErrorResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import feign.FeignException;
import feign.Response;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConflictException extends FeignException {

private final Logger logger = LoggerFactory.getLogger(this.getClass());
private final String conflictingId;

public ConflictException(Response response, FeignException exception) {
super(response.status(), exception.getMessage(), response.request(), exception);
conflictingId = getConflictingId(exception.contentUTF8());
}

public Optional<String> getConflictingId(){
return Optional.ofNullable(conflictingId);
}

private String getConflictingId(String body){
try {
String conflictingId = DEFAULT_OBJECT_MAPPER.readValue(body, ErrorResponse.class).getMessage();
if (!StringUtils.isEmpty(conflictingId) && !conflictingId.contains(" ")) {
return conflictingId;
} else {
logger.warn("The Conflict/409 Error response does not hold a valid conflicting id: `{}`",conflictingId);
return null;
}
} catch (JsonProcessingException e) {
e.printStackTrace();
sgera marked this conversation as resolved.
Show resolved Hide resolved
logger.warn("The Conflict/409 Error response is not of the expected format",e.getMessage());
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*
*/
public interface ProviderService {
String CUSTOM_EVENTS_PROVIDER_METADATA_ID = "3rd_party_custom_events";
sgera marked this conversation as resolved.
Show resolved Hide resolved

List<Provider> getProviders();

Expand All @@ -41,8 +42,23 @@ public interface ProviderService {

Optional<Provider> createProvider(final ProviderInputModel providerCreateModel);

/**
*
* @param providerInputModel the input payload
* @return create and if conflict/409 arises, instead, update a provider using the provided payload
*/
Optional<Provider> createOrUpdateProvider(ProviderInputModel providerInputModel);

Optional<Provider> updateProvider(final String id, final ProviderInputModel providerUpdateModel);

Optional<Provider> findCustomEventsProviderByInstanceId(String instanceId);

/**
*
* @param providerMetadataId
* @param instanceId
* @return the providers list matching the provided criteria and with non-empty event metadata list
*/
sgera marked this conversation as resolved.
Show resolved Hide resolved
Optional<Provider> findProviderBy(final String providerMetadataId, final String instanceId);

List<EventMetadata> getEventMetadata(final String providerId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class ProviderServiceImpl implements ProviderService {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

private final ProviderApi providerApi;
private final EventMetadataApi eventMetadataApi;
private final Workspace workspace;
Expand All @@ -46,6 +50,7 @@ class ProviderServiceImpl implements ProviderService {
workspace.validateWorkspaceContext();
this.providerApi = FeignUtil.getDefaultBuilder()
.requestInterceptor(authInterceptor)
.errorDecoder(new ConflictErrorDecoder())
.target(ProviderApi.class, apiUrl);
this.eventMetadataApi = FeignUtil.getDefaultBuilder()
.requestInterceptor(authInterceptor)
Expand All @@ -71,23 +76,44 @@ public Optional<Provider> findProviderById(final String providerId) {

@Override
public void deleteProvider(final String providerId) {
logger.info("Deleting provider id `{}`",providerId);
providerApi.delete(workspace.getConsumerOrgId(), workspace.getProjectId(),
workspace.getWorkspaceId(), providerId);
}

@Override
public Optional<Provider> createProvider(final ProviderInputModel providerInputModel) {
logger.info("Creating provider using `{}`",providerInputModel);
return providerApi.create(workspace.getConsumerOrgId(), workspace.getProjectId(),
workspace.getWorkspaceId(), providerInputModel);
}

@Override
public Optional<Provider> createOrUpdateProvider(final ProviderInputModel providerInputModel) {
try {
return createProvider(providerInputModel);
}
catch (ConflictException e){
String providerId = e.getConflictingId().orElseThrow(()->e);
logger.info("Another provider (id:`{}`) exist with conflicting natural keys, "
+ "trying to update it ...",providerId);
return updateProvider(providerId,providerInputModel);
}
}

@Override
public Optional<Provider> updateProvider(final String id,
final ProviderInputModel providerUpdateModel) {
logger.info("Updating provider `{}` using `{}`",id, providerUpdateModel);
return providerApi.update(workspace.getConsumerOrgId(), workspace.getProjectId(),
workspace.getWorkspaceId(), id, providerUpdateModel);
}

@Override
public Optional<Provider> findCustomEventsProviderByInstanceId(final String instanceId){
return findProviderBy(CUSTOM_EVENTS_PROVIDER_METADATA_ID,instanceId);
}

@Override
public Optional<Provider> findProviderBy(final String providerMetadataId,
final String instanceId) {
Expand All @@ -102,7 +128,7 @@ public Optional<Provider> findProviderBy(final String providerMetadataId,
if (providerCollection.get().getProviders().isEmpty()) {
return Optional.empty();
} else {
return Optional.of(providerCollection.get().getProviders().get(1));
return Optional.of(providerCollection.get().getProviders().get(0));
// there can only be one by API contract
}
} else {
Expand All @@ -129,6 +155,7 @@ public Optional<EventMetadata> getEventMetadata(final String providerId, final S
@Override
public Optional<EventMetadata> createEventMetadata(final String providerId,
final EventMetadata eventMetadata) {
logger.info("Creating Event Metadata for provider `{}` using `{}`", providerId, eventMetadata);
return eventMetadataApi.create(
workspace.getConsumerOrgId(), workspace.getProjectId(), workspace.getWorkspaceId(),
providerId, eventMetadata);
Expand All @@ -137,20 +164,23 @@ public Optional<EventMetadata> createEventMetadata(final String providerId,
@Override
public Optional<EventMetadata> updateEventMetadata(final String providerId,
final EventMetadata eventMetadata) {
logger.info("Updating Event Metadata for provider `{}` using `{}`", providerId, eventMetadata);
return eventMetadataApi.update(
workspace.getConsumerOrgId(), workspace.getProjectId(), workspace.getWorkspaceId(),
providerId, eventMetadata.getEventCode(), eventMetadata);
}

@Override
public void deleteEventMetadata(final String providerId, final String eventCode) {
logger.info("Deleting Event Metadata for provider `{}` and eventCode `{}`", providerId, eventCode);
eventMetadataApi.deleteByProviderIdAndEventCode(
workspace.getConsumerOrgId(), workspace.getProjectId(), workspace.getWorkspaceId(),
providerId, eventCode);
}

@Override
public void deleteAllEventMetadata(final String providerId) {
logger.info("Deleting All Event Metadata for provider `{}`", providerId);
eventMetadataApi.deleteByProviderId(
workspace.getConsumerOrgId(), workspace.getProjectId(), workspace.getWorkspaceId(),
providerId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2017 Adobe. All rights reserved.
* This file is licensed to you 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 REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package com.adobe.event.management.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.util.Objects;

@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class ErrorResponse {

private String reason;
private String message;

public String getReason() {
return reason;
}

public void setReason(String reason) {
this.reason = reason;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ErrorResponse that = (ErrorResponse) o;
return Objects.equals(reason, that.reason) &&
Objects.equals(message, that.message);
}

@Override
public int hashCode() {
return Objects.hash(reason, message);
}

@Override
public String toString() {
return "ErrorResponse{" +
"reason='" + reason + '\'' +
", message='" + message + '\'' +
'}';
}
}
Loading