1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
%global _empty_manifest_terminate_build 0
Name: python-pyramid-openapi3
Version: 0.16.0
Release: 1
Summary: Pyramid addon for OpenAPI3 validation of requests and responses.
License: MIT
URL: https://github.com/Pylons/pyramid_openapi3
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/f9/9d/b6e037bdc73fd6f65fdbc33fcaa0a5bcc26b9e2bbe6e3cf551918ef2fc01/pyramid_openapi3-0.16.0.tar.gz
BuildArch: noarch
Requires: python3-openapi-core
Requires: python3-pyramid
%description
## Validate [Pyramid](https://trypyramid.com) views against an [OpenAPI 3.0](https://swagger.io/specification/) document
<p align="center">
<img height="200" src="https://github.com/Pylons/pyramid_openapi3/blob/main/header.jpg?raw=true" />
</p>
<p align="center">
<a href="https://circleci.com/gh/Pylons/pyramid_openapi3">
<img alt="CircleCI for pyramid_openapi3 (main branch)"
src="https://circleci.com/gh/Pylons/pyramid_openapi3.svg?style=shield">
</a>
<img alt="Test coverage (main branch)"
src="https://img.shields.io/badge/tests_coverage-100%25-brightgreen.svg">
<img alt="Test coverage (main branch)"
src="https://img.shields.io/badge/types_coverage-100%25-brightgreen.svg">
<a href="https://pypi.org/project/pyramid_openapi3/">
<img alt="latest version of pyramid_openapi3 on PyPI"
src="https://img.shields.io/pypi/v/pyramid_openapi3.svg">
</a>
<a href="https://pypi.org/project/pyramid_openapi3/">
<img alt="Supported Python versions"
src="https://img.shields.io/pypi/pyversions/pyramid_openapi3.svg">
</a>
<a href="https://github.com/Pylons/pyramid_openapi3/blob/main/LICENSE">
<img alt="License: MIT"
src="https://img.shields.io/badge/License-MIT-yellow.svg">
</a>
<a href="https://github.com/Pylons/pyramid_openapi3/graphs/contributors">
<img alt="Built by these great folks!"
src="https://img.shields.io/github/contributors/Pylons/pyramid_openapi3.svg">
</a>
</p>
## Peace of Mind
The reason this package exists is to give you peace of mind when providing a RESTful API. Instead of chasing down preventable bugs and saying sorry to consumers, you can focus on more important things in life.
- Your **API documentation is never out-of-date**, since it is generated out of the API document that you write.
- The documentation comes with **_try-it-out_ examples** for every endpoint in your API. You don't have to provide (and maintain) `curl` commands to showcase how your API works. Users can try it themselves, right in their browsers.
- Your **API document is always valid**, since your Pyramid app won't even start if the document does not comply with the OpenAPI 3.0 specification.
- Automatic request **payload validation and sanitization**. Your views do not require any code for validation and input sanitation. Your view code only deals with business logic. Tons of tests never need to be written since every request, and its payload, is validated against your API document before it reaches your view code.
- Your API **responses always match your API document**. Every response from your view is validated against your document and a `500 Internal Server Error` is returned if the response does not exactly match what your document says the output of a certain API endpoint should be. This decreases the effects of [Hyrum's Law](https://www.hyrumslaw.com).
- **A single source of truth**. Because of the checks outlined above, you can be sure that whatever your API document says is in fact what is going on in reality. You have a single source of truth to consult when asking an API related question, such as "Remind me again, which fields are returned by the endpoint `/user/info`?".
- Based on [Pyramid](https://trypyramid.com), a **mature Python Web framework**. Companies such as Mozilla, Yelp, RollBar and SurveyMonkey [trust Pyramid](https://trypyramid.com/community-powered-by-pyramid.html), and the new [pypi.org](https://github.com/pypa/warehouse) runs on Pyramid, too. Pyramid is thoroughly [tested](https://github.com/Pylons/Pyramid/actions?query=workflow%3A%22Build+and+test%22) and [documented](https://docs.pylonsproject.org/projects/pyramid/en/latest/), providing flexibility, performance, and a large ecosystem of [high-quality add-ons](https://trypyramid.com/extending-pyramid.html).
<p align="center">
<a href="https://www.youtube.com/watch?v=P0zNxrDO0sE&t=1061" title="Building Robust APIs" rel="nofollow" class="rich-diff-level-one"><img src="https://user-images.githubusercontent.com/311580/97364772-6d246a80-189c-11eb-84f2-a0ad23236003.png" alt="Building Robust APIs" style="max-width:100%;"></a>
</p>
## Features
- Validates your API document (for example, `openapi.yaml` or `openapi.json`) against the OpenAPI 3.0 specification using the [openapi-spec-validator](https://github.com/p1c2u/openapi-spec-validator).
- Generates and serves the [Swagger try-it-out documentation](https://swagger.io/tools/swagger-ui/) for your API.
- Validates incoming requests *and* outgoing responses against your API document using [openapi-core](https://github.com/p1c2u/openapi-core).
## Getting started
1. Declare `pyramid_openapi3` as a dependency in your Pyramid project.
2. Include the following lines:
```python
config.include("pyramid_openapi3")
config.pyramid_openapi3_spec('openapi.yaml', route='/api/v1/openapi.yaml')
config.pyramid_openapi3_add_explorer(route='/api/v1/')
```
3. Use the `openapi` [view predicate](https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/viewconfig.html#view-configuration-parameters) to enable request/response validation:
```python
@view_config(route_name="foobar", openapi=True, renderer='json')
def myview(request):
return request.openapi_validated.parameters
```
For requests, `request.openapi_validated` is available with two fields: `parameters` and `body`.
For responses, if the payload does not match the API document, an exception is raised.
## Advanced configuration
### Relative File References in Spec
A feature introduced in OpenAPI3 is the ability to use `$ref` links to external files (https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#referenceObject).
To use this, you must ensure that you have all of your spec files in a given directory (ensure that you do not have any code in this directory as all the files in it are exposed as static files), then **replace** the `pyramid_openapi3_spec` call that you did in [Getting Started](#getting-started) with the following:
```python
config.pyramid_openapi3_spec_directory('path/to/openapi.yaml', route='/api/v1/spec')
```
Some notes:
- Do not set the `route` of your `pyramid_openapi3_spec_directory` to the same value as the `route` of `pyramid_openapi3_add_explorer`.
- The `route` that you set for `pyramid_openapi3_spec_directory` should not contain any file extensions, as this becomes the root for all of the files in your specified `filepath`.
- You cannot use `pyramid_openapi3_spec_directory` and `pyramid_openapi3_spec` in the same app.
### Endpoints / Request / Response Validation
Provided with `pyramid_openapi3` are a few validation features:
* incoming request validation (i.e., what a client sends to your app)
* outgoing response validation (i.e., what your app sends to a client)
* endpoint validation (i.e., your app registers routes for all defined API endpoints)
These features are enabled as a default, but you can disable them if you need to:
```python
config.registry.settings["pyramid_openapi3.enable_endpoint_validation"] = False
config.registry.settings["pyramid_openapi3.enable_request_validation"] = False
config.registry.settings["pyramid_openapi3.enable_response_validation"] = False
```
> **Warning:**
Disabling request validation will result in `request.openapi_validated` no longer being available to use.
### Register Pyramid's Routes
You can register routes in your pyramid application.
First, write the `x-pyramid-route-name` extension in the PathItem of the OpenAPI schema.
```yaml
paths:
/foo:
x-pyramid-route-name: foo_route
get:
responses:
200:
description: GET foo
```
Then put the config directive `pyramid_openapi3_register_routes` in the app_factory of your application.
```python
config.pyramid_openapi3_register_routes()
```
This is equal to manually writing the following:
```python
config.add_route("foo_route", pattern="/foo")
```
The `pyramid_openapi3_register_routes()` method supports setting a factory and route prefix as well. See the source for details.
## Demo / Examples
There are three examples provided with this package:
* A fairly simple [single-file app providing a Hello World API](https://github.com/Pylons/pyramid_openapi3/tree/main/examples/singlefile).
* A slightly more [built-out app providing a TODO app API](https://github.com/Pylons/pyramid_openapi3/tree/main/examples/todoapp).
* Another TODO app API, defined using a [YAML spec split into multiple files](https://github.com/Pylons/pyramid_openapi3/tree/main/examples/splitfile).
All examples come with tests that exhibit pyramid_openapi's error handling and validation capabilities.
A **fully built-out app**, with 100% test coverage, providing a [RealWorld.io](https://realworld.io) API is available at [niteoweb/pyramid-realworld-example-app](https://github.com/niteoweb/pyramid-realworld-example-app). It is a Heroku-deployable Pyramid app that provides an API for a Medium.com-like social app. You are encouraged to use it as a scaffold for your next project.
## Design defense
The authors of pyramid_openapi3 believe that the approach of validating a manually-written API document is superior to the approach of generating the API document from Python code. Here are the reasons:
1. Both generation and validation against a document are lossy processes. The underlying libraries running the generation/validation will always have something missing. Either a feature from the latest OpenAPI specification, or an implementation bug. Having to fork the underlying library in order to generate the part of your API document that might only be needed for the frontend is unfortunate.
Validation on the other hand allows one to skip parts of validation that are not supported yet, and not block a team from shipping the document.
2. The validation approach does sacrifice DRY-ness, and one has to write the API document and then the (view) code in Pyramid. It feels a bit redundant at first. However, this provides a clear separation between the intent and the implementation.
3. The generation approach has the drawback of having to write Python code even for parts of the API document that the Pyramid backend does not handle, as it might be handled by a different system, or be specific only to documentation or only to the client side of the API. This bloats your Pyramid codebase with code that does not belong there.
## Running tests
You need to have [poetry](https://python-poetry.org/) and Python 3.7 through 3.11 installed on your machine.
Alternatively, if you use [nix](https://nix.dev/tutorials/declarative-and-reproducible-developer-environments), run `nix-shell` to drop into a shell that has everything prepared for development.
Then you can run:
```shell
make tests
```
## Related packages
These packages tackle the same problem-space:
- [pyramid_oas3](https://github.com/kazuki/pyramid-oas3) seems to do things very similarly to pyramid_openapi3, but the documentation is not in English and we sadly can't fully understand what it does by just reading the code.
- [pyramid_swagger](https://github.com/striglia/pyramid_swagger) does a similar
thing, but for Swagger 2.0 documents.
- [connexion](https://github.com/zalando/connexion) takes the same "write spec first, code second" approach as pyramid_openapi3, but is based on Flask.
- [bottle-swagger](https://github.com/ampedandwired/bottle-swagger) takes the same "write spec first, code second" approach too, but is based on Bottle.
- [pyramid_apispec](https://github.com/ergo/pyramid_apispec) uses generation with
help of apispec and the marshmallow validation library. See above [why we prefer validation instead of generation](#design-defense).
## Deprecation policy
We do our best to follow the rules below.
* Support the latest few releases of Python, currently Python 3.7 through 3.11.
* Support the latest few releases of Pyramid, currently 1.10.7 through 2.0.
* Support the latest few releases of `openapi-core`, currently 0.16.1 through 0.16.2.
* See `poetry.lock` for a frozen-in-time known-good-set of all dependencies.
## Use in the wild
A couple of projects that use pyramid_openapi3 in production:
- [WooCart API](https://app.woocart.com/api/v1) - User control panel for WooCart Managed WooCommerce service.
- [Kafkai API](https://app.kafkai.com/api/v1) - User control panel for Kafkai text generation service.
- [Open on-chain data API](https://tradingstrategy.ai/api/explorer/) - Decentralised exchange and blockchain trading data open API
- [Pareto Security Team Dashboard API](https://dash.paretosecurity.app/api/v1) - Team Dashboard for Pareto Security macOS security app
%package -n python3-pyramid-openapi3
Summary: Pyramid addon for OpenAPI3 validation of requests and responses.
Provides: python-pyramid-openapi3
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-pyramid-openapi3
## Validate [Pyramid](https://trypyramid.com) views against an [OpenAPI 3.0](https://swagger.io/specification/) document
<p align="center">
<img height="200" src="https://github.com/Pylons/pyramid_openapi3/blob/main/header.jpg?raw=true" />
</p>
<p align="center">
<a href="https://circleci.com/gh/Pylons/pyramid_openapi3">
<img alt="CircleCI for pyramid_openapi3 (main branch)"
src="https://circleci.com/gh/Pylons/pyramid_openapi3.svg?style=shield">
</a>
<img alt="Test coverage (main branch)"
src="https://img.shields.io/badge/tests_coverage-100%25-brightgreen.svg">
<img alt="Test coverage (main branch)"
src="https://img.shields.io/badge/types_coverage-100%25-brightgreen.svg">
<a href="https://pypi.org/project/pyramid_openapi3/">
<img alt="latest version of pyramid_openapi3 on PyPI"
src="https://img.shields.io/pypi/v/pyramid_openapi3.svg">
</a>
<a href="https://pypi.org/project/pyramid_openapi3/">
<img alt="Supported Python versions"
src="https://img.shields.io/pypi/pyversions/pyramid_openapi3.svg">
</a>
<a href="https://github.com/Pylons/pyramid_openapi3/blob/main/LICENSE">
<img alt="License: MIT"
src="https://img.shields.io/badge/License-MIT-yellow.svg">
</a>
<a href="https://github.com/Pylons/pyramid_openapi3/graphs/contributors">
<img alt="Built by these great folks!"
src="https://img.shields.io/github/contributors/Pylons/pyramid_openapi3.svg">
</a>
</p>
## Peace of Mind
The reason this package exists is to give you peace of mind when providing a RESTful API. Instead of chasing down preventable bugs and saying sorry to consumers, you can focus on more important things in life.
- Your **API documentation is never out-of-date**, since it is generated out of the API document that you write.
- The documentation comes with **_try-it-out_ examples** for every endpoint in your API. You don't have to provide (and maintain) `curl` commands to showcase how your API works. Users can try it themselves, right in their browsers.
- Your **API document is always valid**, since your Pyramid app won't even start if the document does not comply with the OpenAPI 3.0 specification.
- Automatic request **payload validation and sanitization**. Your views do not require any code for validation and input sanitation. Your view code only deals with business logic. Tons of tests never need to be written since every request, and its payload, is validated against your API document before it reaches your view code.
- Your API **responses always match your API document**. Every response from your view is validated against your document and a `500 Internal Server Error` is returned if the response does not exactly match what your document says the output of a certain API endpoint should be. This decreases the effects of [Hyrum's Law](https://www.hyrumslaw.com).
- **A single source of truth**. Because of the checks outlined above, you can be sure that whatever your API document says is in fact what is going on in reality. You have a single source of truth to consult when asking an API related question, such as "Remind me again, which fields are returned by the endpoint `/user/info`?".
- Based on [Pyramid](https://trypyramid.com), a **mature Python Web framework**. Companies such as Mozilla, Yelp, RollBar and SurveyMonkey [trust Pyramid](https://trypyramid.com/community-powered-by-pyramid.html), and the new [pypi.org](https://github.com/pypa/warehouse) runs on Pyramid, too. Pyramid is thoroughly [tested](https://github.com/Pylons/Pyramid/actions?query=workflow%3A%22Build+and+test%22) and [documented](https://docs.pylonsproject.org/projects/pyramid/en/latest/), providing flexibility, performance, and a large ecosystem of [high-quality add-ons](https://trypyramid.com/extending-pyramid.html).
<p align="center">
<a href="https://www.youtube.com/watch?v=P0zNxrDO0sE&t=1061" title="Building Robust APIs" rel="nofollow" class="rich-diff-level-one"><img src="https://user-images.githubusercontent.com/311580/97364772-6d246a80-189c-11eb-84f2-a0ad23236003.png" alt="Building Robust APIs" style="max-width:100%;"></a>
</p>
## Features
- Validates your API document (for example, `openapi.yaml` or `openapi.json`) against the OpenAPI 3.0 specification using the [openapi-spec-validator](https://github.com/p1c2u/openapi-spec-validator).
- Generates and serves the [Swagger try-it-out documentation](https://swagger.io/tools/swagger-ui/) for your API.
- Validates incoming requests *and* outgoing responses against your API document using [openapi-core](https://github.com/p1c2u/openapi-core).
## Getting started
1. Declare `pyramid_openapi3` as a dependency in your Pyramid project.
2. Include the following lines:
```python
config.include("pyramid_openapi3")
config.pyramid_openapi3_spec('openapi.yaml', route='/api/v1/openapi.yaml')
config.pyramid_openapi3_add_explorer(route='/api/v1/')
```
3. Use the `openapi` [view predicate](https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/viewconfig.html#view-configuration-parameters) to enable request/response validation:
```python
@view_config(route_name="foobar", openapi=True, renderer='json')
def myview(request):
return request.openapi_validated.parameters
```
For requests, `request.openapi_validated` is available with two fields: `parameters` and `body`.
For responses, if the payload does not match the API document, an exception is raised.
## Advanced configuration
### Relative File References in Spec
A feature introduced in OpenAPI3 is the ability to use `$ref` links to external files (https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#referenceObject).
To use this, you must ensure that you have all of your spec files in a given directory (ensure that you do not have any code in this directory as all the files in it are exposed as static files), then **replace** the `pyramid_openapi3_spec` call that you did in [Getting Started](#getting-started) with the following:
```python
config.pyramid_openapi3_spec_directory('path/to/openapi.yaml', route='/api/v1/spec')
```
Some notes:
- Do not set the `route` of your `pyramid_openapi3_spec_directory` to the same value as the `route` of `pyramid_openapi3_add_explorer`.
- The `route` that you set for `pyramid_openapi3_spec_directory` should not contain any file extensions, as this becomes the root for all of the files in your specified `filepath`.
- You cannot use `pyramid_openapi3_spec_directory` and `pyramid_openapi3_spec` in the same app.
### Endpoints / Request / Response Validation
Provided with `pyramid_openapi3` are a few validation features:
* incoming request validation (i.e., what a client sends to your app)
* outgoing response validation (i.e., what your app sends to a client)
* endpoint validation (i.e., your app registers routes for all defined API endpoints)
These features are enabled as a default, but you can disable them if you need to:
```python
config.registry.settings["pyramid_openapi3.enable_endpoint_validation"] = False
config.registry.settings["pyramid_openapi3.enable_request_validation"] = False
config.registry.settings["pyramid_openapi3.enable_response_validation"] = False
```
> **Warning:**
Disabling request validation will result in `request.openapi_validated` no longer being available to use.
### Register Pyramid's Routes
You can register routes in your pyramid application.
First, write the `x-pyramid-route-name` extension in the PathItem of the OpenAPI schema.
```yaml
paths:
/foo:
x-pyramid-route-name: foo_route
get:
responses:
200:
description: GET foo
```
Then put the config directive `pyramid_openapi3_register_routes` in the app_factory of your application.
```python
config.pyramid_openapi3_register_routes()
```
This is equal to manually writing the following:
```python
config.add_route("foo_route", pattern="/foo")
```
The `pyramid_openapi3_register_routes()` method supports setting a factory and route prefix as well. See the source for details.
## Demo / Examples
There are three examples provided with this package:
* A fairly simple [single-file app providing a Hello World API](https://github.com/Pylons/pyramid_openapi3/tree/main/examples/singlefile).
* A slightly more [built-out app providing a TODO app API](https://github.com/Pylons/pyramid_openapi3/tree/main/examples/todoapp).
* Another TODO app API, defined using a [YAML spec split into multiple files](https://github.com/Pylons/pyramid_openapi3/tree/main/examples/splitfile).
All examples come with tests that exhibit pyramid_openapi's error handling and validation capabilities.
A **fully built-out app**, with 100% test coverage, providing a [RealWorld.io](https://realworld.io) API is available at [niteoweb/pyramid-realworld-example-app](https://github.com/niteoweb/pyramid-realworld-example-app). It is a Heroku-deployable Pyramid app that provides an API for a Medium.com-like social app. You are encouraged to use it as a scaffold for your next project.
## Design defense
The authors of pyramid_openapi3 believe that the approach of validating a manually-written API document is superior to the approach of generating the API document from Python code. Here are the reasons:
1. Both generation and validation against a document are lossy processes. The underlying libraries running the generation/validation will always have something missing. Either a feature from the latest OpenAPI specification, or an implementation bug. Having to fork the underlying library in order to generate the part of your API document that might only be needed for the frontend is unfortunate.
Validation on the other hand allows one to skip parts of validation that are not supported yet, and not block a team from shipping the document.
2. The validation approach does sacrifice DRY-ness, and one has to write the API document and then the (view) code in Pyramid. It feels a bit redundant at first. However, this provides a clear separation between the intent and the implementation.
3. The generation approach has the drawback of having to write Python code even for parts of the API document that the Pyramid backend does not handle, as it might be handled by a different system, or be specific only to documentation or only to the client side of the API. This bloats your Pyramid codebase with code that does not belong there.
## Running tests
You need to have [poetry](https://python-poetry.org/) and Python 3.7 through 3.11 installed on your machine.
Alternatively, if you use [nix](https://nix.dev/tutorials/declarative-and-reproducible-developer-environments), run `nix-shell` to drop into a shell that has everything prepared for development.
Then you can run:
```shell
make tests
```
## Related packages
These packages tackle the same problem-space:
- [pyramid_oas3](https://github.com/kazuki/pyramid-oas3) seems to do things very similarly to pyramid_openapi3, but the documentation is not in English and we sadly can't fully understand what it does by just reading the code.
- [pyramid_swagger](https://github.com/striglia/pyramid_swagger) does a similar
thing, but for Swagger 2.0 documents.
- [connexion](https://github.com/zalando/connexion) takes the same "write spec first, code second" approach as pyramid_openapi3, but is based on Flask.
- [bottle-swagger](https://github.com/ampedandwired/bottle-swagger) takes the same "write spec first, code second" approach too, but is based on Bottle.
- [pyramid_apispec](https://github.com/ergo/pyramid_apispec) uses generation with
help of apispec and the marshmallow validation library. See above [why we prefer validation instead of generation](#design-defense).
## Deprecation policy
We do our best to follow the rules below.
* Support the latest few releases of Python, currently Python 3.7 through 3.11.
* Support the latest few releases of Pyramid, currently 1.10.7 through 2.0.
* Support the latest few releases of `openapi-core`, currently 0.16.1 through 0.16.2.
* See `poetry.lock` for a frozen-in-time known-good-set of all dependencies.
## Use in the wild
A couple of projects that use pyramid_openapi3 in production:
- [WooCart API](https://app.woocart.com/api/v1) - User control panel for WooCart Managed WooCommerce service.
- [Kafkai API](https://app.kafkai.com/api/v1) - User control panel for Kafkai text generation service.
- [Open on-chain data API](https://tradingstrategy.ai/api/explorer/) - Decentralised exchange and blockchain trading data open API
- [Pareto Security Team Dashboard API](https://dash.paretosecurity.app/api/v1) - Team Dashboard for Pareto Security macOS security app
%package help
Summary: Development documents and examples for pyramid-openapi3
Provides: python3-pyramid-openapi3-doc
%description help
## Validate [Pyramid](https://trypyramid.com) views against an [OpenAPI 3.0](https://swagger.io/specification/) document
<p align="center">
<img height="200" src="https://github.com/Pylons/pyramid_openapi3/blob/main/header.jpg?raw=true" />
</p>
<p align="center">
<a href="https://circleci.com/gh/Pylons/pyramid_openapi3">
<img alt="CircleCI for pyramid_openapi3 (main branch)"
src="https://circleci.com/gh/Pylons/pyramid_openapi3.svg?style=shield">
</a>
<img alt="Test coverage (main branch)"
src="https://img.shields.io/badge/tests_coverage-100%25-brightgreen.svg">
<img alt="Test coverage (main branch)"
src="https://img.shields.io/badge/types_coverage-100%25-brightgreen.svg">
<a href="https://pypi.org/project/pyramid_openapi3/">
<img alt="latest version of pyramid_openapi3 on PyPI"
src="https://img.shields.io/pypi/v/pyramid_openapi3.svg">
</a>
<a href="https://pypi.org/project/pyramid_openapi3/">
<img alt="Supported Python versions"
src="https://img.shields.io/pypi/pyversions/pyramid_openapi3.svg">
</a>
<a href="https://github.com/Pylons/pyramid_openapi3/blob/main/LICENSE">
<img alt="License: MIT"
src="https://img.shields.io/badge/License-MIT-yellow.svg">
</a>
<a href="https://github.com/Pylons/pyramid_openapi3/graphs/contributors">
<img alt="Built by these great folks!"
src="https://img.shields.io/github/contributors/Pylons/pyramid_openapi3.svg">
</a>
</p>
## Peace of Mind
The reason this package exists is to give you peace of mind when providing a RESTful API. Instead of chasing down preventable bugs and saying sorry to consumers, you can focus on more important things in life.
- Your **API documentation is never out-of-date**, since it is generated out of the API document that you write.
- The documentation comes with **_try-it-out_ examples** for every endpoint in your API. You don't have to provide (and maintain) `curl` commands to showcase how your API works. Users can try it themselves, right in their browsers.
- Your **API document is always valid**, since your Pyramid app won't even start if the document does not comply with the OpenAPI 3.0 specification.
- Automatic request **payload validation and sanitization**. Your views do not require any code for validation and input sanitation. Your view code only deals with business logic. Tons of tests never need to be written since every request, and its payload, is validated against your API document before it reaches your view code.
- Your API **responses always match your API document**. Every response from your view is validated against your document and a `500 Internal Server Error` is returned if the response does not exactly match what your document says the output of a certain API endpoint should be. This decreases the effects of [Hyrum's Law](https://www.hyrumslaw.com).
- **A single source of truth**. Because of the checks outlined above, you can be sure that whatever your API document says is in fact what is going on in reality. You have a single source of truth to consult when asking an API related question, such as "Remind me again, which fields are returned by the endpoint `/user/info`?".
- Based on [Pyramid](https://trypyramid.com), a **mature Python Web framework**. Companies such as Mozilla, Yelp, RollBar and SurveyMonkey [trust Pyramid](https://trypyramid.com/community-powered-by-pyramid.html), and the new [pypi.org](https://github.com/pypa/warehouse) runs on Pyramid, too. Pyramid is thoroughly [tested](https://github.com/Pylons/Pyramid/actions?query=workflow%3A%22Build+and+test%22) and [documented](https://docs.pylonsproject.org/projects/pyramid/en/latest/), providing flexibility, performance, and a large ecosystem of [high-quality add-ons](https://trypyramid.com/extending-pyramid.html).
<p align="center">
<a href="https://www.youtube.com/watch?v=P0zNxrDO0sE&t=1061" title="Building Robust APIs" rel="nofollow" class="rich-diff-level-one"><img src="https://user-images.githubusercontent.com/311580/97364772-6d246a80-189c-11eb-84f2-a0ad23236003.png" alt="Building Robust APIs" style="max-width:100%;"></a>
</p>
## Features
- Validates your API document (for example, `openapi.yaml` or `openapi.json`) against the OpenAPI 3.0 specification using the [openapi-spec-validator](https://github.com/p1c2u/openapi-spec-validator).
- Generates and serves the [Swagger try-it-out documentation](https://swagger.io/tools/swagger-ui/) for your API.
- Validates incoming requests *and* outgoing responses against your API document using [openapi-core](https://github.com/p1c2u/openapi-core).
## Getting started
1. Declare `pyramid_openapi3` as a dependency in your Pyramid project.
2. Include the following lines:
```python
config.include("pyramid_openapi3")
config.pyramid_openapi3_spec('openapi.yaml', route='/api/v1/openapi.yaml')
config.pyramid_openapi3_add_explorer(route='/api/v1/')
```
3. Use the `openapi` [view predicate](https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/viewconfig.html#view-configuration-parameters) to enable request/response validation:
```python
@view_config(route_name="foobar", openapi=True, renderer='json')
def myview(request):
return request.openapi_validated.parameters
```
For requests, `request.openapi_validated` is available with two fields: `parameters` and `body`.
For responses, if the payload does not match the API document, an exception is raised.
## Advanced configuration
### Relative File References in Spec
A feature introduced in OpenAPI3 is the ability to use `$ref` links to external files (https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#referenceObject).
To use this, you must ensure that you have all of your spec files in a given directory (ensure that you do not have any code in this directory as all the files in it are exposed as static files), then **replace** the `pyramid_openapi3_spec` call that you did in [Getting Started](#getting-started) with the following:
```python
config.pyramid_openapi3_spec_directory('path/to/openapi.yaml', route='/api/v1/spec')
```
Some notes:
- Do not set the `route` of your `pyramid_openapi3_spec_directory` to the same value as the `route` of `pyramid_openapi3_add_explorer`.
- The `route` that you set for `pyramid_openapi3_spec_directory` should not contain any file extensions, as this becomes the root for all of the files in your specified `filepath`.
- You cannot use `pyramid_openapi3_spec_directory` and `pyramid_openapi3_spec` in the same app.
### Endpoints / Request / Response Validation
Provided with `pyramid_openapi3` are a few validation features:
* incoming request validation (i.e., what a client sends to your app)
* outgoing response validation (i.e., what your app sends to a client)
* endpoint validation (i.e., your app registers routes for all defined API endpoints)
These features are enabled as a default, but you can disable them if you need to:
```python
config.registry.settings["pyramid_openapi3.enable_endpoint_validation"] = False
config.registry.settings["pyramid_openapi3.enable_request_validation"] = False
config.registry.settings["pyramid_openapi3.enable_response_validation"] = False
```
> **Warning:**
Disabling request validation will result in `request.openapi_validated` no longer being available to use.
### Register Pyramid's Routes
You can register routes in your pyramid application.
First, write the `x-pyramid-route-name` extension in the PathItem of the OpenAPI schema.
```yaml
paths:
/foo:
x-pyramid-route-name: foo_route
get:
responses:
200:
description: GET foo
```
Then put the config directive `pyramid_openapi3_register_routes` in the app_factory of your application.
```python
config.pyramid_openapi3_register_routes()
```
This is equal to manually writing the following:
```python
config.add_route("foo_route", pattern="/foo")
```
The `pyramid_openapi3_register_routes()` method supports setting a factory and route prefix as well. See the source for details.
## Demo / Examples
There are three examples provided with this package:
* A fairly simple [single-file app providing a Hello World API](https://github.com/Pylons/pyramid_openapi3/tree/main/examples/singlefile).
* A slightly more [built-out app providing a TODO app API](https://github.com/Pylons/pyramid_openapi3/tree/main/examples/todoapp).
* Another TODO app API, defined using a [YAML spec split into multiple files](https://github.com/Pylons/pyramid_openapi3/tree/main/examples/splitfile).
All examples come with tests that exhibit pyramid_openapi's error handling and validation capabilities.
A **fully built-out app**, with 100% test coverage, providing a [RealWorld.io](https://realworld.io) API is available at [niteoweb/pyramid-realworld-example-app](https://github.com/niteoweb/pyramid-realworld-example-app). It is a Heroku-deployable Pyramid app that provides an API for a Medium.com-like social app. You are encouraged to use it as a scaffold for your next project.
## Design defense
The authors of pyramid_openapi3 believe that the approach of validating a manually-written API document is superior to the approach of generating the API document from Python code. Here are the reasons:
1. Both generation and validation against a document are lossy processes. The underlying libraries running the generation/validation will always have something missing. Either a feature from the latest OpenAPI specification, or an implementation bug. Having to fork the underlying library in order to generate the part of your API document that might only be needed for the frontend is unfortunate.
Validation on the other hand allows one to skip parts of validation that are not supported yet, and not block a team from shipping the document.
2. The validation approach does sacrifice DRY-ness, and one has to write the API document and then the (view) code in Pyramid. It feels a bit redundant at first. However, this provides a clear separation between the intent and the implementation.
3. The generation approach has the drawback of having to write Python code even for parts of the API document that the Pyramid backend does not handle, as it might be handled by a different system, or be specific only to documentation or only to the client side of the API. This bloats your Pyramid codebase with code that does not belong there.
## Running tests
You need to have [poetry](https://python-poetry.org/) and Python 3.7 through 3.11 installed on your machine.
Alternatively, if you use [nix](https://nix.dev/tutorials/declarative-and-reproducible-developer-environments), run `nix-shell` to drop into a shell that has everything prepared for development.
Then you can run:
```shell
make tests
```
## Related packages
These packages tackle the same problem-space:
- [pyramid_oas3](https://github.com/kazuki/pyramid-oas3) seems to do things very similarly to pyramid_openapi3, but the documentation is not in English and we sadly can't fully understand what it does by just reading the code.
- [pyramid_swagger](https://github.com/striglia/pyramid_swagger) does a similar
thing, but for Swagger 2.0 documents.
- [connexion](https://github.com/zalando/connexion) takes the same "write spec first, code second" approach as pyramid_openapi3, but is based on Flask.
- [bottle-swagger](https://github.com/ampedandwired/bottle-swagger) takes the same "write spec first, code second" approach too, but is based on Bottle.
- [pyramid_apispec](https://github.com/ergo/pyramid_apispec) uses generation with
help of apispec and the marshmallow validation library. See above [why we prefer validation instead of generation](#design-defense).
## Deprecation policy
We do our best to follow the rules below.
* Support the latest few releases of Python, currently Python 3.7 through 3.11.
* Support the latest few releases of Pyramid, currently 1.10.7 through 2.0.
* Support the latest few releases of `openapi-core`, currently 0.16.1 through 0.16.2.
* See `poetry.lock` for a frozen-in-time known-good-set of all dependencies.
## Use in the wild
A couple of projects that use pyramid_openapi3 in production:
- [WooCart API](https://app.woocart.com/api/v1) - User control panel for WooCart Managed WooCommerce service.
- [Kafkai API](https://app.kafkai.com/api/v1) - User control panel for Kafkai text generation service.
- [Open on-chain data API](https://tradingstrategy.ai/api/explorer/) - Decentralised exchange and blockchain trading data open API
- [Pareto Security Team Dashboard API](https://dash.paretosecurity.app/api/v1) - Team Dashboard for Pareto Security macOS security app
%prep
%autosetup -n pyramid-openapi3-0.16.0
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-pyramid-openapi3 -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 0.16.0-1
- Package Spec generated
|