Skip to content

Commit

Permalink
Attachment endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
trungie committed Apr 19, 2017
1 parent 2e0e004 commit a429213
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 81 deletions.
43 changes: 26 additions & 17 deletions src/main/java/com/xero/api/OAuthRequestResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@

import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.auth.oauth.OAuthSigner;
import com.google.api.client.http.ByteArrayContent;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpContent;
import com.google.api.client.http.HttpHeaders;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.*;
import com.google.api.client.http.apache.ApacheHttpTransport;
import com.google.api.client.util.Beta;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -81,21 +75,36 @@ public class OAuthRequestResource extends GenericUrl {
/** {@code true} for POST request or the default {@code false} for GET request. */
protected boolean usePost;

public OAuthRequestResource(Config config, String resource, String method, String body, Map<? extends String, ?> params) {
private void init(Config config, String resource, String method, Map<? extends String, ?> params) {
this.config = config;

Url = new GenericUrl(config.getApiUrl() + resource);
this.httpMethod = method;
if(method.equals("POST") || method.equals("PUT")){
usePost = true;
}
}

if (params != null) {
Url.putAll(params);
}

Url.putAll(params);
}
}

public OAuthRequestResource(Config config, String resource, String method, String body, Map<? extends String, ?> params) {
init(config, resource, method, params);
this.body = body;
}

public OAuthRequestResource(Config config, String resource, String method, String contentType, byte[] bytes, Map<? extends String, ?> params) {
init(config, resource, method, params);
this.contentType = contentType;
this.requestBody = new ByteArrayContent(contentType, bytes);
}

public OAuthRequestResource(Config config, String resource, String method, String contentType, File file, Map<? extends String, ?> params) {
init(config, resource, method, params);
this.contentType = contentType;
this.requestBody = new FileContent(contentType, file);
}

