Skip to content

Commit

Permalink
Merge pull request #124 from Moesif/add-subscription-support
Browse files Browse the repository at this point in the history
Added subscription support
  • Loading branch information
matthewtanner91 authored Mar 1, 2024
2 parents d5a806b + dee3d6f commit cc02c8a
Show file tree
Hide file tree
Showing 22 changed files with 631 additions and 13 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,63 @@ CompanyModel company = new CompanyBuilder()
filter.updateCompaniesBatch(companies);
```

## Update a Single Subscription

Create or update a subscription profile in Moesif. The metadata field can store any subscription-related information you wish to keep. The `subscription_id`, `company_id`, and `status` fields are all required. This method is a convenient helper that calls the Moesif API library. For details, visit the [Java API Reference](https://www.moesif.com/docs/api?java#update-a-subscription).

```java
MoesifFilter filter = new MoesifFilter("Your Moesif Application Id", new MoesifConfiguration());

// Only subscriptionId, companyId, and status are required
// metadata can be any custom object
SubscriptionModel subscription = new SubscriptionBuilder()
.subscriptionId("sub_12345")
.companyId("67890")
.status("active")
.metadata(APIHelper.deserialize("{" +
"\"email\": \"[email protected]\"," +
"\"string_field\": \"value_1\"," +
"\"number_field\": 0," +
"\"object_field\": {" +
"\"field_1\": \"value_1\"," +
"\"field_2\": \"value_2\"" +
"}" +
"}"))
.build();

filter.updateSubscription(subscription);
```

## Update Subscriptions in Batch

Similar to `updateSubscription`, but used to update a list of subscriptions in one batch. The `subscription_id`, `company_id`, and `status` fields are required for each subscription in the list. This method is a convenient helper that calls the Moesif API library. For details, visit the [Java API Reference](https://www.moesif.com/docs/api?java#update-subscriptions-in-batch).

You can update subscriptions _synchronously_ or _asynchronously_ on a background thread. Unless you require synchronous behavior, we recommend the async versions.

```java
MoesifFilter filter = new MoesifFilter("Your Moesif Application Id", new MoesifConfiguration());

List<SubscriptionModel> subscriptions = new ArrayList<>();
subscriptions.add(new SubscriptionBuilder()
.subscriptionId("sub_12345")
.companyId("67890")
.status("active")
.metadata(APIHelper.deserialize("{" +
"\"email\": \"[email protected]\"," +
"\"string_field\": \"value_1\"," +
"\"number_field\": 0," +
"\"object_field\": {" +
"\"field_1\": \"value_1\"," +
"\"field_2\": \"value_2\"" +
"}" +
"}"))
.build());

// Add more subscriptions as needed

