Skip to content
This repository has been archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
Fix Jetpack tunnel ktlint violations
Browse files Browse the repository at this point in the history
  • Loading branch information
aforcier committed Mar 19, 2018
1 parent 767772f commit 1185f6a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ class MockedStack_JetpackTunnelTest : MockedStack_Base() {
},
BaseErrorListener {
error -> run {
throw AssertionError("Unexpected BaseNetworkError: "
+ (error as WPComGsonNetworkError).apiError + " - " + error.message)
throw AssertionError("Unexpected BaseNetworkError: " +
(error as WPComGsonNetworkError).apiError + " - " + error.message)
}
})

Expand Down Expand Up @@ -105,8 +105,8 @@ class MockedStack_JetpackTunnelTest : MockedStack_Base() {
},
BaseErrorListener {
error -> run {
throw AssertionError("Unexpected BaseNetworkError: "
+ (error as WPComGsonNetworkError).apiError + " - " + error.message)
throw AssertionError("Unexpected BaseNetworkError: " +
(error as WPComGsonNetworkError).apiError + " - " + error.message)
}
})

Expand All @@ -115,9 +115,12 @@ class MockedStack_JetpackTunnelTest : MockedStack_Base() {
}

@Singleton
class JetpackTunnelClientForTests @Inject constructor(appContext: Context, dispatcher: Dispatcher,
requestQueue: RequestQueue, accessToken: AccessToken,
userAgent: UserAgent
class JetpackTunnelClientForTests @Inject constructor(
appContext: Context,
dispatcher: Dispatcher,
requestQueue: RequestQueue,
accessToken: AccessToken,
userAgent: UserAgent
) : BaseWPComRestClient(appContext, dispatcher, requestQueue, accessToken, userAgent) {
/**
* Wraps and exposes the protected [add] method so that tests can add requests directly.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,13 @@ object JetpackTunnelGsonRequest {
*
* @param T the expected response object from the WP-API endpoint
*/
fun <T : Any> buildGetRequest(wpApiEndpoint: String, siteId: Long, params: Map<String, String>,
type: Type, listener: (T?) -> Unit, errorListener: BaseErrorListener
fun <T : Any> buildGetRequest(
wpApiEndpoint: String,
siteId: Long,
params: Map<String, String>,
type: Type,
listener: (T?) -> Unit,
errorListener: BaseErrorListener
): WPComGsonRequest<JetpackTunnelResponse<T>>? {
val wrappedParams = createTunnelParams(params, wpApiEndpoint)

Expand All @@ -126,8 +131,13 @@ object JetpackTunnelGsonRequest {
*
* @param T the expected response object from the WP-API endpoint
*/
fun <T : Any> buildPostRequest(wpApiEndpoint: String, siteId: Long, body: Map<String, Any>,
type: Type, listener: (T?) -> Unit, errorListener: BaseErrorListener
fun <T : Any> buildPostRequest(
wpApiEndpoint: String,
siteId: Long,
body: Map<String, Any>,
type: Type,
listener: (T?) -> Unit,
errorListener: BaseErrorListener
): WPComGsonRequest<JetpackTunnelResponse<T>>? {
val wrappedBody = createTunnelBody(method = "post", body = body, path = wpApiEndpoint)
return buildWrappedPostRequest(siteId, wrappedBody, type, listener, errorListener)
Expand All @@ -145,8 +155,13 @@ object JetpackTunnelGsonRequest {
*
* @param T the expected response object from the WP-API endpoint
*/
fun <T : Any> buildPatchRequest(wpApiEndpoint: String, siteId: Long, body: Map<String, Any>,
type: Type, listener: (T?) -> Unit, errorListener: BaseErrorListener
fun <T : Any> buildPatchRequest(
wpApiEndpoint: String,
siteId: Long,
body: Map<String, Any>,
type: Type,
listener: (T?) -> Unit,
errorListener: BaseErrorListener
): WPComGsonRequest<JetpackTunnelResponse<T>>? {
val wrappedBody = createTunnelBody(method = "patch", body = body, path = wpApiEndpoint)
return buildWrappedPostRequest(siteId, wrappedBody, type, listener, errorListener)
Expand All @@ -164,8 +179,13 @@ object JetpackTunnelGsonRequest {
*
* @param T the expected response object from the WP-API endpoint
*/
fun <T : Any> buildPutRequest(wpApiEndpoint: String, siteId: Long, body: Map<String, Any>,
type: Type, listener: (T?) -> Unit, errorListener: BaseErrorListener
fun <T : Any> buildPutRequest(
wpApiEndpoint: String,
siteId: Long,
body: Map<String, Any>,
type: Type,
listener: (T?) -> Unit,
errorListener: BaseErrorListener
): WPComGsonRequest<JetpackTunnelResponse<T>>? {
val wrappedBody = createTunnelBody(method = "put", body = body, path = wpApiEndpoint)
return buildWrappedPostRequest(siteId, wrappedBody, type, listener, errorListener)
Expand All @@ -183,15 +203,24 @@ object JetpackTunnelGsonRequest {
*
* @param T the expected response object from the WP-API endpoint
*/
fun <T : Any> buildDeleteRequest(wpApiEndpoint: String, siteId: Long, params: Map<String, String>,
type: Type, listener: (T?) -> Unit, errorListener: BaseErrorListener
fun <T : Any> buildDeleteRequest(
wpApiEndpoint: String,
siteId: Long,
params: Map<String, String>,
type: Type,
listener: (T?) -> Unit,
errorListener: BaseErrorListener
): WPComGsonRequest<JetpackTunnelResponse<T>>? {
val wrappedBody = createTunnelBody(method = "delete", params = params, path = wpApiEndpoint)
return buildWrappedPostRequest(siteId, wrappedBody, type, listener, errorListener)
}

private fun <T : Any> buildWrappedPostRequest(siteId: Long, wrappedBody: Map<String, Any>, type: Type,
listener: (T?) -> Unit, errorListener: BaseErrorListener
private fun <T : Any> buildWrappedPostRequest(
siteId: Long,
wrappedBody: Map<String, Any>,
type: Type,
listener: (T?) -> Unit,
errorListener: BaseErrorListener
): WPComGsonRequest<JetpackTunnelResponse<T>>? {
val tunnelRequestUrl = getTunnelApiUrl(siteId)
val wrappedType = TypeToken.getParameterized(JetpackTunnelResponse::class.java, type).type
Expand All @@ -212,8 +241,12 @@ object JetpackTunnelGsonRequest {
return finalParams
}

private fun createTunnelBody(method: String, body: Map<String, Any> = mapOf(),
params: Map<String, String> = mapOf(), path: String): MutableMap<String, Any> {
private fun createTunnelBody(
method: String,
body: Map<String, Any> = mapOf(),
params: Map<String, String> = mapOf(),
path: String
): MutableMap<String, Any> {
val finalBody = mutableMapOf<String, Any>()
with(finalBody) {
put("path", buildRestApiPath(path, params, method))
Expand All @@ -226,7 +259,7 @@ object JetpackTunnelGsonRequest {
}

private fun buildRestApiPath(path: String, params: Map<String, String>, method: String): String {
var result = path + "&_method=" + method
var result = "$path&_method=$method"
if (params.isNotEmpty()) {
for (param in params) {
result += "&" + URLEncoder.encode(param.key, "UTF-8") + "=" + URLEncoder.encode(param.value, "UTF-8")
Expand Down

0 comments on commit 1185f6a

Please sign in to comment.