/**
* Executes the HTTP request for a temporary or long-lived token.
Expand All @@ -113,7 +122,7 @@ public final HttpResponse execute() throws IOException {

transport = new ApacheHttpTransport();

if(usePost){
if(usePost && body != null){
requestBody = ByteArrayContent.fromString(null, body);
}

Expand All @@ -123,7 +132,7 @@ public final HttpResponse execute() throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.setUserAgent(config.getUserAgent());
headers.setAccept(config.getAccept());
headers.setContentType("application/xml");
headers.setContentType(contentType == null ? "application/xml" : contentType);
if(ifModifiedSince != null) {
System.out.println("Set Header " + this.ifModifiedSince);
headers.setIfModifiedSince(this.ifModifiedSince);
Expand Down
109 changes: 45 additions & 64 deletions src/main/java/com/xero/api/XeroClient.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.xero.api;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
Expand All @@ -23,66 +20,7 @@
import javax.xml.transform.stream.StreamSource;

import com.google.api.client.http.HttpResponse;
import com.xero.model.Account;
import com.xero.model.Allocation;
import com.xero.model.ApiException;
import com.xero.model.ArrayOfAccount;
import com.xero.model.ArrayOfAllocation;
import com.xero.model.ArrayOfBankTransaction;
import com.xero.model.ArrayOfBankTransfer;
import com.xero.model.ArrayOfBrandingTheme;
import com.xero.model.ArrayOfContact;
import com.xero.model.ArrayOfContactGroup;
import com.xero.model.ArrayOfCreditNote;
import com.xero.model.ArrayOfCurrency;
import com.xero.model.ArrayOfEmployee;
import com.xero.model.ArrayOfExpenseClaim;
import com.xero.model.ArrayOfInvoice;
import com.xero.model.ArrayOfInvoiceReminder;
import com.xero.model.ArrayOfItem;
import com.xero.model.ArrayOfJournal;
import com.xero.model.ArrayOfLinkedTransaction;
import com.xero.model.ArrayOfManualJournal;
import com.xero.model.ArrayOfOverpayment;
import com.xero.model.ArrayOfPayment;
import com.xero.model.ArrayOfPrepayment;
import com.xero.model.ArrayOfPurchaseOrder;
import com.xero.model.ArrayOfReceipt;
import com.xero.model.ArrayOfRepeatingInvoice;
import com.xero.model.ArrayOfTaxRate;
import com.xero.model.ArrayOfTrackingCategory;
import com.xero.model.ArrayOfTrackingCategoryOption;
import com.xero.model.ArrayOfUser;
import com.xero.model.BankTransaction;
import com.xero.model.BankTransfer;
import com.xero.model.BrandingTheme;
import com.xero.model.Contact;
import com.xero.model.ContactGroup;
import com.xero.model.ContactGroupStatus;
import com.xero.model.CreditNote;
import com.xero.model.Currency;
import com.xero.model.Employee;
import com.xero.model.ExpenseClaim;
import com.xero.model.Invoice;
import com.xero.model.InvoiceReminder;
import com.xero.model.Item;
import com.xero.model.Journal;
import com.xero.model.LinkedTransaction;
import com.xero.model.ManualJournal;
import com.xero.model.ObjectFactory;
import com.xero.model.Organisation;
import com.xero.model.Overpayment;
import com.xero.model.Payment;
import com.xero.model.Prepayment;
import com.xero.model.PurchaseOrder;
import com.xero.model.Receipt;
import com.xero.model.RepeatingInvoice;
import com.xero.model.Report;
import com.xero.model.Response;
import com.xero.model.TaxRate;
import com.xero.model.TrackingCategory;
import com.xero.model.TrackingCategoryOption;
import com.xero.model.User;
import com.xero.model.*;

public class XeroClient {

Expand Down Expand Up @@ -178,6 +116,36 @@ protected Response put(String endPoint, JAXBElement<?> object) throws IOExceptio
return unmarshallResponse(resp.parseAsString(), Response.class);
}

protected Response put(String endPoint, String contentType, byte[] bytes) throws IOException {
HttpResponse resp = null;
OAuthRequestResource req = new OAuthRequestResource(config, endPoint,"PUT", contentType, bytes,null);
req.setToken(token);
req.setTokenSecret(tokenSecret);

resp = req.execute();

if (resp.getStatusCode() != 200) {
throw newApiException(resp);
}

return unmarshallResponse(resp.parseAsString(), Response.class);
}

protected Response put(String endPoint, String contentType, File file) throws IOException {
HttpResponse resp = null;
OAuthRequestResource req = new OAuthRequestResource(config, endPoint,"PUT", contentType, file,null);
req.setToken(token);
req.setTokenSecret(tokenSecret);

resp = req.execute();

if (resp.getStatusCode() != 200) {
throw newApiException(resp);
}

return unmarshallResponse(resp.parseAsString(), Response.class);
}

protected Response post(String endPoint, JAXBElement<?> object) throws IOException {
String contents = marshallRequest(object);
HttpResponse resp = null;
Expand Down Expand Up @@ -1307,4 +1275,17 @@ public User getUser(String id) throws IOException {
return singleResult(get("Users/" + id).getUsers().getUser());
}

// ATTACHMENTS
public List<Attachment> getAttachments(String endpoint, String guid) throws IOException {
return get(endpoint + "/" + guid + "/Attachments/", null, null).getAttachments().getAttachment();
}

public Attachment createAttachment(String endpoint, String guid, String filename, String contentType, byte[] bytes) throws IOException {
return singleResult(put(endpoint + "/" + guid + "/Attachments/" + filename, contentType, bytes).getAttachments().getAttachment());
}

public Attachment createAttachment(String endpoint, String guid, String filename, String contentType, File file) throws IOException {
return singleResult(put(endpoint + "/" + guid + "/Attachments/" + filename, contentType, file).getAttachments().getAttachment());
}

}

0 comments on commit a429213

Please sign in to comment.