Merge pull request #312 from idosh/master

add support to multipart form data and urlencoded for android client
This commit is contained in:
Tony Tam 2014-12-02 20:40:02 -08:00
commit 43a5ece5b3
10 changed files with 867 additions and 240 deletions

View File

@ -131,6 +131,24 @@
<version>${httpclient-version}</version> <version>${httpclient-version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpclient-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpclient-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>compile</scope>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>
@ -146,7 +164,7 @@
<junit-version>4.8.1</junit-version> <junit-version>4.8.1</junit-version>
<maven-plugin-version>1.0.0</maven-plugin-version> <maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version> <junit-version>4.8.1</junit-version>
<httpclient-version>4.0</httpclient-version> <httpclient-version>4.3</httpclient-version>
</properties> </properties>
</project> </project>

View File

@ -12,7 +12,9 @@ import org.apache.http.client.methods.*;
import org.apache.http.conn.*; import org.apache.http.conn.*;
import org.apache.http.conn.scheme.*; import org.apache.http.conn.scheme.*;
import org.apache.http.conn.ssl.*; import org.apache.http.conn.ssl.*;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.impl.client.*; import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.*; import org.apache.http.impl.conn.*;
import org.apache.http.params.*; import org.apache.http.params.*;
@ -156,10 +158,23 @@ public class ApiInvoker {
} }
else if ("POST".equals(method)) { else if ("POST".equals(method)) {
HttpPost post = new HttpPost(url); HttpPost post = new HttpPost(url);
if (body != null) { if (body != null) {
post.setHeader("Content-Type", contentType); if("application/x-www-form-urlencoded".equals(contentType)) {
post.setEntity(new StringEntity(serialize(body), "UTF-8")); post.setHeader("Content-Type", contentType);
post.setEntity(new UrlEncodedFormEntity((List<NameValuePair>)body));
}
else if("multipart/form-data".equals(contentType)) {
String boundary = "-------------" + System.currentTimeMillis();
post.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
MultipartEntityBuilder builder = (MultipartEntityBuilder)body;
builder.setBoundary(boundary);
post.setEntity(builder.build());
}
else {
post.setHeader("Content-Type", contentType);
post.setEntity(new StringEntity(serialize(body), "UTF-8"));
}
} }
for(String key : headers.keySet()) { for(String key : headers.keySet()) {
post.setHeader(key, headers.get(key)); post.setHeader(key, headers.get(key));
@ -169,8 +184,21 @@ public class ApiInvoker {
else if ("PUT".equals(method)) { else if ("PUT".equals(method)) {
HttpPut put = new HttpPut(url); HttpPut put = new HttpPut(url);
if(body != null) { if(body != null) {
put.setHeader("Content-Type", contentType); if("application/x-www-form-urlencoded".equals(contentType)) {
put.setEntity(new StringEntity(serialize(body), "UTF-8")); put.setHeader("Content-Type", contentType);
put.setEntity(new UrlEncodedFormEntity((List<NameValuePair>)body));
}
else if("multipart/form-data".equals(contentType)) {
String boundary = "-------------" + System.currentTimeMillis();
put.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
MultipartEntityBuilder builder = (MultipartEntityBuilder)body;
builder.setBoundary(boundary);
put.setEntity(builder.build());
}
else {
put.setHeader("Content-Type", contentType);
put.setEntity(new StringEntity(serialize(body), "UTF-8"));
}
} }
for(String key : headers.keySet()) { for(String key : headers.keySet()) {
put.setHeader(key, headers.get(key)); put.setHeader(key, headers.get(key));

View File

@ -6,6 +6,14 @@ import com.wordnik.petstore.model.Pet;
import java.util.*; import java.util.*;
import java.io.File; import java.io.File;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.mime.content.*;
import org.apache.http.entity.ContentType;
import android.webkit.MimeTypeMap;
public class PetApi { public class PetApi {
String basePath = "http://petstore.swagger.wordnik.com/api"; String basePath = "http://petstore.swagger.wordnik.com/api";
ApiInvoker apiInvoker = ApiInvoker.getInstance(); ApiInvoker apiInvoker = ApiInvoker.getInstance();
@ -26,56 +34,53 @@ public class PetApi {
return basePath; return basePath;
} }
private static String getMimeType(File file) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
int index = file.getName().lastIndexOf('.')+1;
String ext = file.getName().substring(index).toLowerCase();
return mime.getMimeTypeFromExtension(ext);
}
//error info- code: 400 reason: "Invalid ID supplied" model: <none> //error info- code: 400 reason: "Invalid ID supplied" model: <none>
//error info- code: 404 reason: "Pet not found" model: <none> //error info- code: 404 reason: "Pet not found" model: <none>
public Pet getPetById (Long petId) throws ApiException { //error info- code: 405 reason: "Validation exception" model: <none>
public void updatePet (Pet body) throws ApiException {
Object postBody = body;
// verify required params are set // verify required params are set
if(petId == null ) { if(body == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
} }
// create path and map variables // create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString())); String path = "/pet".replaceAll("\\{format\\}","json");
// query params // query params
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = body;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, contentType);
if(response != null){
return (Pet) ApiInvoker.deserialize(response, "", Pet.class);
}
else {
return null;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return null;
}
else {
throw ex;
}
}
}
//error info- code: 405 reason: "Invalid input" model: <none>
public void updatePetWithForm (String petId, String name, String status) throws ApiException {
// verify required params are set
if(petId == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, contentType);
if(response != null){ if(response != null){
return ; return ;
} }
@ -91,72 +96,9 @@ public class PetApi {
} }
} }
} }
//error info- code: 400 reason: "Invalid pet value" model: <none>
public void deletePet (String petId) throws ApiException {
// verify required params are set
if(petId == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
//error info- code: 400 reason: "Invalid tag value" model: <none>
public List<Pet> partialUpdate (String petId, Pet body) throws ApiException {
// verify required params are set
if(petId == null || body == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, body, headerParams, contentType);
if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
}
else {
return null;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return null;
}
else {
throw ex;
}
}
}
//error info- code: 405 reason: "Invalid input" model: <none> //error info- code: 405 reason: "Invalid input" model: <none>
public void addPet (Pet body) throws ApiException { public void addPet (Pet body) throws ApiException {
Object postBody = body;
// verify required params are set // verify required params are set
if(body == null ) { if(body == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -168,44 +110,30 @@ public class PetApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json","application/xml"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = body;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
//error info- code: 400 reason: "Invalid ID supplied" model: <none>
//error info- code: 404 reason: "Pet not found" model: <none>
//error info- code: 405 reason: "Validation exception" model: <none>
public void updatePet (Pet body) throws ApiException {
// verify required params are set
if(body == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/pet".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, contentType);
if(response != null){ if(response != null){
return ; return ;
} }
@ -223,6 +151,7 @@ public class PetApi {
} }
//error info- code: 400 reason: "Invalid status value" model: <none> //error info- code: 400 reason: "Invalid status value" model: <none>
public List<Pet> findPetsByStatus (String status) throws ApiException { public List<Pet> findPetsByStatus (String status) throws ApiException {
Object postBody = null;
// verify required params are set // verify required params are set
if(status == null ) { if(status == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -236,10 +165,30 @@ public class PetApi {
if(!"null".equals(String.valueOf(status))) if(!"null".equals(String.valueOf(status)))
queryParams.put("status", String.valueOf(status)); queryParams.put("status", String.valueOf(status));
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class); return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
} }
@ -257,6 +206,7 @@ public class PetApi {
} }
//error info- code: 400 reason: "Invalid tag value" model: <none> //error info- code: 400 reason: "Invalid tag value" model: <none>
public List<Pet> findPetsByTags (String tags) throws ApiException { public List<Pet> findPetsByTags (String tags) throws ApiException {
Object postBody = null;
// verify required params are set // verify required params are set
if(tags == null ) { if(tags == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -270,10 +220,251 @@ public class PetApi {
if(!"null".equals(String.valueOf(tags))) if(!"null".equals(String.valueOf(tags)))
queryParams.put("tags", String.valueOf(tags)); queryParams.put("tags", String.valueOf(tags));
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType);
if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
}
else {
return null;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return null;
}
else {
throw ex;
}
}
}
//error info- code: 405 reason: "Invalid input" model: <none>
public void updatePetWithForm (String petId, String name, String status) throws ApiException {
Object postBody = null;
// verify required params are set
if(petId == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String[] contentTypes = {
"application/x-www-form-urlencoded"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
hasFields = true;
mp.add(new BasicNameValuePair("name", name));
hasFields = true;
mp.add(new BasicNameValuePair("status", status));
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
hasFields = true;
builder.addTextBody("name", name.toString());
hasFields = true;
builder.addTextBody("status", status.toString());
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
//error info- code: 400 reason: "Invalid ID supplied" model: <none>
//error info- code: 404 reason: "Pet not found" model: <none>
public Pet getPetById (Long petId) throws ApiException {
Object postBody = null;
// verify required params are set
if(petId == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType);
if(response != null){
return (Pet) ApiInvoker.deserialize(response, "", Pet.class);
}
else {
return null;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return null;
}
else {
throw ex;
}
}
}
//error info- code: 400 reason: "Invalid pet value" model: <none>
public void deletePet (String petId) throws ApiException {
Object postBody = null;
// verify required params are set
if(petId == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try {
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
//error info- code: 400 reason: "Invalid tag value" model: <none>
public List<Pet> partialUpdate (String petId, Pet body) throws ApiException {
Object postBody = body;
// verify required params are set
if(petId == null || body == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/pet/{petId}".replaceAll("\\{format\\}","json").replaceAll("\\{" + "petId" + "\\}", apiInvoker.escapeString(petId.toString()));
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String[] contentTypes = {
"application/json","application/xml"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = body;
}
try {
String response = apiInvoker.invokeAPI(basePath, path, "PATCH", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class); return (List<Pet>) ApiInvoker.deserialize(response, "List", Pet.class);
} }
@ -290,6 +481,7 @@ public class PetApi {
} }
} }
public void uploadFile (String additionalMetadata, File file) throws ApiException { public void uploadFile (String additionalMetadata, File file) throws ApiException {
Object postBody = null;
// create path and map variables // create path and map variables
String path = "/pet/uploadImage".replaceAll("\\{format\\}","json"); String path = "/pet/uploadImage".replaceAll("\\{format\\}","json");
@ -297,10 +489,36 @@ public class PetApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"multipart/form-data"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
hasFields = true;
mp.add(new BasicNameValuePair("additionalMetadata", additionalMetadata));
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
hasFields = true;
builder.addTextBody("additionalMetadata", additionalMetadata.toString());
hasFields = true;
builder.addBinaryBody("file", file, ContentType.create(getMimeType(file)), file.getName());
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return ; return ;
} }

View File

@ -6,6 +6,14 @@ import com.wordnik.petstore.model.Order;
import java.util.*; import java.util.*;
import java.io.File; import java.io.File;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.mime.content.*;
import org.apache.http.entity.ContentType;
import android.webkit.MimeTypeMap;
public class StoreApi { public class StoreApi {
String basePath = "http://petstore.swagger.wordnik.com/api"; String basePath = "http://petstore.swagger.wordnik.com/api";
ApiInvoker apiInvoker = ApiInvoker.getInstance(); ApiInvoker apiInvoker = ApiInvoker.getInstance();
@ -26,8 +34,16 @@ public class StoreApi {
return basePath; return basePath;
} }
private static String getMimeType(File file) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
int index = file.getName().lastIndexOf('.')+1;
String ext = file.getName().substring(index).toLowerCase();
return mime.getMimeTypeFromExtension(ext);
}
//error info- code: 400 reason: "Invalid order" model: <none> //error info- code: 400 reason: "Invalid order" model: <none>
public void placeOrder (Order body) throws ApiException { public void placeOrder (Order body) throws ApiException {
Object postBody = body;
// verify required params are set // verify required params are set
if(body == null ) { if(body == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -39,10 +55,30 @@ public class StoreApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = body;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return ; return ;
} }
@ -61,6 +97,7 @@ public class StoreApi {
//error info- code: 400 reason: "Invalid ID supplied" model: <none> //error info- code: 400 reason: "Invalid ID supplied" model: <none>
//error info- code: 404 reason: "Order not found" model: <none> //error info- code: 404 reason: "Order not found" model: <none>
public void deleteOrder (String orderId) throws ApiException { public void deleteOrder (String orderId) throws ApiException {
Object postBody = null;
// verify required params are set // verify required params are set
if(orderId == null ) { if(orderId == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -72,10 +109,30 @@ public class StoreApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return ; return ;
} }
@ -94,6 +151,7 @@ public class StoreApi {
//error info- code: 400 reason: "Invalid ID supplied" model: <none> //error info- code: 400 reason: "Invalid ID supplied" model: <none>
//error info- code: 404 reason: "Order not found" model: <none> //error info- code: 404 reason: "Order not found" model: <none>
public Order getOrderById (String orderId) throws ApiException { public Order getOrderById (String orderId) throws ApiException {
Object postBody = null;
// verify required params are set // verify required params are set
if(orderId == null ) { if(orderId == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -105,10 +163,30 @@ public class StoreApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return (Order) ApiInvoker.deserialize(response, "", Order.class); return (Order) ApiInvoker.deserialize(response, "", Order.class);
} }

View File

@ -6,6 +6,14 @@ import com.wordnik.petstore.model.User;
import java.util.*; import java.util.*;
import java.io.File; import java.io.File;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.mime.content.*;
import org.apache.http.entity.ContentType;
import android.webkit.MimeTypeMap;
public class UserApi { public class UserApi {
String basePath = "http://petstore.swagger.wordnik.com/api"; String basePath = "http://petstore.swagger.wordnik.com/api";
ApiInvoker apiInvoker = ApiInvoker.getInstance(); ApiInvoker apiInvoker = ApiInvoker.getInstance();
@ -26,9 +34,121 @@ public class UserApi {
return basePath; return basePath;
} }
private static String getMimeType(File file) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
int index = file.getName().lastIndexOf('.')+1;
String ext = file.getName().substring(index).toLowerCase();
return mime.getMimeTypeFromExtension(ext);
}
public void createUsersWithArrayInput (List<User> body) throws ApiException {
Object postBody = body;
// verify required params are set
if(body == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/user/createWithArray".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = body;
}
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
public void createUsersWithListInput (List<User> body) throws ApiException {
Object postBody = body;
// verify required params are set
if(body == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/user/createWithList".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = body;
}
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
//error info- code: 400 reason: "Invalid username supplied" model: <none> //error info- code: 400 reason: "Invalid username supplied" model: <none>
//error info- code: 404 reason: "User not found" model: <none> //error info- code: 404 reason: "User not found" model: <none>
public void updateUser (String username, User body) throws ApiException { public void updateUser (String username, User body) throws ApiException {
Object postBody = body;
// verify required params are set // verify required params are set
if(username == null || body == null ) { if(username == null || body == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -40,10 +160,30 @@ public class UserApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = body;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, body, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "PUT", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return ; return ;
} }
@ -62,6 +202,7 @@ public class UserApi {
//error info- code: 400 reason: "Invalid username supplied" model: <none> //error info- code: 400 reason: "Invalid username supplied" model: <none>
//error info- code: 404 reason: "User not found" model: <none> //error info- code: 404 reason: "User not found" model: <none>
public void deleteUser (String username) throws ApiException { public void deleteUser (String username) throws ApiException {
Object postBody = null;
// verify required params are set // verify required params are set
if(username == null ) { if(username == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -73,10 +214,30 @@ public class UserApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "DELETE", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return ; return ;
} }
@ -95,6 +256,7 @@ public class UserApi {
//error info- code: 400 reason: "Invalid username supplied" model: <none> //error info- code: 400 reason: "Invalid username supplied" model: <none>
//error info- code: 404 reason: "User not found" model: <none> //error info- code: 404 reason: "User not found" model: <none>
public User getUserByName (String username) throws ApiException { public User getUserByName (String username) throws ApiException {
Object postBody = null;
// verify required params are set // verify required params are set
if(username == null ) { if(username == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -106,10 +268,30 @@ public class UserApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return (User) ApiInvoker.deserialize(response, "", User.class); return (User) ApiInvoker.deserialize(response, "", User.class);
} }
@ -127,6 +309,7 @@ public class UserApi {
} }
//error info- code: 400 reason: "Invalid username and password combination" model: <none> //error info- code: 400 reason: "Invalid username and password combination" model: <none>
public String loginUser (String username, String password) throws ApiException { public String loginUser (String username, String password) throws ApiException {
Object postBody = null;
// verify required params are set // verify required params are set
if(username == null || password == null ) { if(username == null || password == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -142,10 +325,30 @@ public class UserApi {
queryParams.put("username", String.valueOf(username)); queryParams.put("username", String.valueOf(username));
if(!"null".equals(String.valueOf(password))) if(!"null".equals(String.valueOf(password)))
queryParams.put("password", String.valueOf(password)); queryParams.put("password", String.valueOf(password));
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return (String) ApiInvoker.deserialize(response, "", String.class); return (String) ApiInvoker.deserialize(response, "", String.class);
} }
@ -162,6 +365,7 @@ public class UserApi {
} }
} }
public void logoutUser () throws ApiException { public void logoutUser () throws ApiException {
Object postBody = null;
// create path and map variables // create path and map variables
String path = "/user/logout".replaceAll("\\{format\\}","json"); String path = "/user/logout".replaceAll("\\{format\\}","json");
@ -169,72 +373,30 @@ public class UserApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = null;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, null, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "GET", queryParams, postBody, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
public void createUsersWithArrayInput (List<User> body) throws ApiException {
// verify required params are set
if(body == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/user/createWithArray".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType);
if(response != null){
return ;
}
else {
return ;
}
} catch (ApiException ex) {
if(ex.getCode() == 404) {
return ;
}
else {
throw ex;
}
}
}
public void createUsersWithListInput (List<User> body) throws ApiException {
// verify required params are set
if(body == null ) {
throw new ApiException(400, "missing required params");
}
// create path and map variables
String path = "/user/createWithList".replaceAll("\\{format\\}","json");
// query params
Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json";
try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType);
if(response != null){ if(response != null){
return ; return ;
} }
@ -251,6 +413,7 @@ public class UserApi {
} }
} }
public void createUser (User body) throws ApiException { public void createUser (User body) throws ApiException {
Object postBody = body;
// verify required params are set // verify required params are set
if(body == null ) { if(body == null ) {
throw new ApiException(400, "missing required params"); throw new ApiException(400, "missing required params");
@ -262,10 +425,30 @@ public class UserApi {
Map<String, String> queryParams = new HashMap<String, String>(); Map<String, String> queryParams = new HashMap<String, String>();
Map<String, String> headerParams = new HashMap<String, String>(); Map<String, String> headerParams = new HashMap<String, String>();
String contentType = "application/json"; String[] contentTypes = {
"application/json"};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
if(hasFields)
postBody = builder;
}
else {
postBody = body;
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, body, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "POST", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return ; return ;
} }

View File

@ -4,7 +4,6 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.*; import java.util.*;
import com.wordnik.petstore.model.Category; import com.wordnik.petstore.model.Category;
import com.wordnik.petstore.model.Tag;
public class Pet { public class Pet {
/* unique identifier for the pet */ /* unique identifier for the pet */
@JsonProperty("id") @JsonProperty("id")

View File

@ -8,6 +8,14 @@ import {{invokerPackage}}.ApiInvoker;
import java.util.*; import java.util.*;
import java.io.File; import java.io.File;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.mime.content.*;
import org.apache.http.entity.ContentType;
import android.webkit.MimeTypeMap;
{{#operations}} {{#operations}}
public class {{classname}} { public class {{classname}} {
String basePath = "{{basePath}}"; String basePath = "{{basePath}}";
@ -29,12 +37,20 @@ public class {{classname}} {
return basePath; return basePath;
} }
private static String getMimeType(File file) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
int index = file.getName().lastIndexOf('.')+1;
String ext = file.getName().substring(index).toLowerCase();
return mime.getMimeTypeFromExtension(ext);
}
{{#operation}} {{#operation}}
{{#errorList}} //error info- code: {{code}} reason: "{{reason}}" model: {{#responseModel}}{{responseModel}} {{#errorList}} //error info- code: {{code}} reason: "{{reason}}" model: {{#responseModel}}{{responseModel}}
{{/responseModel}}{{^responseModel}}<none> {{/responseModel}}{{^responseModel}}<none>
{{/responseModel}} {{/responseModel}}
{{/errorList}} {{/errorList}}
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}} {{nickname}} ({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException {
Object postBody = {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
{{#requiredParamCount}} {{#requiredParamCount}}
// verify required params are set // verify required params are set
if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) { if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) {
@ -56,10 +72,52 @@ public class {{classname}} {
{{#headerParams}}headerParams.put("{{baseName}}", {{paramName}}); {{#headerParams}}headerParams.put("{{baseName}}", {{paramName}});
{{/headerParams}} {{/headerParams}}
String contentType = "application/json"; String[] contentTypes = {
{{#consumes}}"{{mediaType}}"{{#hasMore}},{{/hasMore}}{{/consumes}}
};
String contentType = contentTypes.length > 0 ? contentTypes[0] : "application/json";
if(contentType.startsWith("application/x-www-form-urlencoded")) {
boolean hasFields = false;
List<NameValuePair> mp = new ArrayList<NameValuePair>();
{{#formParams}}
{{^isFile}}
hasFields = true;
mp.add(new BasicNameValuePair("{{baseName}}", {{paramName}}));
{{/isFile}}
{{/formParams}}
if(hasFields)
postBody = mp;
}
else if(contentType.startsWith("multipart/form-data")) {
boolean hasFields = false;
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
{{#formParams}}
hasFields = true;
{{^isFile}}
builder.addTextBody("{{baseName}}", {{paramName}}.toString());
{{/isFile}}
{{#isFile}}
builder.addBinaryBody("file", {{paramName}}, ContentType.create(getMimeType({{paramName}})), {{paramName}}.getName());
{{/isFile}}
{{/formParams}}
{{#bodyParams}}
{{#isFile}}
hasFields = true;
builder.addBinaryBody("file", {{paramName}}, ContentType.create(getMimeType({{paramName}})), {{paramName}}.getName());
{{/isFile}}
{{/bodyParams}}
if(hasFields)
postBody = builder;
}
else {
postBody = {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
}
try { try {
String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}, headerParams, contentType); String response = apiInvoker.invokeAPI(basePath, path, "{{httpMethod}}", queryParams, postBody, headerParams, contentType);
if(response != null){ if(response != null){
return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}}; return {{#returnType}}({{{returnType}}}) ApiInvoker.deserialize(response, "{{returnContainer}}", {{returnBaseType}}.class){{/returnType}};
} }

View File

@ -12,7 +12,9 @@ import org.apache.http.client.methods.*;
import org.apache.http.conn.*; import org.apache.http.conn.*;
import org.apache.http.conn.scheme.*; import org.apache.http.conn.scheme.*;
import org.apache.http.conn.ssl.*; import org.apache.http.conn.ssl.*;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.StringEntity; import org.apache.http.entity.StringEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.impl.client.*; import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.*; import org.apache.http.impl.conn.*;
import org.apache.http.params.*; import org.apache.http.params.*;
@ -156,10 +158,23 @@ public class ApiInvoker {
} }
else if ("POST".equals(method)) { else if ("POST".equals(method)) {
HttpPost post = new HttpPost(url); HttpPost post = new HttpPost(url);
if (body != null) { if (body != null) {
post.setHeader("Content-Type", contentType); if("application/x-www-form-urlencoded".equals(contentType)) {
post.setEntity(new StringEntity(serialize(body), "UTF-8")); post.setHeader("Content-Type", contentType);
post.setEntity(new UrlEncodedFormEntity((List<NameValuePair>)body));
}
else if("multipart/form-data".equals(contentType)) {
String boundary = "-------------" + System.currentTimeMillis();
post.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
MultipartEntityBuilder builder = (MultipartEntityBuilder)body;
builder.setBoundary(boundary);
post.setEntity(builder.build());
}
else {
post.setHeader("Content-Type", contentType);
post.setEntity(new StringEntity(serialize(body), "UTF-8"));
}
} }
for(String key : headers.keySet()) { for(String key : headers.keySet()) {
post.setHeader(key, headers.get(key)); post.setHeader(key, headers.get(key));
@ -169,8 +184,21 @@ public class ApiInvoker {
else if ("PUT".equals(method)) { else if ("PUT".equals(method)) {
HttpPut put = new HttpPut(url); HttpPut put = new HttpPut(url);
if(body != null) { if(body != null) {
put.setHeader("Content-Type", contentType); if("application/x-www-form-urlencoded".equals(contentType)) {
put.setEntity(new StringEntity(serialize(body), "UTF-8")); put.setHeader("Content-Type", contentType);
put.setEntity(new UrlEncodedFormEntity((List<NameValuePair>)body));
}
else if("multipart/form-data".equals(contentType)) {
String boundary = "-------------" + System.currentTimeMillis();
put.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
MultipartEntityBuilder builder = (MultipartEntityBuilder)body;
builder.setBoundary(boundary);
put.setEntity(builder.build());
}
else {
put.setHeader("Content-Type", contentType);
put.setEntity(new StringEntity(serialize(body), "UTF-8"));
}
} }
for(String key : headers.keySet()) { for(String key : headers.keySet()) {
put.setHeader(key, headers.get(key)); put.setHeader(key, headers.get(key));

View File

@ -131,6 +131,24 @@
<version>${httpclient-version}</version> <version>${httpclient-version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpclient-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpclient-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>compile</scope>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>
@ -146,6 +164,6 @@
<junit-version>4.8.1</junit-version> <junit-version>4.8.1</junit-version>
<maven-plugin-version>1.0.0</maven-plugin-version> <maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.8.1</junit-version> <junit-version>4.8.1</junit-version>
<httpclient-version>4.0</httpclient-version> <httpclient-version>4.3</httpclient-version>
</properties> </properties>
</project> </project>

View File

@ -122,7 +122,7 @@ class Codegen(config: CodegenConfig) {
params += "hasMore" -> "true" params += "hasMore" -> "true"
params += "allowMultiple" -> param.allowMultiple.toString params += "allowMultiple" -> param.allowMultiple.toString
if(param.dataType == "File") params += "isFile" -> "true" if(param.dataType.toLowerCase() == "file") params += "isFile" -> "true"
else params += "notFile" -> "true" else params += "notFile" -> "true"
val u = param.dataType.indexOf("[") match { val u = param.dataType.indexOf("[") match {
@ -155,7 +155,6 @@ class Codegen(config: CodegenConfig) {
if (!param.required) { if (!param.required) {
bodyParamRequired = None bodyParamRequired = None
} }
bodyParam = Some("body") bodyParam = Some("body")
bodyParams += params.clone bodyParams += params.clone
} }