-
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.
Still requires the latest java API release (with subscription support) to be added to POM of each project. Then, this will all need to be tested.
1 parent
d5a806b
commit 1c204ed
Showing
11 changed files
with
554 additions
and
0 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
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 }"; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
import com.moesif.api.APIHelper; | ||
import com.moesif.api.models.CompanyBuilder; | ||
import com.moesif.api.models.CompanyModel; | ||
import com.moesif.api.models.SubscriptionBuilder; | ||
import com.moesif.api.models.SubscriptionModel; | ||
import com.moesif.api.models.UserBuilder; | ||
import junit.framework.TestCase; | ||
import org.mockito.Mockito; | ||
|
@@ -241,5 +243,60 @@ public void testUpdateCompaniesBatch() throws Throwable { | |
filter.updateCompaniesBatch(companies); | ||
} | ||
|
||
public void testUpdateSubscription() throws Throwable { | ||
|
||
// Only subscriptionId, companyId, and status | ||
// 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>(); | ||
|
||
SubscriptionModel subscriptionA = new SubscriptionBuilder() | ||
.subscriptionId("sub_12345") | ||
.companyId("67890") | ||
.currentPeriodStart(new Date()) | ||
.currentPeriodEnd(new Date()) | ||
.status("active") | ||
.build(); | ||
subscriptions.add(subscriptionA); | ||
|
||
SubscriptionModel subscriptionB = new SubscriptionBuilder() | ||
.subscriptionId("sub_54321") | ||
.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(); | ||
subscriptions.add(subscriptionB); | ||
|
||
filter.updateSubscriptionsBatch(subscriptions); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
servlet-example/src/main/java/com/moesif/servlet/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,74 @@ | ||
package com.moesif.servlet.example; | ||
|
||
import java.util.Date; | ||
|
||
import com.moesif.api.APIHelper; | ||
import com.moesif.api.models.*; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import com.moesif.servlet.MoesifFilter; | ||
|
||
public class SubscriptionsServlet extends HttpServlet { | ||
|
||
private MoesifFilter moesifFilter; | ||
|
||
@Override | ||
public void init() throws ServletException { | ||
String applicationId = getInitParameter("application-id"); | ||
moesifFilter = new MoesifFilter(); | ||
moesifFilter.setApplicationId(applicationId); | ||
} | ||
|
||
public void doPost(HttpServletRequest request, HttpServletResponse response) | ||
throws IOException { | ||
|
||
if (request.getPathInfo() != null && !request.getPathInfo().isEmpty()) { | ||
// Only subscriptionId, companyId, and status | ||
// 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\": \"johndoe@acmeinc.com\"," + | ||
"\"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."); | ||
} | ||
|
||
response.setHeader("Content-Type", "application/json"); | ||
response.setStatus(201); | ||
PrintWriter out = response.getWriter(); | ||
String json = "{" | ||
+ "\"updated_subscription\": true" | ||
+ "}"; | ||
out.println(json); | ||
} else { | ||
response.setHeader("Content-Type", "application/json"); | ||
response.setStatus(400); | ||
PrintWriter out = response.getWriter(); | ||
String json = "{" | ||
+ "\"msg\": \"subscription_id, company_id, and status are required.\"" | ||
+ "}"; | ||
out.println(json); | ||
} | ||
} | ||
} | ||
|
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
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