|
| 1 | +# codegen-kotlin-okhttp |
| 2 | + |
| 3 | +> Converts Postman-SDK Request into code snippet for kotlin-okhttp. |
| 4 | +
|
| 5 | +#### Prerequisites |
| 6 | +To run Code-Gen, ensure that you have NodeJS >= v12. A copy of the NodeJS installable can be downloaded from https://nodejs.org/en/download/package-manager. |
| 7 | + |
| 8 | +## Using the Module |
| 9 | +The module will expose an object which will have property `convert` which is the function for converting the Postman-SDK request to kotlin-okhttp code snippet and `getOptions` function which returns an array of supported options. |
| 10 | + |
| 11 | +### convert function |
| 12 | +Convert function will take three parameters |
| 13 | +* `request`- Postman-SDK Request object |
| 14 | + |
| 15 | +* `options`- options is an object which can have following properties |
| 16 | + * `indentType`- string representing type of indentation for code snippet. eg: 'Space', 'Tab' |
| 17 | + * `indentCount`- positiveInteger representing count of indentation required. |
| 18 | + * `includeBoilerplate`- boolean representing whether to include class definition in code snippet |
| 19 | + * `requestTimeout` : Integer denoting time after which the request will bail out in milli-seconds |
| 20 | + * `trimRequestBody` : Trim request body fields |
| 21 | + * `followRedirect` : Boolean denoting whether to redirect a request |
| 22 | + |
| 23 | +* `callback`- callback function with first parameter as error and second parameter as string for code snippet |
| 24 | + |
| 25 | +##### Example: |
| 26 | +```js |
| 27 | +var request = new sdk.Request('www.google.com'), //using postman sdk to create request |
| 28 | + options = { |
| 29 | + indentType: 'Space', |
| 30 | + indentCount: 2, |
| 31 | + includeBoilerplate: false |
| 32 | + }; |
| 33 | +convert(request, options, function(error, snippet) { |
| 34 | + if (error) { |
| 35 | + // handle error |
| 36 | + } |
| 37 | + // handle snippet |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +### getOptions function |
| 42 | + |
| 43 | +This function returns a list of options supported by this codegen. |
| 44 | + |
| 45 | +#### Example |
| 46 | +```js |
| 47 | +var options = getOptions(); |
| 48 | + |
| 49 | +console.log(options); |
| 50 | +// output |
| 51 | +// [ |
| 52 | +// { |
| 53 | +// name: 'Set indentation count', |
| 54 | +// id: 'indentCount', |
| 55 | +// type: 'positiveInteger', |
| 56 | +// default: 2, |
| 57 | +// description: 'Set the number of indentation characters to add per code level' |
| 58 | +// }, |
| 59 | +// ... |
| 60 | +// ] |
| 61 | +``` |
| 62 | + |
| 63 | +### Guideline for using generated snippet |
| 64 | +* Generated snippet requires dependencies [okhttp3](https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp/3.9.1) and [okio](https://mvnrepository.com/artifact/com.squareup.okio/okio/1.13.0) to compile and run |
| 65 | + |
| 66 | +* Generated snippet uses `.method(nameOfMethod, body)` from `Request` class to form HTTP request. If the `method` doesn't require body then the value of `body` will be `null`. |
| 67 | + |
| 68 | +* Generated snippet uses `MultipartBody.Builder()` when `multipart/formdata` is used otherwise it uses `RequestBody.create()` in order to add body to request. |
| 69 | + |
| 70 | +* Since Postman-SDK Request object doesn't provide complete path of the file, it needs to be manually inserted in case of uploading a file. |
| 71 | + |
| 72 | +* `content-type` needs to be specified in order to add body to the request. So if no `content-type` is specified then `text/plain` will be used as default. **In case of `multipart/formdata` `content-type` is generated by snippet itself**. |
| 73 | + |
| 74 | +* This module doesn't support cookies. |
0 commit comments