Browse Source

Merge pull request #524 from IcaroJerry/master

Include Python as example language
Shalvah 6 years ago
parent
commit
d07f68a981

+ 1 - 1
config/apidoc.php

@@ -198,7 +198,7 @@ return [
 
     /*
      * Example requests for each endpoint will be shown in each of these languages.
-     * Supported options are: bash, javascript, php
+     * Supported options are: bash, javascript, php, python
      * You can add a language of your own, but you must publish the package's views
      * and define a corresponding view for it in the partials/example-requests directory.
      * See https://laravel-apidoc-generator.readthedocs.io/en/latest/generating-documentation.html

+ 1 - 1
docs/config.md

@@ -35,7 +35,7 @@ If you want to use this, please note that the image size must be 230 x 52.
 When [documenting your api](documenting.md), you use `@group` annotations to group API endpoints. Endpoints which do not have a ggroup annotation will be grouped under the `default_group`. Defaults to **"general"**.
 
 ## `example_languages`
-For each endpoint, an example request is shown in each of the languages specified in this array. Currently only `bash`, `javascript` and `php` are supported. You can add your own language, but you must also define the corresponding view (see [Specifying languages for examples](generating-documentation.html#specifying-language-for-examples)). Default: `["bash", "javascript"]` 
+For each endpoint, an example request is shown in each of the languages specified in this array. Currently only `bash`, `javascript`, `php` and `python` are supported. You can add your own language, but you must also define the corresponding view (see [Specifying languages for examples](generating-documentation.html#specifying-language-for-examples)). Default: `["bash", "javascript"]` 
  
 ##  `faker_seed`
 When generating example requests, this package uses fzanninoto/faker to generate random values. If you would like the package to generate the same example values for parameters on each run, set this to any number (eg. 1234). (Note: alternatively, you can set example values for parameters when [documenting them.](documenting.html#specifying-request-parameters))

+ 1 - 1
docs/generating-documentation.md

@@ -47,7 +47,7 @@ php artisan apidoc:rebuild
  
  This will copy the views files to `\resources\views\vendor\apidoc`.
  
- - Next, create a file called {language-name}.blade.php (for example, python.blade.php) in the partials/example-requests directory. You can then write Markdown with Blade templating that describes how the example request for the language should be rendered. You have the `$route` variable available to you. This variable is an array with the following keys:
+ - Next, create a file called {language-name}.blade.php (for example, ruby.blade.php) in the partials/example-requests directory. You can then write Markdown with Blade templating that describes how the example request for the language should be rendered. You have the `$route` variable available to you. This variable is an array with the following keys:
     - `methods`: an array of the HTTP methods for that route
     - `boundUri`: the complete URL for the route, with any url parameters replaced (/users/{id} -> /users/1)
     - `headers`: key-value array of headers to be sent with route (according to your configuration)

+ 27 - 0
resources/views/partials/example-requests/python.blade.php

@@ -0,0 +1,27 @@
+```python
+import requests
+import json
+
+url = '{{ rtrim($baseUrl, '/') }}/{{ ltrim($route['boundUri'], '/') }}'
+@if(count($route['cleanBodyParameters']))
+payload = {
+	@foreach($route['cleanBodyParameters'] as $attribute => $parameter)
+	    '{{ $attribute }}': '{{ $parameter['value'] }}'@if(!($loop->last)),@endif  {{ !$parameter['required'] ? '# optional' : '' }}
+	@endforeach
+}
+@endif
+@if(count($route['cleanQueryParameters']))
+params = {
+	@foreach($route['cleanQueryParameters'] as $attribute => $parameter)
+	    '{{ $attribute }}': '{{ $parameter['value'] }}'@if(!($loop->last)),@endif  {{ !$parameter['required'] ? '# optional' : '' }}
+	@endforeach
+}
+@endif
+headers = {
+	@foreach($route['headers'] as $header => $value)
+	    '{{$header}}': '{{$value}}'@if(!($loop->last)),@endif
+	@endforeach
+}
+response = requests.request('{{$route['methods'][0]}}', url, headers=headers{{ count($route['cleanBodyParameters']) ? ', json=payload' : '' }}{{ count($route['cleanQueryParameters']) ? ', params=params' : ''}})
+response.json()
+```