filter.updateSubscriptionsBatch(subscriptions);
```

## Troubleshooting

### How to print debug logs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<dependency>
<groupId>com.moesif.servlet</groupId>
<artifactId>moesif-servlet-jakarta</artifactId>
<version>2.1.2</version>
<version>2.2.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
import java.util.Date;
import com.moesif.api.APIHelper;
import com.moesif.api.models.*;
import com.moesif.servlet.MoesifConfiguration;
Expand Down Expand Up @@ -162,4 +163,36 @@ public String updateCompany(@PathVariable("id") String id) throws IOException {

return "{ \"update_company\": true }";
}

@RequestMapping(value = "/api/subscriptions/{id}", method = RequestMethod.POST)
@ResponseBody
@ResponseStatus(code = HttpStatus.CREATED)
public String updateSubscription(@PathVariable("id") String id) throws IOException {
// Only subscriptionId is required
// metadata can be any custom object
SubscriptionModel subscription = new SubscriptionBuilder()
.subscriptionId("sub_12345")
.companyId("67890")
.currentPeriodStart(new Date())
.currentPeriodEnd(new Date())
.status("active")
.metadata(APIHelper.deserialize("{" +
"\"email\": \"[email protected]\"," +
"\"string_field\": \"value_1\"," +
"\"number_field\": 0," +
"\"object_field\": {" +
"\"field_1\": \"value_1\"," +
"\"field_2\": \"value_2\"" +
"}" +
"}"))
.build();

try {
moesifFilter.updateSubscription(subscription);
} catch (Throwable t) {
System.out.println("Error while updating the subscription profile.");
}

return "{ \"update_subscription\": true }";
}
}
2 changes: 1 addition & 1 deletion jersey-servlet-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<dependency>
<groupId>com.moesif.servlet</groupId>
<artifactId>moesif-servlet</artifactId>
<version>1.7.11</version>
<version>1.8.0</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.moesif.servlet.jersey.example;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import com.moesif.api.MoesifAPIClient;
import com.moesif.api.APIHelper;
import com.moesif.api.controllers.APIController;
import com.moesif.api.models.SubscriptionModel;
import com.moesif.api.models.SubscriptionBuilder;

/**
* Root resource (exposed at "subscriptions" path)
*/
@Path("subscriptions/{id}")
public class SubscriptionsServlet {

private APIController apiClient;

@Context
private Configuration config;

@PostConstruct
public void init() {
String applicationId = (String) config.getProperty("application-id");
apiClient = new MoesifAPIClient(applicationId).getAPI();
}

/**
* Method handling HTTP POST requests. The returned object will be sent
* to the client as "application/json" media type.
*
* @return Response that will be returned as an application/json.
*/
@POST
@Produces(MediaType.APPLICATION_JSON)
public Response updateSubscription(@PathParam("id") String id) throws Throwable {

// Only subscriptionId, companyId, and status are required
// metadata can be any custom object
SubscriptionModel subscription = new SubscriptionBuilder()
.subscriptionId("sub_12345")
.companyId("67890")
.currentPeriodStart(new Date())
.currentPeriodEnd(new Date())
.status("active")
.metadata(APIHelper.deserialize("{" +
"\"email\": \"[email protected]\"," +
"\"string_field\": \"value_1\"," +
"\"number_field\": 0," +
"\"object_field\": {" +
"\"field_1\": \"value_1\"," +
"\"field_2\": \"value_2\"" +
"}" +
"}"))
.build();

apiClient.updateSubscription(subscription);

return Response.status(Status.CREATED).entity("{"
+ "\"updated_company\": true"
+ "}").build();
}
}
4 changes: 2 additions & 2 deletions moesif-servlet-jakarta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.moesif.servlet</groupId>
<artifactId>moesif-servlet-jakarta</artifactId>
<version>2.1.2</version>
<version>2.2.0</version>
<packaging>jar</packaging>
<name>moesif-servlet-jakarta</name>
<description>Moesif SDK for Java Servlet to log and analyze API calls using Jakarta</description>
Expand Down Expand Up @@ -52,7 +52,7 @@
<dependency>
<groupId>com.moesif.api</groupId>
<artifactId>moesifapi</artifactId>
<version>1.7.7</version>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,69 @@ public void updateCompaniesBatch(List<CompanyModel> companiesModel) throws Throw
}
}

public void updateSubscription(SubscriptionModel subscriptionModel) throws Throwable{

if (this.moesifApi != null) {
String subscriptionId = subscriptionModel.getSubscriptionId();
String companyId = subscriptionModel.getCompanyId();
String status = subscriptionModel.getStatus();

if (subscriptionId != null && !subscriptionId.isEmpty()
|| companyId != null && !companyId.isEmpty()
|| status != null && !status.isEmpty()
) {
try {
moesifApi.getAPI().updateSubscription(subscriptionModel);
}
catch(Exception e) {
if (debug) {
logger.warning("Update Subscription to Moesif failed " + e.toString());
}
}
}
else {
throw new IllegalArgumentException("To update a subscription, the subscriptionId, companyId, and status fields are required");
}
}
else {
logger.warning("The application Id should be set before using MoesifFilter");
}
}

public void updateSubscriptionsBatch(List<SubscriptionModel> subscriptionsModel) throws Throwable{

List<SubscriptionModel> subscriptions = new ArrayList<SubscriptionModel>();
if (this.moesifApi != null) {
for (SubscriptionModel subscription : subscriptionsModel) {
String subscriptionId = subscription.getSubscriptionId();
String companyId = subscription.getCompanyId();
String status = subscription.getStatus();

if (subscriptionId != null && !subscriptionId.isEmpty()
|| companyId != null && !companyId.isEmpty()
|| status != null && !status.isEmpty()
) {
subscriptions.add(subscription);
} else {
throw new IllegalArgumentException("To update a subscription, the subscriptionId, companyId, and status fields are required");
}
}
}
else {
logger.warning("The application Id should be set before using MoesifFilter");
}

if (!subscriptions.isEmpty()) {
try {
moesifApi.getAPI().updateSubscriptionsBatch(subscriptions);
} catch (Exception e) {
if (debug) {
logger.warning("Update Subscriptions to Moesif failed " + e.toString());
}
}
}
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
if (debug) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.moesif.api.models.CampaignModel;
import com.moesif.api.models.CampaignBuilder;
import static org.mockito.Mockito.when;
import com.moesif.api.models.SubscriptionBuilder;
import com.moesif.api.models.SubscriptionModel;


public class MoesifServletTests extends TestCase {
Expand Down Expand Up @@ -241,5 +243,67 @@ public void testUpdateCompaniesBatch() throws Throwable {
filter.updateCompaniesBatch(companies);
}

public void testUpdateSubscription() throws Throwable {

// Only subscriptionId is required
// metadata can be any custom object
SubscriptionModel subscription = new SubscriptionBuilder()
.subscriptionId("sub_12345")
.companyId("67890")
.currentPeriodStart(new Date())
.currentPeriodEnd(new Date())
.status("active")
.metadata(APIHelper.deserialize("{" +
"\"email\": \"[email protected]\"," +
"\"string_field\": \"value_1\"," +
"\"number_field\": 0," +
"\"object_field\": {" +
"\"field_1\": \"value_1\"," +
"\"field_2\": \"value_2\"" +
"}" +
"}"))
.build();

filter.updateSubscription(subscription);
}

public void testUpdateSubscriptionsBatch() throws Throwable {

List<SubscriptionModel> subscriptions = new ArrayList<SubscriptionModel>();

HashMap<String, Object> metadata = new HashMap<String, Object>();
metadata = APIHelper.deserialize("{" +
"\"email\": \"[email protected]\"," +
"\"string_field\": \"value_1\"," +
"\"number_field\": 0," +
"\"object_field\": {" +
"\"field_1\": \"value_1\"," +
"\"field_2\": \"value_2\"" +
"}" +
"}");

SubscriptionModel subscriptionA = new SubscriptionBuilder()
.subscriptionId("sub_12345")
.companyId("67890")
.currentPeriodStart(new Date())
.currentPeriodEnd(new Date())
.status("active")
.metadata(metadata)
.build();

SubscriptionModel subscriptionB = new SubscriptionBuilder()
.subscriptionId("sub_67890")
.companyId("12345")
.currentPeriodStart(new Date())
.currentPeriodEnd(new Date())
.status("active")
.metadata(metadata)
.build();

subscriptions.add(subscriptionA);
subscriptions.add(subscriptionB);

filter.updateSubscriptionsBatch(subscriptions);
}

}
4 changes: 2 additions & 2 deletions moesif-servlet/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.moesif.servlet</groupId>
<artifactId>moesif-servlet</artifactId>
<version>1.7.11</version>
<version>1.8.0</version>
<packaging>jar</packaging>
<name>moesif-servlet</name>
<description>Moesif SDK for Java Servlet to log and analyze API calls</description>
Expand Down Expand Up @@ -57,7 +57,7 @@
<dependency>
<groupId>com.moesif.api</groupId>
<artifactId>moesifapi</artifactId>
<version>1.7.7</version>
<version>1.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Loading

0 comments on commit cc02c8a

Please sign in to comment.