geetikagupta16 1e4f30eb16 Fixed bug for handling optional header parameters (#3776)
* Fixed #3774
Refactored code to handle optional header parameters.

* Changed null check with pattern matching
2016-09-15 23:28:01 +08:00

68 lines
2.6 KiB
Plaintext

package {{package}}
{{#imports}}import {{import}}
{{/imports}}
import com.wordnik.swagger.client._
import scala.concurrent.Future
import collection.mutable
{{#operations}}
class {{classname}}(client: TransportClient, config: SwaggerConfig) extends ApiClient(client, config) {
{{#operation}}
def {{operationId}}({{#allParams}}{{^required}}{{paramName}}: Option[{{dataType}}] = {{#defaultValue}}Some({{defaultValue}}){{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}{{#hasMore}},{{/hasMore}}
{{/required}}{{#required}}{{paramName}}: {{dataType}}{{#defaultValue}} = {{{defaultValue}}}{{/defaultValue}}{{#hasMore}},
{{/hasMore}}{{/required}}{{/allParams}})(implicit reader: ClientResponseReader[{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}Unit{{/returnType}}]{{#bodyParams}}, writer: RequestWriter[{{dataType}}]{{/bodyParams}}){{#returnType}}: Future[{{returnType}}]{{/returnType}}{{^returnType}}: Future[Unit]{{/returnType}} = {
// create path and map variables
val path = (addFmt("{{path}}"){{#pathParams}}
replaceAll ("\\{" + "{{baseName}}" + "\\}",{{paramName}}.toString){{/pathParams}})
// query params
val queryParams = new mutable.HashMap[String, String]
val headerParams = new mutable.HashMap[String, String]
{{#allParams}}
{{#required}}
{{^isPrimitiveType}}
if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}")
{{/isPrimitiveType}}
{{#isString}}
if ({{paramName}} == null) throw new Exception("Missing required parameter '{{paramName}}' when calling {{classname}}->{{operationId}}")
{{/isString}}
{{/required}}
{{/allParams}}
{{#queryParams}}
{{^required}}
{{paramName}} match {
case Some(param) => queryParams += "{{baseName}}" -> param.toString
case _ => queryParams
}
{{/required}}
{{#required}}
queryParams += "{{baseName}}" -> {{paramName}}.toString
{{/required}}
{{/queryParams}}
{{#headerParams}}
{{^required}}
{{paramName}} match {
case Some(param) => headerParams += "{{baseName}}" -> param.toString
case _ => headerParams
}
{{/required}}
{{#required}}
headerParams += "{{baseName}}" -> {{paramName}}.toString
{{/required}}
{{/headerParams}}
val resFuture = client.submit("{{httpMethod}}", path, queryParams.toMap, headerParams.toMap, {{#bodyParam}}writer.write({{paramName}}){{/bodyParam}}{{^bodyParam}}"{{emptyBodyParam}}"{{/bodyParam}})
resFuture flatMap { resp =>
process(reader.read(resp))
}
}
{{/operation}}
}
{{/operations}}