Usage and Billing Portal

Spring Boot Java Session API

6min
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> &#x9; \<groupid>org projectlombok\</groupid> &#x9; \<artifactid>lombok\</artifactid> &#x9; \<version>1 18 20\</version> &#x9;\</dependency> \</dependencies> setup the environment to configure amberflo client settings setup the environment to configure the amberflo sdk client settings add the following entries to the application properties file set up 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 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 a 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 spring boot java session api docid\ qoxnn2chctmrkvggduxn0 api reference create a session docid 8wrigrkizlweihh9zldvd 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()); } }