Skip to content

Commit

Permalink
Merge pull request #55 from Moesif/add-subscription-support
Browse files Browse the repository at this point in the history
added subscription support
  • Loading branch information
matthewtanner91 authored Feb 29, 2024
2 parents 97e7ab8 + 71d1a16 commit 281a695
Show file tree
Hide file tree
Showing 6 changed files with 580 additions and 1 deletion.
80 changes: 79 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,83 @@ apiClient.updateCompaniesBatchAsync(companies, callBack);
apiClient.updateCompaniesBatch(companies);
```

## Update a Single Subscription

Create or update a subscription in Moesif. The metadata field can store any subscription-related information you want to keep. The `subscriptionId`, `companyId`, and `status` fields are required. For more details, visit the [Java API Reference](https://www.moesif.com/docs/api?java#update-a-subscription).

```java
MoesifAPIClient apiClient = new MoesifAPIClient("YOUR_COLLECTOR_APPLICATION_ID").Api;

// Create a subscription model with required and optional fields
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();
```

### Update the Subscription Asynchronously

```java
APICallBack<HttpResponse> callBack = new APICallBack<HttpResponse>() {
public void onSuccess(HttpContext context, HttpResponse response) {
// Handle success
}

public void onFailure(HttpContext context, Throwable error) {
// Handle failure
}
};

apiClient.updateSubscriptionAsync(subscription, callBack);
```

### Update the Subscription Synchronously

```java
apiClient.updateSubscription(subscription);
```

## Update Subscriptions in Batch

Similar to updating a single subscription, but used to update a list of subscriptions in one batch. The `subscriptionId`, `companyId`, and `status` fields are required for each subscription in the list.

### Update the Subscriptions Asynchronously

```java
List<SubscriptionModel> subscriptions = new ArrayList<>();
subscriptions.add(subscription); // Assuming 'subscription' is previously defined

APICallBack<HttpResponse> callBack = new APICallBack<HttpResponse>() {
public void onSuccess(HttpContext context, HttpResponse response) {
// Handle success
}

public void onFailure(HttpContext context, Throwable error) {
// Handle failure
}
};

