forked from loafle/openapi-generator-original
added file upload support
This commit is contained in:
@@ -4,6 +4,9 @@ package {{package}}
|
||||
{{/imports}}
|
||||
import {{invokerPackage}}.ApiInvoker
|
||||
import {{invokerPackage}}.ApiException
|
||||
|
||||
import java.io.File
|
||||
|
||||
import scala.collection.mutable.HashMap
|
||||
|
||||
{{#operations}}
|
||||
@@ -19,7 +22,16 @@ class {{classname}} {
|
||||
val path = "{{path}}".replaceAll("\\{format\\}","json"){{#pathParams}}.replaceAll("\\{" + "{{baseName}}" + "\\}",apiInvoker.escapeString({{paramName}}))
|
||||
|
||||
{{/pathParams}}
|
||||
|
||||
|
||||
{{newline}}
|
||||
val contentType = {
|
||||
{{#bodyParam}}if({{bodyParam}} != null && {{bodyParam}}.isInstanceOf[File] )
|
||||
"multipart/form-data"
|
||||
else "application/json"
|
||||
{{/bodyParam}}
|
||||
{{^bodyParam}}"application/json"{{/bodyParam}}
|
||||
}
|
||||
|
||||
// query params
|
||||
val queryParams = new HashMap[String, String]
|
||||
val headerParams = new HashMap[String, String]
|
||||
@@ -39,7 +51,7 @@ class {{classname}} {
|
||||
{{/headerParams}}
|
||||
|
||||
try {
|
||||
apiInvoker.invokeApi(basePath, path, "{{httpMethod}}", queryParams.toMap, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}None{{/bodyParam}}, headerParams.toMap) match {
|
||||
apiInvoker.invokeApi(basePath, path, "{{httpMethod}}", queryParams.toMap, {{#bodyParam}}{{bodyParam}}{{/bodyParam}}{{^bodyParam}}None{{/bodyParam}}, headerParams.toMap, contentType) match {
|
||||
case s: String =>
|
||||
{{#returnType}} Some(ApiInvoker.deserialize(s, "{{returnContainer}}", classOf[{{returnBaseType}}]).asInstanceOf[{{returnType}}])
|
||||
{{/returnType}}
|
||||
|
||||
@@ -6,6 +6,10 @@ import com.sun.jersey.api.client.config.ClientConfig
|
||||
import com.sun.jersey.api.client.config.DefaultClientConfig
|
||||
import com.sun.jersey.api.client.filter.LoggingFilter
|
||||
|
||||
import com.sun.jersey.multipart.FormDataMultiPart
|
||||
import com.sun.jersey.multipart.file.FileDataBodyPart
|
||||
|
||||
import java.io.File
|
||||
import java.net.URLEncoder
|
||||
import javax.ws.rs.core.MediaType
|
||||
|
||||
@@ -75,11 +79,11 @@ object ApiInvoker {
|
||||
} else null
|
||||
}
|
||||
|
||||
def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], body: AnyRef, headerParams: Map[String, String]) = {
|
||||
def invokeApi(host: String, path: String, method: String, queryParams: Map[String, String], body: AnyRef, headerParams: Map[String, String], contentType: String) = {
|
||||
val client = getClient(host)
|
||||
|
||||
val querystring = queryParams.filter(k => k._2 != null).map(k => (escapeString(k._1) + "=" + escapeString(k._2))).mkString("?", "&", "")
|
||||
val builder = client.resource(host + path + querystring).`type`("application/json")
|
||||
val builder = client.resource(host + path + querystring).`type`(contentType)
|
||||
|
||||
headerParams.map(p => builder.header(p._1, p._2))
|
||||
defaultHeaders.map(p => {
|
||||
@@ -94,7 +98,16 @@ object ApiInvoker {
|
||||
builder.get(classOf[ClientResponse]).asInstanceOf[ClientResponse]
|
||||
}
|
||||
case "POST" => {
|
||||
builder.post(classOf[ClientResponse], serialize(body))
|
||||
if(body != null && body.isInstanceOf[File]) {
|
||||
val file = body.asInstanceOf[File]
|
||||
val form = new FormDataMultiPart()
|
||||
form.field("filename", file.getName())
|
||||
form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE))
|
||||
builder.post(classOf[ClientResponse], form)
|
||||
}
|
||||
else {
|
||||
builder.post(classOf[ClientResponse], serialize(body))
|
||||
}
|
||||
}
|
||||
case "PUT" => {
|
||||
builder.put(classOf[ClientResponse], serialize(body))
|
||||
@@ -134,3 +147,4 @@ class ApiException extends Exception {
|
||||
this()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,6 @@
|
||||
</prerequisites>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>scala-tools.org</id>
|
||||
<name>Scala-Tools Maven2 Repository</name>
|
||||
<url>http://scala-tools.org/repo-releases</url>
|
||||
</pluginRepository>
|
||||
<pluginRepository>
|
||||
<id>maven-mongodb-plugin-repo</id>
|
||||
<name>maven mongodb plugin repository</name>
|
||||
@@ -161,6 +156,12 @@
|
||||
<version>${jersey-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.jersey.contribs</groupId>
|
||||
<artifactId>jersey-multipart</artifactId>
|
||||
<version>${jersey-version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.scala-lang</groupId>
|
||||
<artifactId>scala-library</artifactId>
|
||||
@@ -196,4 +197,4 @@
|
||||
<junit-version>4.8.1</junit-version>
|
||||
<scala-test-version>1.6.1</scala-test-version>
|
||||
</properties>
|
||||
</project>
|
||||
</project>
|
||||
|
||||
Reference in New Issue
Block a user