Skip to content

Commit

Permalink
Add Javadoc to all the classes and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
seyiadisa committed Jan 19, 2024
1 parent 5b82365 commit a5b56c7
Show file tree
Hide file tree
Showing 10 changed files with 897 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,45 @@

import org.json.JSONObject;

/**
* The Account class is a wrapper for the Account module of the Chimoney API.
*
*/
public class Account extends Base {

/**
* Constructs a new Account object with the specified Chimoney instance.
*
* @param chimoney the Chimoney instance associated with the account
*/
public Account(Chimoney chimoney) {
super(chimoney);
}

/**
* Retrieves a list of transactions by issue ID.
*
* @param issueID the issueID of the transaction
* @return a list of transactions
* @throws Exception if an error occurs during the request
*/
public List<Object> getTransactionsByIssueID(String issueID) throws Exception {
HttpResponse<String> response = handlePOSTRequest("accounts/issue-id-transactions?issueID=" + issueID,
null);
JSONObject jo = parseJSONData(response);

return jo.getJSONArray("data").toList();
return getTransactionsByIssueID(issueID, null);
}

/**
* Retrieves a list of transactions by issue ID and sub-account.
*
* @param issueID the issue ID of the transactions to retrieve
* @param subAccount the sub-account associated with the transactions
* @return a list of transactions as objects
* @throws Exception if an error occurs during the retrieval process
*/
public List<Object> getTransactionsByIssueID(String issueID, String subAccount) throws Exception {
JSONObject jsonParams = new JSONObject();
jsonParams.put("subAccount", subAccount);

if (subAccount != null)
jsonParams.put("subAccount", subAccount);

HttpResponse<String> response = handlePOSTRequest("accounts/issue-id-transactions?issueID=" + issueID,
jsonParams);
Expand All @@ -31,36 +53,66 @@ public List<Object> getTransactionsByIssueID(String issueID, String subAccount)
return jo.getJSONArray("data").toList();
}

/**
* Retrieves the profile information for a user.
* Specify either userID or linkCode. If both are specified, userID will be
* used.
*
* @param userID the ID of the user
* @param linkCode the link code associated with the user
* @return a map containing the profile information
* @throws Exception if an error occurs during the API request
*/
public Map<String, Object> getProfile(String userID, String linkCode) throws Exception {
JSONObject jsonParams = new JSONObject();
jsonParams.put("userID", userID);
jsonParams.put("linkCode", linkCode);

HttpResponse<String> response = handlePOSTRequest("accounts/public-profile", jsonParams);
JSONObject jo = parseJSONData(response);

return jo.getJSONObject("data").toMap();
return getProfile(userID, linkCode, null);
}

public Map<String, Object> getProfile(String subAccount, String userID, String linkCode) throws Exception {
/**
* Retrieves the profile information for a user and sub-account.
* Specify either userID or linkCode. If both are specified, userID will be
* used.
*
* @param userID the ID of the user
* @param linkCode the link code associated with the user
* @param subAccount the sub-account associated with the user
* @return a map containing the profile information
* @throws Exception if there is an error retrieving the profile
*/
public Map<String, Object> getProfile(String userID, String linkCode, String subAccount) throws Exception {
JSONObject jsonParams = new JSONObject();
jsonParams.put("subAccount", subAccount);
jsonParams.put("userID", userID);
jsonParams.put("linkCode", linkCode);
if (userID != null)
jsonParams.put("userID", userID);
else if (linkCode != null)
jsonParams.put("linkCode", linkCode);
else
throw new ChimoneyException("Either userID or linkCode must be specified.");

if (subAccount != null)
jsonParams.put("subAccount", subAccount);

HttpResponse<String> response = handlePOSTRequest("accounts/public-profile", jsonParams);
JSONObject jo = parseJSONData(response);

return jo.getJSONObject("data").toMap();
}

/**
* Retrieves a list of transactions for this account.
*
* @return a list of transactions for this account
* @throws Exception if an error occurs while retrieving the transactions
*/
public List<Object> getTransactionsByAccount() throws Exception {
HttpResponse<String> response = handlePOSTRequest("accounts/transactions", null);
JSONObject jo = parseJSONData(response);

return jo.getJSONArray("data").toList();
return getTransactionsByAccount(null);
}

/**
* Retrieves a list of transactions for a specific sub-account.
*
* @param subAccount the subAccount for which to retrieve transactions
* @return a list of transactions as objects
* @throws Exception if an error occurs during the request
*/
public List<Object> getTransactionsByAccount(String subAccount) throws Exception {
JSONObject jsonParams = new JSONObject();
jsonParams.put("subAccount", subAccount);
Expand All @@ -71,72 +123,114 @@ public List<Object> getTransactionsByAccount(String subAccount) throws Exception
return jo.getJSONArray("data").toList();
}

/**
* Retrieves a transaction from this account by the transaction ID.
*
* @param id The ID of the transaction to retrieve.
* @return A map containing the transaction data.
* @throws Exception If an error occurs while retrieving the transaction.
*/
public Map<String, Object> getTransaction(String id) throws Exception {
HttpResponse<String> response = handlePOSTRequest("accounts/transaction?id=" + id, null);
JSONObject jo = parseJSONData(response);

return jo.getJSONObject("data").toMap();
return getTransaction(id, null);
}

/**
* Retrieves a transaction from this account by the transaction ID.
*
* @param id The ID of the transaction to retrieve.
* @param subAccount The sub-account associated with the transaction.
* @return A map containing the transaction data.
* @throws Exception If an error occurs while retrieving the transaction.
*/
public Map<String, Object> getTransaction(String id, String subAccount) throws Exception {
JSONObject jsonParams = new JSONObject();
jsonParams.put("subAccount", subAccount);

if (subAccount != null)
jsonParams.put("subAccount", subAccount);

HttpResponse<String> response = handlePOSTRequest("accounts/transaction?id=" + id, jsonParams);
JSONObject jo = parseJSONData(response);

return jo.getJSONObject("data").toMap();
}

/**
* Transfers funds from this account to the specified receiver.
*
* @param receiver the ID of the recipient of the funds
* @param valueInUSD the amount to transfer in USD
* @return a map containing the details of the transfer
* @throws Exception if an error occurs during the transfer
*/
public Map<String, Object> accountTransfer(String receiver, int valueInUSD) throws Exception {
JSONObject jsonParams = new JSONObject();
jsonParams.put("receiver", receiver);
jsonParams.put("valueInUSD", valueInUSD);

HttpResponse<String> response = handlePOSTRequest("accounts/transfer", jsonParams);
JSONObject jo = parseJSONData(response);

return jo.getJSONObject("data").toMap();
return accountTransfer(receiver, valueInUSD, null, null);
}

public Map<String, Object> accountTransfer(String receiver, int valueInUSD, String wallet) throws Exception {
JSONObject jsonParams = new JSONObject();
jsonParams.put("receiver", receiver);
jsonParams.put("valueInUSD", valueInUSD);
jsonParams.put("wallet", wallet);

HttpResponse<String> response = handlePOSTRequest("accounts/transfer", jsonParams);
JSONObject jo = parseJSONData(response);

return jo.getJSONObject("data").toMap();
/**
* Transfers funds from this account to the specified receiver.
*
* @param receiver the ID of the recipient of the funds
* @param valueInUSD the amount to transfer in USD
* @param wallet the wallet type to use for the transfer
* @return a map containing the details of the transfer
* @throws Exception if an error occurs during the transfer
*/
public Map<String, Object> accountTransfer(String receiver, int valueInUSD, Wallet.Types wallet) throws Exception {
return accountTransfer(receiver, valueInUSD, wallet, null);
}

public Map<String, Object> accountTransfer(String receiver, int valueInUSD, String wallet, String subAccount)
/**
* Transfers funds from this account to the specified receiver.
*
* @param receiver the ID of the recipient of the funds
* @param valueInUSD the amount to transfer in USD
* @param wallet the wallet type to use for the transfer
* @param subAccount the sub-account to use for the transfer
* @return a map containing the response data from the transfer operation
* @throws Exception if an error occurs during the transfer
*/
public Map<String, Object> accountTransfer(String receiver, int valueInUSD, Wallet.Types wallet, String subAccount)
throws Exception {
JSONObject jsonParams = new JSONObject();
jsonParams.put("receiver", receiver);
jsonParams.put("valueInUSD", valueInUSD);
jsonParams.put("wallet", wallet);
jsonParams.put("subAccount", subAccount);

if (wallet != null)
jsonParams.put("wallet", wallet.toString());

if (subAccount != null)
jsonParams.put("subAccount", subAccount);

HttpResponse<String> response = handlePOSTRequest("accounts/transfer", jsonParams);
JSONObject jo = parseJSONData(response);

return jo.getJSONObject("data").toMap();
}

public Map<String, Object> deleteUnpaidTransaction(String chiRef)
throws Exception {
HttpResponse<String> response = handleDELETERequest("accounts/delete-unpaid-transaction?chiRef=" + chiRef);
JSONObject jo = parseJSONData(response);

return jo.getJSONObject("data").toMap();
/**
* Deletes an unpaid transaction with the specified chiRef.
*
* @param chiRef the reference of the transaction to be deleted
* @return a map containing the result of the deletion operation
* @throws Exception if an error occurs during the deletion process
*/
public Map<String, Object> deleteUnpaidTransaction(String chiRef) throws Exception {
return deleteUnpaidTransaction(chiRef, null);
}

/**
* Deletes an unpaid transaction with the specified chiRef.
*
* @param chiRef the reference of the transaction to be deleted
* @param subAccount the subAccount associated with the transaction
* @return a map containing the data of the deleted transaction
* @throws Exception if an error occurs during the deletion process
*/
public Map<String, Object> deleteUnpaidTransaction(String chiRef, String subAccount)
throws Exception {
String ext = subAccount != null ? "&subAccount=" + subAccount : "";
HttpResponse<String> response = handleDELETERequest(
"accounts/delete-unpaid-transaction?chiRef=" + chiRef + "&subAccount=" + subAccount);
"accounts/delete-unpaid-transaction?chiRef=" + chiRef + ext);
JSONObject jo = parseJSONData(response);

return jo.getJSONObject("data").toMap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.chimoney.chimoney;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand Down Expand Up @@ -45,7 +44,8 @@ JSONObject parseJSONData(HttpResponse<String> response) throws ChimoneyException
JSONObject obj = new JSONObject(response.body());

if (obj.has("error")) {
throw new ChimoneyException(obj.getString("error"));
String msg = "Error code " + response.statusCode() + " - " + obj.getString("error");
throw new ChimoneyException(msg);
}

return obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,31 @@

import org.json.JSONObject;

/**
* The Beneficiary class is a wrapper for the Beneficiary module of the Chimoney
* API.
*
*/
public class Beneficiary extends Base {

/**
* Constructs a new Beneficiary object with the specified Chimoney instance.
*
* @param chimoney the Chimoney instance associated with the beneficiary
*/
public Beneficiary(Chimoney chimoney) {
super(chimoney);
}

/**
* Creates a beneficiary with the given user ID and beneficiary data.
*
* @param ownerID The ID of the user/sub-account.
* @param beneficiaryData The data of the beneficiary.
* @return A map containing the beneficiary data. Check the Chimoney API
* documentation for more information.
* @throws Exception If an error occurs during the creation of the beneficiary.
*/
public Map<String, Object> createBeneficiary(String ownerID, Map<String, String> beneficiaryData) throws Exception {
JSONObject paramsJson = new JSONObject();
paramsJson.put("owner", ownerID);
Expand Down
Loading

0 comments on commit a5b56c7

Please sign in to comment.