-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Enhancement/kotlin/apiclient and auth #6585
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
Merged
wing328
merged 8 commits into
OpenAPITools:master
from
Dataport:enhancement/kotlin/apiclient_and_auth
Jun 11, 2020
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
01aeb85
Add multiple auth methods to ApiClient. ApiClient more configurable
ch4rl3x 971373d
Fixed missing close tag for isMultipart in api.mustache
ch4rl3x 46331ed
Generated samples ./bin/kotlin-client-all.sh
ch4rl3x b647b3d
Generated samples./bin/openapi3/kotlin-client-petstore-all.sh
ch4rl3x 039b25b
Use of better way to add supporting files based on conditions
ch4rl3x ce8f8f5
Merge branch 'master' into enhancement/kotlin/apiclient_and_auth
ch4rl3x b70538f
Fixed missing check for retrofit generating auth files
ch4rl3x 0a44be0
Generated samples
ch4rl3x File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
## Requires | ||
|
||
{{#jvm}} | ||
* Kotlin 1.3.41 | ||
* Kotlin 1.3.61 | ||
* Gradle 4.9 | ||
{{/jvm}} | ||
{{#multiplatform}} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...enapi-generator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/api_doc.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# {{classname}}{{#description}} | ||
{{description}}{{/description}} | ||
|
||
All URIs are relative to *{{basePath}}* | ||
|
||
Method | HTTP request | Description | ||
------------- | ------------- | ------------- | ||
{{#operations}}{{#operation}}[**{{operationId}}**]({{classname}}.md#{{operationId}}) | **{{httpMethod}}** {{path}} | {{#summary}}{{summary}}{{/summary}} | ||
{{/operation}}{{/operations}} | ||
|
||
{{#operations}} | ||
{{#operation}} | ||
|
||
{{summary}}{{#notes}} | ||
|
||
{{notes}}{{/notes}} | ||
|
||
### Example | ||
```kotlin | ||
// Import classes: | ||
//import {{{packageName}}}.* | ||
//import {{{packageName}}}.infrastructure.* | ||
//import {{{modelPackage}}}.* | ||
|
||
val apiClient = ApiClient() | ||
{{#authMethods}} | ||
{{#isBasic}} | ||
{{#isBasicBasic}} | ||
apiClient.setCredentials("USERNAME", "PASSWORD") | ||
{{/isBasicBasic}} | ||
{{#isBasicBearer}} | ||
apiClient.setBearerToken("TOKEN") | ||
{{/isBasicBearer}} | ||
{{/isBasic}} | ||
{{/authMethods}} | ||
val webService = apiClient.createWebservice({{{classname}}}::class.java) | ||
{{#allParams}} | ||
val {{{paramName}}} : {{{dataType}}} = {{{example}}} // {{{dataType}}} | {{{description}}} | ||
{{/allParams}} | ||
|
||
{{#useCoroutines}} | ||
launch(Dispatchers.IO) { | ||
{{/useCoroutines}} | ||
{{#useCoroutines}} {{/useCoroutines}}{{#returnType}}val result : {{{returnType}}}{{#nullableReturnType}}?{{/nullableReturnType}} = {{/returnType}}webService.{{{operationId}}}({{#allParams}}{{{paramName}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) | ||
{{#useCoroutines}} | ||
} | ||
{{/useCoroutines}} | ||
``` | ||
|
||
### Parameters | ||
{{^allParams}}This endpoint does not need any parameter.{{/allParams}}{{#allParams}}{{#-last}} | ||
Name | Type | Description | Notes | ||
------------- | ------------- | ------------- | -------------{{/-last}}{{/allParams}} | ||
{{#allParams}} **{{paramName}}** | {{#isPrimitiveType}}**{{dataType}}**{{/isPrimitiveType}}{{^isPrimitiveType}}{{#isFile}}**{{dataType}}**{{/isFile}}{{^isFile}}{{#generateModelDocs}}[**{{dataType}}**]({{baseType}}.md){{/generateModelDocs}}{{^generateModelDocs}}**{{dataType}}**{{/generateModelDocs}}{{/isFile}}{{/isPrimitiveType}}| {{description}} |{{^required}} [optional]{{/required}}{{#defaultValue}} [default to {{defaultValue}}]{{/defaultValue}}{{#allowableValues}} [enum: {{#values}}{{{.}}}{{^-last}}, {{/-last}}{{/values}}]{{/allowableValues}} | ||
{{/allParams}} | ||
|
||
### Return type | ||
|
||
{{#returnType}}{{#returnTypeIsPrimitive}}**{{returnType}}**{{/returnTypeIsPrimitive}}{{^returnTypeIsPrimitive}}{{#generateModelDocs}}[**{{returnType}}**]({{returnBaseType}}.md){{/generateModelDocs}}{{^generateModelDocs}}**{{returnType}}**{{/generateModelDocs}}{{/returnTypeIsPrimitive}}{{/returnType}}{{^returnType}}null (empty response body){{/returnType}} | ||
|
||
### Authorization | ||
|
||
{{^authMethods}}No authorization required{{/authMethods}} | ||
{{#authMethods}} | ||
{{#isBasic}} | ||
{{#isBasicBasic}} | ||
Configure {{name}}: | ||
ApiClient().setCredentials("USERNAME", "PASSWORD") | ||
{{/isBasicBasic}} | ||
{{#isBasicBearer}} | ||
Configure {{name}}: | ||
ApiClient().setBearerToken("TOKEN") | ||
{{/isBasicBearer}} | ||
{{/isBasic}} | ||
{{/authMethods}} | ||
|
||
### HTTP request headers | ||
|
||
- **Content-Type**: {{#consumes}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/consumes}}{{^consumes}}Not defined{{/consumes}} | ||
- **Accept**: {{#produces}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/produces}}{{^produces}}Not defined{{/produces}} | ||
|
||
{{/operation}} | ||
{{/operations}} |
50 changes: 50 additions & 0 deletions
50
...ator/src/main/resources/kotlin-client/libraries/jvm-retrofit2/auth/ApiKeyAuth.kt.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package {{packageName}}.auth | ||
|
||
import java.io.IOException | ||
import java.net.URI | ||
import java.net.URISyntaxException | ||
|
||
import okhttp3.Interceptor | ||
import okhttp3.Response | ||
|
||
class ApiKeyAuth( | ||
private val location: String = "", | ||
private val paramName: String = "", | ||
private var apiKey: String = "" | ||
) : Interceptor { | ||
|
||
@Throws(IOException::class) | ||
override fun intercept(chain: Interceptor.Chain): Response { | ||
var request = chain.request() | ||
|
||
if ("query" == location) { | ||
var newQuery = request.url.toUri().query | ||
val paramValue = "$paramName=$apiKey" | ||
if (newQuery == null) { | ||
newQuery = paramValue | ||
} else { | ||
newQuery += "&$paramValue" | ||
} | ||
|
||
val newUri: URI | ||
try { | ||
val oldUri = request.url.toUri() | ||
newUri = URI(oldUri.scheme, oldUri.authority, | ||
oldUri.path, newQuery, oldUri.fragment) | ||
} catch (e: URISyntaxException) { | ||
throw IOException(e) | ||
} | ||
|
||
request = request.newBuilder().url(newUri.toURL()).build() | ||
} else if ("header" == location) { | ||
request = request.newBuilder() | ||
.addHeader(paramName, apiKey) | ||
.build() | ||
} else if ("cookie" == location) { | ||
request = request.newBuilder() | ||
.addHeader("Cookie", "$paramName=$apiKey") | ||
.build() | ||
} | ||
return chain.proceed(request) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.