Spring Boot Java Session API
This sample illustrates setting up an API endpoint using Spring Boot to call the Amberflo Session API and return the session token using the JAVA SDK. This session token can be used for all Customer Usage and Billing Portal apps.
Steps:
- Setup Amberflo environment
- Setup configuration for service discovery of Amberflo Customer Usage and Billing Portal Session client
- Create API Endpoint and controller to return Session ID.
Maven dependencies (check for latest versions):
<dependencies>
<dependency>
<groupId>io.amberflo</groupId>
<artifactId>metering-java-client</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
</dependencies>
Setup the environment to configure Amberflo client settings
- Setup the environment to configure the Amberflo SDK client settings
- Add following entries to the application.properties file
- Setup an environment for service discoverable.
amberflo.apiKey=your-api-key-here
amberflo.region=US_West
amberflo.domain=Prod
amberflo.ingestion.frequency.seconds=1
amberflo.ingestion.batch.size=10
amberflo.isDebug=true
Property | Description | Options |
---|---|---|
apiKey | Amberflo API key | |
region | The region of where the ingestion client is running | US_West |
domain | The domain can be Prod or Dev | Prod, Dev |
ingestion.frequency.seconds | Frequency at which queued data will be sent to API. | 1 |
ingestion.batch.size | Number of messages posted to the API. | |
amberflo.isDebug | Log debug info for ingestion code calling the SDK |
import com.amberflo.metering.ingest.meter_message.Domain;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.amberflo.metering.ingest.meter_message.Region;
@Getter
@Component
public class AmberfloEnvironment {
@Value("${amberflo.apiKey}")
private String apiKey;
@Value("${amberflo.region}")
private Region region;
@Value("${amberflo.domain}")
private Domain domain;
@Value("${amberflo.ingestion.frequency.seconds}")
private Double ingestionFrequencyInSeconds;
@Value("${amberflo.ingestion.batch.size}")
private Integer ingestionBatchSize;
}
Setup Amberflo client bean for service discoverability
Make the Customer Usage and Billing Portal Session client be a discoverable service that can be injected as a dependency by Spring Boot configuration. This will return the client instance as a bean.
import com.amberflo.metering.customerportal.clients.CustomerPortalSessionClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AmberfloClientConfig {
@Autowired
private AmberfloEnvironment amberfloEnvironment;
@Bean
public CustomerPortalSessionClient customerPortalSessionClient(){
LOGGER.info("Amberflo client CustomerPortalSessionClient initialized");
return new CustomerPortalSessionClient(this.amberfloEnvironment.getApiKey());
}
}
API Endpoint to return session token
In most instances, the user or customer ID is passed in a request header. Set IDENTITY_HEADER to this header.
This is a sample REST controller which returns the session token from Amberflo. This session token can be used for any of the Customer Usage and Billing Portal apps.
The endpoint will be https://yourappdomain/amberflo/session
API Reference https://amberflo.readme.io/reference/post_session
import com.amberflo.metering.customerportal.clients.CustomerPortalSessionClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.amberflo.metering.customerportal.model.CustomerPortalSessionRequest;
import com.amberflo.metering.customerportal.model.CustomerPortalSessionResponse;
@RestController
@RequestMapping("/amberflo")
public class AmberfloController {
//TODO: add the identity header
private final static String IDENTITY_HEADER = "";
@Autowired
private CustomerPortalSessionClient customerPortalSessionClient;
@PostMapping("/session")
public ResponseEntity getConnectedApps(@RequestAttribute(IDENTITY_HEADER) String customerId){
final CustomerPortalSessionRequest customerPortalSessionRequest = new CustomerPortalSessionRequest(customerId);
final CustomerPortalSessionResponse result = this.customerPortalSessionClient.getSession(customerPortalSessionRequest);
return ResponseEntity.ok(result.getSessionToken());
}
}
Updated over 1 year ago