-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from Moesif/add-subscription-support
Added subscription support
- Loading branch information
Showing
22 changed files
with
631 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 }"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...servlet-example/src/main/java/com/moesif/servlet/jersey/example/SubscriptionsServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.