Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a request decorator #422

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions lib/src/test/resources/example-union-types-ning-client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,21 @@ package io.apibuilder.example.union.types.v0 {

object Users extends Users {
override def get(
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: _root_.com.ning.http.client.RequestBuilder => _root_.com.ning.http.client.RequestBuilder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[Seq[io.apibuilder.example.union.types.v0.models.User]] = {
_executeRequest("GET", s"/users", requestHeaders = requestHeaders).map {
_executeRequest("GET", s"/users", requestHeaders = requestHeaders, requestModifier = requestModifier).map {
case r if r.getStatusCode == 200 => _root_.io.apibuilder.example.union.types.v0.Client.parseJson("Seq[io.apibuilder.example.union.types.v0.models.User]", r, _.validate[Seq[io.apibuilder.example.union.types.v0.models.User]])
case r => throw io.apibuilder.example.union.types.v0.errors.FailedRequest(r.getStatusCode, s"Unsupported response code[${r.getStatusCode}]. Expected: 200", requestUri = Some(r.getUri))
}
}

override def getByGuid(
guid: _root_.java.util.UUID,
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: _root_.com.ning.http.client.RequestBuilder => _root_.com.ning.http.client.RequestBuilder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.apibuilder.example.union.types.v0.models.User] = {
_executeRequest("GET", s"/users/${guid}", requestHeaders = requestHeaders).map {
_executeRequest("GET", s"/users/${guid}", requestHeaders = requestHeaders, requestModifier = requestModifier).map {
case r if r.getStatusCode == 200 => _root_.io.apibuilder.example.union.types.v0.Client.parseJson("io.apibuilder.example.union.types.v0.models.User", r, _.validate[io.apibuilder.example.union.types.v0.models.User])
case r if r.getStatusCode == 404 => throw io.apibuilder.example.union.types.v0.errors.UnitResponse(r.getStatusCode)
case r => throw io.apibuilder.example.union.types.v0.errors.FailedRequest(r.getStatusCode, s"Unsupported response code[${r.getStatusCode}]. Expected: 200, 404", requestUri = Some(r.getUri))
Expand All @@ -394,11 +396,12 @@ package io.apibuilder.example.union.types.v0 {

override def post(
user: io.apibuilder.example.union.types.v0.models.User,
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: _root_.com.ning.http.client.RequestBuilder => _root_.com.ning.http.client.RequestBuilder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.apibuilder.example.union.types.v0.models.User] = {
val payload = play.api.libs.json.Json.toJson(user)

_executeRequest("POST", s"/users", body = Some(payload), requestHeaders = requestHeaders).map {
_executeRequest("POST", s"/users", body = Some(payload), requestHeaders = requestHeaders, requestModifier = requestModifier).map {
case r if r.getStatusCode == 201 => _root_.io.apibuilder.example.union.types.v0.Client.parseJson("io.apibuilder.example.union.types.v0.models.User", r, _.validate[io.apibuilder.example.union.types.v0.models.User])
case r => throw io.apibuilder.example.union.types.v0.errors.FailedRequest(r.getStatusCode, s"Unsupported response code[${r.getStatusCode}]. Expected: 201", requestUri = Some(r.getUri))
}
Expand All @@ -417,8 +420,8 @@ package io.apibuilder.example.union.types.v0 {
}
}

def _requestBuilder(method: String, path: String, requestHeaders: Seq[(String, String)]): RequestBuilder = {
val builder = new RequestBuilder(method)
def _requestBuilder(method: String, path: String, requestHeaders: Seq[(String, String)]): _root_.com.ning.http.client.RequestBuilder = {
val builder = new _root_.com.ning.http.client.RequestBuilder(method)
.setUrl(baseUrl + path)
.addHeader("User-Agent", Constants.UserAgent)
.addHeader("X-Apidoc-Version", Constants.Version)
Expand Down Expand Up @@ -447,7 +450,8 @@ package io.apibuilder.example.union.types.v0 {
path: String,
queryParameters: Seq[(String, String)] = Nil,
requestHeaders: Seq[(String, String)] = Nil,
body: Option[play.api.libs.json.JsValue] = None
body: Option[play.api.libs.json.JsValue] = None,
requestModifier: _root_.com.ning.http.client.RequestBuilder => _root_.com.ning.http.client.RequestBuilder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[com.ning.http.client.Response] = {
val allHeaders = body match {
case None => requestHeaders
Expand All @@ -465,7 +469,7 @@ package io.apibuilder.example.union.types.v0 {
request.setBody(serialized)
}

val finalRequest = requestWithParamsAndBody.build()
val finalRequest = requestModifier(requestWithParamsAndBody).build()
_logRequest(finalRequest)

val result = scala.concurrent.Promise[com.ning.http.client.Response]()
Expand Down Expand Up @@ -535,17 +539,20 @@ package io.apibuilder.example.union.types.v0 {

trait Users {
def get(
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: _root_.com.ning.http.client.RequestBuilder => _root_.com.ning.http.client.RequestBuilder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[Seq[io.apibuilder.example.union.types.v0.models.User]]

def getByGuid(
guid: _root_.java.util.UUID,
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: _root_.com.ning.http.client.RequestBuilder => _root_.com.ning.http.client.RequestBuilder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.apibuilder.example.union.types.v0.models.User]

def post(
user: io.apibuilder.example.union.types.v0.models.User,
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: _root_.com.ning.http.client.RequestBuilder => _root_.com.ning.http.client.RequestBuilder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.apibuilder.example.union.types.v0.models.User]
}

Expand Down
55 changes: 35 additions & 20 deletions lib/src/test/resources/example-union-types-play-23.txt
Original file line number Diff line number Diff line change
Expand Up @@ -488,19 +488,21 @@ package io.apibuilder.example.union.types.v0 {

object Users extends Users {
override def get(
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: play.api.libs.ws.WSRequestHolder => play.api.libs.ws.WSRequestHolder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[Seq[io.apibuilder.example.union.types.v0.models.User]] = {
_executeRequest("GET", s"/users", requestHeaders = requestHeaders).map {
_executeRequest("GET", s"/users", requestHeaders = requestHeaders, requestModifier = requestModifier).map {
case r if r.status == 200 => _root_.io.apibuilder.example.union.types.v0.Client.parseJson("Seq[io.apibuilder.example.union.types.v0.models.User]", r, _.validate[Seq[io.apibuilder.example.union.types.v0.models.User]])
case r => throw io.apibuilder.example.union.types.v0.errors.FailedRequest(r.status, s"Unsupported response code[${r.status}]. Expected: 200")
}
}

override def getByGuid(
guid: _root_.java.util.UUID,
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: play.api.libs.ws.WSRequestHolder => play.api.libs.ws.WSRequestHolder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.apibuilder.example.union.types.v0.models.User] = {
_executeRequest("GET", s"/users/${guid}", requestHeaders = requestHeaders).map {
_executeRequest("GET", s"/users/${guid}", requestHeaders = requestHeaders, requestModifier = requestModifier).map {
case r if r.status == 200 => _root_.io.apibuilder.example.union.types.v0.Client.parseJson("io.apibuilder.example.union.types.v0.models.User", r, _.validate[io.apibuilder.example.union.types.v0.models.User])
case r if r.status == 404 => throw io.apibuilder.example.union.types.v0.errors.UnitResponse(r.status)
case r => throw io.apibuilder.example.union.types.v0.errors.FailedRequest(r.status, s"Unsupported response code[${r.status}]. Expected: 200, 404")
Expand All @@ -509,11 +511,12 @@ package io.apibuilder.example.union.types.v0 {

override def post(
user: io.apibuilder.example.union.types.v0.models.User,
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: play.api.libs.ws.WSRequestHolder => play.api.libs.ws.WSRequestHolder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.apibuilder.example.union.types.v0.models.User] = {
val payload = play.api.libs.json.Json.toJson(user)

_executeRequest("POST", s"/users", body = Some(payload), requestHeaders = requestHeaders).map {
_executeRequest("POST", s"/users", body = Some(payload), requestHeaders = requestHeaders, requestModifier = requestModifier).map {
case r if r.status == 201 => _root_.io.apibuilder.example.union.types.v0.Client.parseJson("io.apibuilder.example.union.types.v0.models.User", r, _.validate[io.apibuilder.example.union.types.v0.models.User])
case r => throw io.apibuilder.example.union.types.v0.errors.FailedRequest(r.status, s"Unsupported response code[${r.status}]. Expected: 201")
}
Expand Down Expand Up @@ -553,32 +556,41 @@ package io.apibuilder.example.union.types.v0 {
path: String,
queryParameters: Seq[(String, String)] = Nil,
requestHeaders: Seq[(String, String)] = Nil,
body: Option[play.api.libs.json.JsValue] = None
body: Option[play.api.libs.json.JsValue] = None,
requestModifier: play.api.libs.ws.WSRequestHolder => play.api.libs.ws.WSRequestHolder = identity
): scala.concurrent.Future[play.api.libs.ws.WSResponse] = {
method.toUpperCase match {
case "GET" => {
_logRequest("GET", _requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*)).get()
val request = requestModifier(_requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*))
_logRequest("GET", request).get()
}
case "POST" => {
_logRequest("POST", _requestHolder(path).withHeaders(_withJsonContentType(requestHeaders):_*).withQueryString(queryParameters:_*)).post(body.getOrElse(play.api.libs.json.Json.obj()))
val request = requestModifier(_requestHolder(path).withHeaders(_withJsonContentType(requestHeaders):_*).withQueryString(queryParameters:_*))
_logRequest("POST", request).post(body.getOrElse(play.api.libs.json.Json.obj()))
}
case "PUT" => {
_logRequest("PUT", _requestHolder(path).withHeaders(_withJsonContentType(requestHeaders):_*).withQueryString(queryParameters:_*)).put(body.getOrElse(play.api.libs.json.Json.obj()))
val request = requestModifier(_requestHolder(path).withHeaders(_withJsonContentType(requestHeaders):_*).withQueryString(queryParameters:_*))
_logRequest("PUT", request).put(body.getOrElse(play.api.libs.json.Json.obj()))
}
case "PATCH" => {
_logRequest("PATCH", _requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*)).patch(body.getOrElse(play.api.libs.json.Json.obj()))
val request = requestModifier(_requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*))
_logRequest("PATCH", request).patch(body.getOrElse(play.api.libs.json.Json.obj()))
}
case "DELETE" => {
_logRequest("DELETE", _requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*)).delete()
val request = requestModifier(_requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*))
_logRequest("DELETE", request).delete()
}
case "HEAD" => {
_logRequest("HEAD", _requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*)).head()
case "HEAD" => {
val request = requestModifier(_requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*))
_logRequest("HEAD", request).head()
}
case "OPTIONS" => {
_logRequest("OPTIONS", _requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*)).options()
case "OPTIONS" => {
val request = requestModifier(_requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*))
_logRequest("OPTIONS", request).options()
}
case _ => {
_logRequest(method, _requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*))
val request = requestModifier(_requestHolder(path).withHeaders(requestHeaders:_*).withQueryString(queryParameters:_*))
_logRequest(method, request)
sys.error("Unsupported method[%s]".format(method))
}
}
Expand Down Expand Up @@ -630,17 +642,20 @@ package io.apibuilder.example.union.types.v0 {

trait Users {
def get(
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: play.api.libs.ws.WSRequestHolder => play.api.libs.ws.WSRequestHolder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[Seq[io.apibuilder.example.union.types.v0.models.User]]

def getByGuid(
guid: _root_.java.util.UUID,
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: play.api.libs.ws.WSRequestHolder => play.api.libs.ws.WSRequestHolder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.apibuilder.example.union.types.v0.models.User]

def post(
user: io.apibuilder.example.union.types.v0.models.User,
requestHeaders: Seq[(String, String)] = Nil
requestHeaders: Seq[(String, String)] = Nil,
requestModifier: play.api.libs.ws.WSRequestHolder => play.api.libs.ws.WSRequestHolder = identity
)(implicit ec: scala.concurrent.ExecutionContext): scala.concurrent.Future[io.apibuilder.example.union.types.v0.models.User]
}

Expand Down
Loading