diff --git a/README.md b/README.md index dd380c5..6276a12 100644 --- a/README.md +++ b/README.md @@ -1,126 +1,39 @@ # java-api-client -Mifiel API Client for Java +Java SDK for the [Mifiel](https://www.mifiel.com) API. -Java SDK for [Mifiel](https://www.mifiel.com) API. -Please read our [documentation](http://docs.mifiel.com/) for instructions on how to start using the API. +## Documentation -## Installation - -TODO - -## Usage - -For your convenience Mifiel offers a Sandbox environment where you can confidently test your code. - -To start using the API in the Sandbox environment you need to first create an account at [app-sandbox.mifiel.com](https://app-sandbox.mifiel.com). - -Once you have an account you will need an APP_ID and an APP_SECRET which you can generate in [app-sandbox.mifiel.com/settings/access-tokens](https://app-sandbox.mifiel.com/settings/access-tokens). - -Then you can configure the library with: - -```java - import com.mifiel.api.ApiClient; - - ApiClient apiClient = new ApiClient(appId, appSecret); - // if you want to use our sandbox environment use: - apiClient.setUrl("https://app-sandbox.mifiel.com"); -``` - -By default the client talks to production (`https://app.mifiel.com`). - -Document methods: - -- Find: - - ```java - import com.mifiel.api.dao.Documents; - import com.mifiel.api.objects.Document; +API reference, guides, and examples: - Documents documents = new Documents(apiClient); - Document document = documents.find("id"); - document.getOriginalHash(); - document.getFile(); - document.getFileSigned(); - // ... - ``` +- English: https://docs.mifiel.com/en/ +- EspaƱol: https://docs.mifiel.com/es/ -- Find all: +This README covers installation and client setup only. - ```java - import java.util.List; - import com.mifiel.api.dao.Documents; - import com.mifiel.api.objects.Document; - - Documents documents = new Documents(apiClient); - List allDocuments = documents.findAll(); - ``` - -- Create: - -> Use only **original_hash** if you dont want us to have the file.
-> Only **file** or **original_hash** must be provided. - -```java - import java.util.List; - import java.util.ArrayList; - import com.mifiel.api.dao.Documents; - import com.mifiel.api.objects.Document; - import com.mifiel.api.objects.Signature; - import com.mifiel.api.utils.MifielUtils; - - Documents documents = new Documents(apiClient); - Document document = new Document(); - document.setFile("path/to/my-file.pdf"); - - List signatures = new ArrayList(); - Signature signature1 = new Signature(); - Signature signature2 = new Signature(); - - signature1.setSignature("Signer 1"); - signature1.setEmail("signer1@email.com"); - signature1.setTaxId("AAA010101AAA"); - - signature2.setSignature("Signer 2"); - signature2.setEmail("signer2@email.com"); - signature2.setTaxId("AAA010102AAA"); - - signatures.add(signature1); - signatures.add(signature2); - - document.setSignatures(signatures); - documents.save(document); +## Installation - // if you dont want us to have the PDF, you can just send us - // the original_hash and the name of the document. Both are required - Document document2 = new Document(); - document2.setOriginalHash(MifielUtils.getDocumentHash("path/to/my-file.pdf")); - document2.setSignatures(signatures); +TODO - documents.save(document2); -``` +## Setup -- Save signed files +1. Create an account (production or [sandbox](https://app-sandbox.mifiel.com)). +2. Generate an `APP_ID` and `APP_SECRET` in [Access Tokens](https://app-sandbox.mifiel.com/settings/access-tokens). +3. Configure the client: ```java - import com.mifiel.api.dao.Documents; +import com.mifiel.api.ApiClient; - Documents documents = new Documents(apiClient); - - // download the signed pdf - documents.saveSignedFile("id", "path/to/save/file-signed.pdf"); - // download the signed xml file - documents.saveXml("id", "path/to/save/xml.xml"); - // download zip file containing the signed pdf and the xml - documents.saveZip("id", "path/to/save/xml.xml"); +ApiClient apiClient = new ApiClient(appId, appSecret); +// Production is the default (https://app.mifiel.com). +// For sandbox: +apiClient.setUrl("https://app-sandbox.mifiel.com"); ``` -- Delete - - ```java - import com.mifiel.api.dao.Documents; - import com.mifiel.api.objects.Document; +## Contributing - Documents documents = new Documents(apiClient); - documents.delete("id"); - ``` +1. Fork it (https://github.com/Mifiel/java-api-client/fork) +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request diff --git a/src/main/java/com/mifiel/api/dao/Webhooks.java b/src/main/java/com/mifiel/api/dao/Webhooks.java new file mode 100644 index 0000000..3c32408 --- /dev/null +++ b/src/main/java/com/mifiel/api/dao/Webhooks.java @@ -0,0 +1,79 @@ +package com.mifiel.api.dao; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.http.HttpEntity; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; + +import com.mifiel.api.ApiClient; +import com.mifiel.api.exception.MifielException; +import com.mifiel.api.objects.Webhook; +import com.mifiel.api.utils.MifielUtils; + +/** + * CRUD + trigger helpers for account-level webhooks. + * + * @see Webhooks API + */ +public class Webhooks extends BaseObjectDAO { + + private final String WEBHOOK_CANONICAL_NAME = Webhook.class.getCanonicalName(); + private final String WEBHOOKS_PATH = "webhooks"; + + public Webhooks(final ApiClient apiClient) { + super(apiClient); + } + + @Override + public Webhook find(final String id) throws MifielException { + final HttpEntity entityResponse = apiClient.get(WEBHOOKS_PATH + "/" + id); + final String response = MifielUtils.entityToString(entityResponse); + return (Webhook) MifielUtils.convertJsonToObject(response, WEBHOOK_CANONICAL_NAME); + } + + @Override + @SuppressWarnings("unchecked") + public List findAll() throws MifielException { + final HttpEntity entityResponse = apiClient.get(WEBHOOKS_PATH); + final String response = MifielUtils.entityToString(entityResponse); + return (List) (Object) MifielUtils.convertJsonToObjects(response, WEBHOOK_CANONICAL_NAME); + } + + @Override + public void delete(final String id) throws MifielException { + apiClient.delete(WEBHOOKS_PATH + "/" + id); + } + + @Override + public Webhook save(final Webhook webhook) throws MifielException { + final String json = MifielUtils.convertObjectToJson(webhook); + final StringEntity httpContent = new StringEntity(json, ContentType.APPLICATION_JSON); + final HttpEntity entityResponse = apiClient.post(WEBHOOKS_PATH, httpContent); + final String response = MifielUtils.entityToString(entityResponse); + return (Webhook) MifielUtils.convertJsonToObject(response, WEBHOOK_CANONICAL_NAME); + } + + /** + * Trigger delivery for a webhook. + * + * @param id webhook id + * @param resource UUID of the related resource included in the callback payload + * @param instant when true, deliver immediately once instead of enqueueing retries + */ + public String trigger(final String id, final String resource, final boolean instant) throws MifielException { + final Map body = new HashMap(); + body.put("resource", resource); + body.put("instant", instant); + final String json = MifielUtils.convertObjectToJson(body); + final StringEntity httpContent = new StringEntity(json, ContentType.APPLICATION_JSON); + final HttpEntity entityResponse = apiClient.post(WEBHOOKS_PATH + "/" + id + "/trigger", httpContent); + return MifielUtils.entityToString(entityResponse); + } + + public String trigger(final String id, final String resource) throws MifielException { + return trigger(id, resource, false); + } +} diff --git a/src/main/java/com/mifiel/api/objects/Webhook.java b/src/main/java/com/mifiel/api/objects/Webhook.java new file mode 100644 index 0000000..68bd6ed --- /dev/null +++ b/src/main/java/com/mifiel/api/objects/Webhook.java @@ -0,0 +1,57 @@ +package com.mifiel.api.objects; + +import org.codehaus.jackson.annotate.JsonIgnoreProperties; +import org.codehaus.jackson.annotate.JsonProperty; + +/** + * Account-level webhook subscription. + * + * @see Webhooks API + */ +@JsonIgnoreProperties(ignoreUnknown = true) +public class Webhook { + + @JsonProperty("id") + private String id; + + @JsonProperty("url") + private String url; + + @JsonProperty("callback_type") + private String callbackType; + + @JsonProperty("created_at") + private String createdAt; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getCallbackType() { + return callbackType; + } + + public void setCallbackType(String callbackType) { + this.callbackType = callbackType; + } + + public String getCreatedAt() { + return createdAt; + } + + public void setCreatedAt(String createdAt) { + this.createdAt = createdAt; + } +}