Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,42 @@ Configure {{name}}:
{{/isApiKey}}
{{#isBasic}}
{{#isBasicBasic}}
Configure {{name}}:
ApiClient.username = ""
ApiClient.password = ""
Configure {{name}} statically:
```kotlin
ApiClient.username = ""
ApiClient.password = ""
```
{{#jvm-okhttp}}
Configure {{name}} dynamically:
```kotlin
apiInstance.userCredentialProvider = { "user" to "pass" }
```
{{/jvm-okhttp}}
{{/isBasicBasic}}
{{#isBasicBearer}}
Configure {{name}}:
ApiClient.accessToken = ""
Configure {{name}} statically:
```kotlin
ApiClient.accessToken = ""
```
{{#jvm-okhttp}}
Configure {{name}} dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```
{{/jvm-okhttp}}
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}
Configure {{name}}:
ApiClient.accessToken = ""
Configure {{name}} statically:
```kotlin
ApiClient.accessToken = ""
```
{{#jvm-okhttp}}
Configure {{name}} dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```
{{/jvm-okhttp}}
{{/isOAuth}}
{{/authMethods}}

Expand All @@ -89,4 +113,4 @@ Configure {{name}}:
- **Accept**: {{#produces}}{{{mediaType}}}{{^-last}}, {{/-last}}{{/produces}}{{^produces}}Not defined{{/produces}}

{{/operation}}
{{/operations}}
{{/operations}}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ import com.squareup.moshi.adapter
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}

{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
{{^nonPublicApi}}{{#explicitApi}}public {{/explicitApi}}{{/nonPublicApi}}var accessTokenProvider: () -> String? = { accessToken }

/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
Expand Down Expand Up @@ -460,25 +463,26 @@ import com.squareup.moshi.adapter
{{#isBasic}}
{{#isBasicBasic}}
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
username?.let { username ->
password?.let { password ->
requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(username, password)
val (user, passw) = userCredentialsProvider()
user?.let { user ->
passw?.let { passw ->
requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(user, passw)
}
}
}
{{/isBasicBasic}}
{{#isBasicBearer}}
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
accessToken?.let { accessToken ->
requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken"
accessTokenProvider()?.let { token ->
requestConfig.headers[AUTHORIZATION] = "Bearer $token"
}
}
{{/isBasicBearer}}
{{/isBasic}}
{{#isOAuth}}
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
accessToken?.let { accessToken ->
requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken "
accessTokenProvider()?.let { token ->
requestConfig.headers[AUTHORIZATION] = "Bearer $token "
}
}
{{/isOAuth}}
Expand Down
22 changes: 17 additions & 5 deletions samples/client/echo_api/kotlin-jvm-okhttp/docs/AuthApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ This endpoint does not need any parameter.
### Authorization


Configure http_auth:
ApiClient.username = ""
ApiClient.password = ""
Configure http_auth statically:
```kotlin
ApiClient.username = ""
ApiClient.password = ""
```
Configure http_auth dynamically:
```kotlin
apiInstance.userCredentialProvider = { "user" to "pass" }
```

### HTTP request headers

Expand Down Expand Up @@ -91,8 +97,14 @@ This endpoint does not need any parameter.
### Authorization


Configure http_bearer_auth:
ApiClient.accessToken = ""
Configure http_bearer_auth statically:
```kotlin
ApiClient.accessToken = ""
```
Configure http_bearer_auth dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```

### HTTP request headers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}

var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
var accessTokenProvider: () -> String? = { accessToken }

/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
Expand Down Expand Up @@ -357,15 +360,16 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie

protected fun <T> updateAuthParams(requestConfig: RequestConfig<T>) {
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
username?.let { username ->
password?.let { password ->
requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(username, password)
val (user, passw) = userCredentialsProvider()
user?.let { user ->
passw?.let { passw ->
requestConfig.headers[AUTHORIZATION] = okhttp3.Credentials.basic(user, passw)
}
}
}
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
accessToken?.let { accessToken ->
requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken"
accessTokenProvider()?.let { token ->
requestConfig.headers[AUTHORIZATION] = "Bearer $token"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ This endpoint does not need any parameter.
### Authorization


Configure http_auth:
ApiClient.username = ""
ApiClient.password = ""
Configure http_auth statically:
```kotlin
ApiClient.username = ""
ApiClient.password = ""
```

### HTTP request headers

Expand Down Expand Up @@ -91,8 +93,10 @@ This endpoint does not need any parameter.
### Authorization


Configure http_bearer_auth:
ApiClient.accessToken = ""
Configure http_bearer_auth statically:
```kotlin
ApiClient.accessToken = ""
```

### HTTP request headers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ This endpoint does not need any parameter.
### Authorization


Configure http_auth:
ApiClient.username = ""
ApiClient.password = ""
Configure http_auth statically:
```kotlin
ApiClient.username = ""
ApiClient.password = ""
```

### HTTP request headers

Expand Down Expand Up @@ -91,8 +93,10 @@ This endpoint does not need any parameter.
### Authorization


Configure http_bearer_auth:
ApiClient.accessToken = ""
Configure http_bearer_auth statically:
```kotlin
ApiClient.accessToken = ""
```

### HTTP request headers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}

var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
var accessTokenProvider: () -> String? = { accessToken }

/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,14 @@ try {
### Authorization


Configure petstore_auth:
ApiClient.accessToken = ""
Configure petstore_auth statically:
```kotlin
ApiClient.accessToken = ""
```
Configure petstore_auth dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```

### HTTP request headers

Expand Down Expand Up @@ -103,8 +109,14 @@ null (empty response body)
### Authorization


Configure petstore_auth:
ApiClient.accessToken = ""
Configure petstore_auth statically:
```kotlin
ApiClient.accessToken = ""
```
Configure petstore_auth dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```

### HTTP request headers

Expand Down Expand Up @@ -151,8 +163,14 @@ try {
### Authorization


Configure petstore_auth:
ApiClient.accessToken = ""
Configure petstore_auth statically:
```kotlin
ApiClient.accessToken = ""
```
Configure petstore_auth dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```

### HTTP request headers

Expand Down Expand Up @@ -199,8 +217,14 @@ try {
### Authorization


Configure petstore_auth:
ApiClient.accessToken = ""
Configure petstore_auth statically:
```kotlin
ApiClient.accessToken = ""
```
Configure petstore_auth dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```

### HTTP request headers

Expand Down Expand Up @@ -296,8 +320,14 @@ try {
### Authorization


Configure petstore_auth:
ApiClient.accessToken = ""
Configure petstore_auth statically:
```kotlin
ApiClient.accessToken = ""
```
Configure petstore_auth dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```

### HTTP request headers

Expand Down Expand Up @@ -347,8 +377,14 @@ null (empty response body)
### Authorization


Configure petstore_auth:
ApiClient.accessToken = ""
Configure petstore_auth statically:
```kotlin
ApiClient.accessToken = ""
```
Configure petstore_auth dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```

### HTTP request headers

Expand Down Expand Up @@ -399,8 +435,14 @@ try {
### Authorization


Configure petstore_auth:
ApiClient.accessToken = ""
Configure petstore_auth statically:
```kotlin
ApiClient.accessToken = ""
```
Configure petstore_auth dynamically:
```kotlin
apiInstance.accessTokenProvider = { "" }
```

### HTTP request headers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}

var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
var accessTokenProvider: () -> String? = { accessToken }

/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
Expand Down Expand Up @@ -357,8 +360,8 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie

protected fun <T> updateAuthParams(requestConfig: RequestConfig<T>) {
if (requestConfig.headers[AUTHORIZATION].isNullOrEmpty()) {
accessToken?.let { accessToken ->
requestConfig.headers[AUTHORIZATION] = "Bearer $accessToken "
accessTokenProvider()?.let { token ->
requestConfig.headers[AUTHORIZATION] = "Bearer $token "
}
}
if (requestConfig.headers["api_key"].isNullOrEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ open class ApiClient(val baseUrl: String, val client: Call.Factory = defaultClie
val builder: OkHttpClient.Builder = OkHttpClient.Builder()
}

var userCredentialsProvider: () -> Pair<String?, String?> = { username to password }
var accessTokenProvider: () -> String? = { accessToken }

/**
* Guess Content-Type header from the given byteArray (defaults to "application/octet-stream").
*
Expand Down
Loading