{"id":18447778,"url":"https://github.com/stoplightio/examplechooserprismproxy","last_synced_at":"2025-07-03T06:04:57.134Z","repository":{"id":146481591,"uuid":"612652077","full_name":"stoplightio/ExampleChooserPrismProxy","owner":"stoplightio","description":"proxy server that adds example-selection logic to an upstream Stoplight Prism mock server","archived":false,"fork":false,"pushed_at":"2024-07-05T23:34:20.000Z","size":114,"stargazers_count":5,"open_issues_count":5,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-26T09:18:01.966Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stoplightio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2023-03-11T15:29:29.000Z","updated_at":"2024-05-05T13:46:59.000Z","dependencies_parsed_at":"2024-04-16T15:52:14.014Z","dependency_job_id":"fe61f649-f95e-4b5b-9e64-8a7fabf98ea9","html_url":"https://github.com/stoplightio/ExampleChooserPrismProxy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/stoplightio/ExampleChooserPrismProxy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stoplightio%2FExampleChooserPrismProxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stoplightio%2FExampleChooserPrismProxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stoplightio%2FExampleChooserPrismProxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stoplightio%2FExampleChooserPrismProxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stoplightio","download_url":"https://codeload.github.com/stoplightio/ExampleChooserPrismProxy/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stoplightio%2FExampleChooserPrismProxy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263271502,"owners_count":23440396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T07:14:24.867Z","updated_at":"2025-07-03T06:04:57.063Z","avatar_url":"https://github.com/stoplightio.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"- [Goal](#goal)\n- [Context](#context)\n- [Solution](#solution)\n  - [Prerequisites](#prerequisites)\n  - [Quick Start](#quick-start)\n- [Customize](#customize)\n\n---------------------------------------------------------------------\n\n## Goal\n\nProvide an example proxy server that allows arbitrarily complex\nexample-selection logic to an upstream [Stoplight\nPrism](https://github.com/stoplightio/prism) server in \"mock\" mode.\n\n## Context\n\nFor the full discussion, see https://github.com/stoplightio/prism/issues/1838.\n\n[Stoplight Prism](https://github.com/stoplightio/prism) offers [the limited\nability to dynamically select\nexamples](https://docs.stoplight.io/docs/prism/83dbbd75532cf-http-mocking#dynamic-response-generation).\nSome users have requested:\n- More complex example selection logic within Prism, or\n- The ability to select examples based only on query string or request body\n  content\n\nSo far, these enhancements lie outside the bounds of Prism's goals.\n\nAn alternative approach is to add a proxy server that includes this complex\nbehavior and use it to add the necessary `Prefer:` HTTP header to the request\nto Prism. This approach has several advantages:\n- We don't need a YAML/JSON DSL embedded in OpenAPI documents _suitable to all\n  users_,\n- No need to further complicate [the existing Prism decision\n  engine](https://docs.stoplight.io/docs/prism/83dbbd75532cf-http-mocking#prism-decision-engine)\n  to accommodate partial or contradictory matches, and\n- Greater flexibility for a variety of client, server, and network constraints.\n\n## Solution\n\n```mermaid\nsequenceDiagram\nparticipant client as Client\nparticipant proxy as Example-Chooser Proxy\nparticipant prism as prism mock -d\n\nclient-\u003e\u003e+proxy: GET /pets/1 \u003cbr\u003e (without any headers)\nnote over proxy: custom logic inspects the request \u003cbr\u003e and adds the \"Prefer\" header\nproxy-\u003e\u003e+prism: GET /pets/1 \u003cbr\u003e Prefer: example=cat\nprism-\u003e\u003e-proxy: {\"id\":1, \"name\":\"Tiger\"}\nnote over proxy: the response is forwarded unchanged\nproxy-\u003e\u003e-client: {\"id\":1, \"name\":\"Tiger\"}\n```\n\n### Prerequisites\n\n- [Docker Compose](https://docs.docker.com/compose/install/#installation-scenarios)\n\n### Quick Start\n\nStart the example-chooser proxy with the following command. (Later you'll\nprobably want the `--detach` option, but omitting it now makes it easy to\nsee how the proxy interacts with Prism).\n\n```sh\ndocker compose -f docker-compose.yaml up\n```\n\nIn a different terminal, run the following commands:\n\n```sh\n% curl -s http://localhost:4010/pets/1\n{\"id\":1,\"name\":\"Fluffy\"}\n\n% curl -s http://localhost:4010/pets/2\n{\"id\":2,\"name\":\"Spot\"}\n\n% curl -v http://localhost:4010/pets/3\n{\"id\":-14714018,\"name\":\"lorem ipsum\"}\n```\n\nYour docker-compose session should have logged something like the following:\n\n```\n[+] Running 2/0\n ⠿ Container prism           Created                                                                          0.0s\n ⠿ Container examplechooser  Recreated                                                                        0.1s\nAttaching to examplechooser, prism\nexamplechooser  |  * Debug mode: off\nexamplechooser  | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\nexamplechooser  |  * Running on all addresses (0.0.0.0)\nexamplechooser  |  * Running on http://127.0.0.1:5000\nexamplechooser  |  * Running on http://192.168.128.2:5000\nexamplechooser  | Press CTRL+C to quit\nprism           | [2:40:46 AM] › [CLI] …  awaiting  Starting Prism…\nprism           | [2:40:49 AM] › [CLI] ℹ  info      GET        http://0.0.0.0:4010/pets/neque\nprism           | [2:40:49 AM] › [CLI] ▶  start     Prism is listening on http://0.0.0.0:4010\nexamplechooser  | [2023-03-11 02:40:51,100] INFO in app: added Prefer: example=cat\nprism           | [2:40:51 AM] › [HTTP SERVER] get /pets/1 ℹ  info      Request received\nprism           | [2:40:51 AM] ›     [NEGOTIATOR] ℹ  info      Request contains an accept header: */*\nprism           | [2:40:51 AM] ›     [VALIDATOR] ✔  success   The request passed the validation rules. Looking for the best response\nprism           | [2:40:51 AM] ›     [NEGOTIATOR] ✔  success   Found a compatible content for */*\nprism           | [2:40:51 AM] ›     [NEGOTIATOR] ✔  success   Responding with the requested status code 200\nexamplechooser  | 192.168.128.1 - - [11/Mar/2023 02:40:51] \"GET /pets/1 HTTP/1.1\" 200 -\nexamplechooser  | [2023-03-11 02:41:04,121] INFO in app: added Prefer: example=dog\nprism           | [2:41:04 AM] › [HTTP SERVER] get /pets/2 ℹ  info      Request received\nprism           | [2:41:04 AM] ›     [NEGOTIATOR] ℹ  info      Request contains an accept header: */*\nprism           | [2:41:04 AM] ›     [VALIDATOR] ✔  success   The request passed the validation rules. Looking for the best response\nprism           | [2:41:04 AM] ›     [NEGOTIATOR] ✔  success   Found a compatible content for */*\nprism           | [2:41:04 AM] ›     [NEGOTIATOR] ✔  success   Responding with the requested status code 200\nexamplechooser  | 192.168.128.1 - - [11/Mar/2023 02:41:04] \"GET /pets/2 HTTP/1.1\" 200 -\nprism           | [2:41:13 AM] › [HTTP SERVER] get /pets/3 ℹ  info      Request received\nprism           | [2:41:13 AM] ›     [NEGOTIATOR] ℹ  info      Request contains an accept header: */*\nprism           | [2:41:13 AM] ›     [VALIDATOR] ✔  success   The request passed the validation rules. Looking for the best response\nprism           | [2:41:13 AM] ›     [NEGOTIATOR] ✔  success   Found a compatible content for */*\nprism           | [2:41:13 AM] ›     [NEGOTIATOR] ✔  success   Responding with the requested status code 200\nexamplechooser  | 192.168.128.1 - - [11/Mar/2023 02:41:13] \"GET /pets/3 HTTP/1.1\" 200 -\n```\n\nIn particular, notice the log entries like the following, where the\nexample-chooser proxy has added a `Prefer:` header to the request it forwarded\nto Prism.\n\n```\nexamplechooser  | [2023-03-11 02:40:51,100] INFO in app: added Prefer: example=cat\n```\n\n## Customize\n\nThe logic in [app.py](./app.py) is very simple in this example.\n\n```python\ndef example_name(request):\n    if request.path.endswith('/1'):\n        return 'cat'\n    elif request.path.endswith('/2'):\n        return 'dog'\n    else:\n        return None\n```\n\nPrism users have expressed interest in varying example responses based on:\n- HTTP headers\n- query parameters\n- request body content\n\nAdditionally, you could load in the same OpenAPI file used by Prism and write\nlogic that depends on:\n- [named examples](https://swagger.io/docs/specification/adding-examples/),\n- [extension properties](https://swagger.io/docs/specification/openapi-extensions/), or\n- matching other properties of pre-made examples to the request\n\nNotice that the container built by [Dockerfile](./Dockerfile) is:\n- **Poorly suited to rapid-feedback development**; you'll probably want to use\n  [Flask's debug mode](https://flask.palletsprojects.com/en/2.2.x/cli/#debug-mode)\n  for debugging and/or automatic reloading;\n- **Poorly suited to any Internet-facing, public deployment**; this is an\n  internal development tool only.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstoplightio%2Fexamplechooserprismproxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstoplightio%2Fexamplechooserprismproxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstoplightio%2Fexamplechooserprismproxy/lists"}