apiClient.updateSubscriptionsBatchAsync(subscriptions, callBack);
```

### Update the Subscriptions Synchronously

```java
apiClient.updateSubscriptionsBatch(subscriptions);
```

## How to build and install manually (Advanced users):


Expand Down Expand Up @@ -552,9 +629,10 @@ http://help.eclipse.org/juno/topic/org.eclipse.jst.j2ee.doc.user/topics/tjimpapp

## How to run JUnit tests for this SDK using `mvn` command

To execute JUnit tests using `mvn` command, the environment variable `MOESIF_APPLICATION_ID` needs to be set
To execute JUnit tests using `mvn` command, the environment variable `MOESIF_APPLICATION_ID` needs to be set. You'll also need to set the `MOESIF_BASE_URI`. Below is the URI for testing against Moesif production, if testing against a local API, change accordingly.
```
export MOESIF_APPLICATION_ID="<Set your Moesif Application Id here>"
export MOESIF_BASE_URI="https://api.moesif.net"
mvn test
```

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/moesif/api/IAPIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.moesif.api.http.client.APICallBack;
import com.moesif.api.models.CompanyModel;
import com.moesif.api.models.EventModel;
import com.moesif.api.models.SubscriptionModel;
import com.moesif.api.models.UserModel;
import com.moesif.api.http.response.HttpResponse;

Expand Down Expand Up @@ -151,4 +152,36 @@ void updateCompanyAsync(final CompanyModel body,
void updateCompaniesBatchAsync(final List<CompanyModel> body,
final APICallBack<HttpResponse> callBack) throws JsonProcessingException;

/**
* Update a Single Subscription
* @param body The subscriptuon to update
* @throws Throwable on error creating event
*/
void updateSubscription(final SubscriptionModel body) throws Throwable;

/**
* Update a Single Subscription async
* @param body The subscription to update
* @param callBack Called after the HTTP response is received
* @throws JsonProcessingException on error creating event
*/
void updateSubscriptionAsync(final SubscriptionModel body,
final APICallBack<HttpResponse> callBack) throws JsonProcessingException;

/**
* Update multiple Subscriptions in a single batch
* @param body The list of subscriptions to update
* @throws Throwable on error creating event
*/
void updateSubscriptionsBatch(final List<SubscriptionModel> body) throws Throwable;

/**
* Update multiple Subscriptions in a single batch async
* @param body The list of subscriptions to update
* @param callBack Called after the HTTP response is received
* @throws JsonProcessingException on error creating event
*/
void updateSubscriptionsBatchAsync(final List<SubscriptionModel> body,
final APICallBack<HttpResponse> callBack) throws JsonProcessingException;

}
66 changes: 66 additions & 0 deletions src/main/java/com/moesif/api/controllers/APIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,72 @@ public void updateCompaniesBatchAsync(
executeRequestAsync(_request, callBack);
}

/**
* Update a Single Subscription
* @param body The subscriptuon to update
* @throws Throwable on error creating event
*/
public void updateSubscription(
final SubscriptionModel body
) throws Throwable {
APICallBackCatcher<HttpResponse> callback = new APICallBackCatcher<HttpResponse>();
updateSubscriptionAsync(body, callback);
if(!callback.isSuccess())
throw callback.getError();
callback.getResult();
}

/**
* Update a Single Subscription async
* @param body The subscription to update
* @param callBack Called after the HTTP response is received
* @throws JsonProcessingException on error creating event
*/
public void updateSubscriptionAsync(
final SubscriptionModel body,
final APICallBack<HttpResponse> callBack
) throws JsonProcessingException {
QueryInfo qInfo = getQueryInfo("/v1/subscriptions");

//prepare and invoke the API call request to fetch the response
final HttpRequest _request = getClientInstance().postBody(qInfo._queryUrl, qInfo._headers, APIHelper.serialize(body));

executeRequestAsync(_request, callBack);
}

/**
* Update multiple Subscriptions in a single batch
* @param body The list of subscriptions to update
* @throws Throwable on error creating event
*/
public void updateSubscriptionsBatch(
final List<SubscriptionModel> body
) throws Throwable {
APICallBackCatcher<HttpResponse> callback = new APICallBackCatcher<HttpResponse>();
updateSubscriptionsBatchAsync(body, callback);
if(!callback.isSuccess())
throw callback.getError();
callback.getResult();
}

/**
* Update multiple Subscriptions in a single batch async
* @param body The list of subscriptions to update
* @param callBack Called after the HTTP response is received
* @throws JsonProcessingException on error creating event
*/
public void updateSubscriptionsBatchAsync(
final List<SubscriptionModel> body,
final APICallBack<HttpResponse> callBack
) throws JsonProcessingException {
QueryInfo qInfo = getQueryInfo("/v1/subscriptions/batch");

//prepare and invoke the API call request to fetch the response
final HttpRequest _request = getClientInstance().postBody(qInfo._queryUrl, qInfo._headers, APIHelper.serialize(body));

executeRequestAsync(_request, callBack);
}

/**
* Get the Application config
* @throws Throwable on error getting app config
Expand Down
88 changes: 88 additions & 0 deletions src/main/java/com/moesif/api/models/SubscriptionBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* MoesifAPILib
*
*
*/
package com.moesif.api.models;

import java.util.*;

public class SubscriptionBuilder {
//the instance to build
private SubscriptionModel SubscriptionModel;

/**
* Default constructor to initialize the instance
*/
public SubscriptionBuilder() {
SubscriptionModel = new SubscriptionModel();
}

/**
* Subscription Id
* @param subscriptionId the field to set
* @return itself
*/
public SubscriptionBuilder subscriptionId(String subscriptionId) {
SubscriptionModel.setSubscriptionId(subscriptionId);
return this;
}

/**
* User's company_id string
* @param companyId the field to set
* @return itself
*/
public SubscriptionBuilder companyId(String companyId) {
SubscriptionModel.setCompanyId(companyId);
return this;
}

/**
* Start date of the current subscription period
* @param currentPeriodStart the field to set
* @return itself
*/
public SubscriptionBuilder currentPeriodStart(Date currentPeriodStart) {
SubscriptionModel.setCurrentPeriodStart(currentPeriodStart);
return this;
}

/**
* End date of the current subscription period
* @param currentPeriodEnd the field to set
* @return itself
*/
public SubscriptionBuilder currentPeriodEnd(Date currentPeriodEnd) {
SubscriptionModel.setCurrentPeriodEnd(currentPeriodEnd);
return this;
}

/**
* Subscription status
* @param status the field to set
* @return itself
*/
public SubscriptionBuilder status(String status) {
SubscriptionModel.setStatus(status);
return this;
}

/**
* Custom user metadata as a JSON object
* @param metadata the field to set
* @return itself
*/
public SubscriptionBuilder metadata(Object metadata) {
SubscriptionModel.setMetadata(metadata);
return this;
}

/**
* Build the instance with the given values
* @return The built UserModel
*/
public SubscriptionModel build() {
return SubscriptionModel;
}
}
Loading

0 comments on commit 281a695

Please sign in to comment.