Implement REST API
API Package
For each api namespace there will be a package generated for example "de.knowis.cards.api.credit" where:
de.knowis.cards is the base package for the service project.
api is the namespace type.
credit is the namespace prefix.
Provider class & Delegate Interface
For each path in your api namespace a provider class will be generated, for example "CreditApiCardsProvider" where:
"credit" is the namespace prefix,
"cards" is the path name "/cards" that this class implements its operations
"provdier" is a postfix for the type of that class.
For Each modelled operation there will be a method in the provider class where operation logic needs to be implemented.
The provider class implements a delegate interface provided by the sdk for example below provider implements "CreditApiCardsDelegate".
This delegate interface contains default implementaion for the operations "i.e returns an example response with 501 no implementation http status code"
Schemas & Parameters
The Request and Reposne schemas are provided by the sdk as classes.
The oepration parameters (path paramters & query paramters) as well as request & response body schemas are typed within the method signature (paramters & return type).
Implemenation Example
Below is an example implementation of "/cards/createcard" post operation.
Notice the autowiring injection of different sdk components such as DomainEntityBuilder , Repository and CardCraeteCommand
package de.knowis.cards.api.credit;
// Java imports
import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
// Importing from sdk api apckage
import de.knowis.cards.sdk.api.credit.api.CreditApiCardsDelegate;
import de.knowis.cards.sdk.api.credit.model.CardSchema;
import de.knowis.cards.sdk.api.credit.model.CardSchema.CardTypeEnum;
import de.knowis.cards.sdk.api.credit.model.SuccessResponseSchema;
// Importing from sdk domain package
import de.knowis.cards.domain.cc.command.CardCraeteCommand;
import de.knowis.cards.sdk.domain.cc.entity.Address;
import de.knowis.cards.sdk.domain.cc.entity.BaseCard;
import de.knowis.cards.sdk.domain.cc.entity.CardRootEntity;
import de.knowis.cards.sdk.domain.cc.entity.CreditCard;
import de.knowis.cards.sdk.domain.cc.entity.Owner;
import de.knowis.cards.sdk.domain.cc.entity.VisaCard;
import de.knowis.cards.sdk.domain.cc.error.CreditCardCreationError;
import de.knowis.cards.sdk.domain.type.Money;
// importing facades from sdk domain package
import de.knowis.cards.sdk.domain.facade.DomainEntityBuilder;
import de.knowis.cards.sdk.domain.facade.Repository;
@Service
@ComponentScan(basePackages = "de.knowis.cards.sdk.api.credit.api")
public class CreditApiCardsProvider implements CreditApiCardsDelegate {
private static Logger log = LoggerFactory.getLogger(CreditApiCardsProvider.class);
@Autowired
private CardCraeteCommand cardFactoryCommand;
@Autowired
private DomainEntityBuilder entityBuilder;
@Autowired
private Repository repo;
@Override
public ResponseEntity<SuccessResponseSchema> createCard(String cardId,
CardSchema cardSchema) {
VisaCard visa = this.entityBuilder.getCc().getVisaCard().build();
visa.setVisaType("American Express");
visa.setCardNumber(String.valueOf(Math.random()));
Owner owner = this.entityBuilder.getCc().getOwner().build();
owner.setAge(20);
owner.setName("John Doe");
owner.setId(cardSchema.getCustomerId());
Address address = this.entityBuilder.getCc().getAddress().build();
address.setCity("Regensburg");
address.setPostcode(93047);
Money currency = new Money();
currency.setAmount(new BigDecimal(2000));
currency.setCurrency(Currency.getInstance("EUR"));
// Creating an input entity that is needed as input to the factory command
CreditCard ccEntity = this.entityBuilder.getCc().getCreditCard().build();
ccEntity.setIsActive(true);
ccEntity.setLimit(cardSchema.getLimit());
ccEntity.setExpiryDate(OffsetDateTime.now());
ccEntity.setCreationTimestamp(OffsetDateTime.now());
ccEntity.setOwner(owner);
ccEntity.setAddress(address);
ccEntity.setCardType(visa);
ccEntity.setCurrency(currency);
try {
CardRootEntity rootEntity = cardFactoryCommand.createCreditCard(ccEntity);
// Prepare and return response
SuccessResponseSchema body = new SuccessResponseSchema();
body.setMessage("Successfully created Credit Card Root Entity with id "
+ rootEntity.getId());
return ResponseEntity.status(HttpStatus.OK).body(body);
} catch (CreditCardCreationError e) {
// TODO Auto-generated catch block
log.error("An error occurred while creating a new credit card", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
//.... More methods for oeprations under the same path