# edgecontext
Python Documentation: https://reddit-edgecontext.readthedocs.io/en/latest/
Go Documentation: https://pkg.go.dev/github.com/reddit/edgecontext/lib/go/edgecontext
Services deep within the backend often need to know information about the
client that originated the request, such as what user is authenticated or what
country they're in. Baseplate services can get this information from the edge
context which is automatically propagated along with calls between services.
This library provides a Thrift specification of an edge context payload and a
corresponding implementation of the [`EdgeContextFactory`] interface from
Baseplate.py.
[`EdgeContextFactory`]: https://baseplate.readthedocs.io/en/latest/api/baseplate/lib/edgecontext.html
And an implementation of [`ecinterface`] from Baseplate.go.
[`ecinterface`]: https://pkg.go.dev/github.com/reddit/baseplate.go/ecinterface
## Usage
### Python
Add the `EdgeContextFactory` to application startup:
```python
from baseplate import Baseplate
from baseplate.lib.secrets import secrets_store_from_config
from reddit_edgecontext import EdgeContextFactory
def make_processor(app_config):
secrets = secrets_store_from_config(app_config, timeout=60)
edgecontext_factory = EdgeContextFactory(secrets)
# pass edgecontext_factory to your framework's integration
# for Thrift: baseplate.frameworks.thrift.baseplateify_processor
# for Pyramid: baseplate.frameworks.pyramid.BaseplateConfigurator
```
Then read fields while handling requests:
```python
def my_view(request):
return request.edge_context.user.id
```
See [the documentation] for all the available fields.
[the documentation]: https://reddit-edgecontext.readthedocs.io/en/latest/
### Go
Use [`edgecontext.Factory`] to create an [`ecinterface.Factory`] implementation
that's expected by [`baseplate.New`]:
```go
ctx, bp, err := baseplate.New(context.Background(), baseplate.NewArgs{
ConfigPath: configPath,
ServiceCfg: &cfg, // or nil if you don't have additional config to parse
EdgeContextFactory: edgecontext.Factory(edgecontext.Config{
Logger: log.ErrorWithSentryWrapper(),
}),
})
```
When using it, get the [`*EdgeRequestContext`] object out of context:
```go
if ec, ok := edgecontext.GetEdgeContext(ctx); ok {
user := ec.User()
loid, ok := user.LoID()
// Do something with loid
}
```
[`edgecontext.Factory`]: https://pkg.go.dev/github.com/reddit/edgecontext/lib/go/edgecontext#Factory
[`ecinterface.Factory`]: https://pkg.go.dev/github.com/reddit/baseplate.go/ecinterface#Factory
[`baseplate.New`]: https://pkg.go.dev/github.com/reddit/baseplate.go#New
[`*EdgeRequestContext`]: https://pkg.go.dev/github.com/reddit/edgecontext/lib/go/edgecontext#EdgeRequestContext
## Development
A Dockerfile is provided to get a development environment running. To use it,
build the base Docker image:
```console
$ docker build -t edgecontext .
```
And then fire up the environment and use the provided Makefile targets to do
common tasks:
```console
$ docker run -it -v $PWD:/src --user "$(id -u):$(id -g)" -w /src edgecontext
$ make fmt
```
The following make targets are provided:
* `fmt`: Apply automatic formatting to the source code.
* `thrift`: Generate code from the Thrift IDL. Run `fmt` after doing this.
* `lint`: Run linters on the code.
* `test`: Run the test suite.
* `docs`: Build docs.
* Python output can be found in `lib/py/build/html/`.
The generated Thrift code is committed to the Git repo, so if you change
`edgecontext.thrift` make sure to run `make thrift fmt` and commit those
changes as well.
For Go, we do the same linting checks as Baseplate.go,
so please follow Baseplate.go's [Editor] guide to make sure you are doing the
same linting locally correctly.
Please also follow Baseplate.go's [Style] guide for code style.
[Editor]: https://github.com/reddit/baseplate.go/blob/master/Editor.md
[Style]: https://github.com/reddit/baseplate.go/blob/master/Style.md
Raw data
{
"_id": null,
"home_page": "https://github.com/reddit/edgecontext",
"name": "reddit-edgecontext",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": null,
"author": "reddit",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/6f/6a/8632746c7fa7ea6b90397364bdd0aaadba4c6aa20ddbc152cf23206e52fa/reddit_edgecontext-1.8.0.tar.gz",
"platform": null,
"description": "# edgecontext\n\nPython Documentation: https://reddit-edgecontext.readthedocs.io/en/latest/\n\nGo Documentation: https://pkg.go.dev/github.com/reddit/edgecontext/lib/go/edgecontext\n\nServices deep within the backend often need to know information about the\nclient that originated the request, such as what user is authenticated or what\ncountry they're in. Baseplate services can get this information from the edge\ncontext which is automatically propagated along with calls between services.\n\nThis library provides a Thrift specification of an edge context payload and a\ncorresponding implementation of the [`EdgeContextFactory`] interface from\nBaseplate.py.\n\n[`EdgeContextFactory`]: https://baseplate.readthedocs.io/en/latest/api/baseplate/lib/edgecontext.html\n\nAnd an implementation of [`ecinterface`] from Baseplate.go.\n\n[`ecinterface`]: https://pkg.go.dev/github.com/reddit/baseplate.go/ecinterface\n\n## Usage\n\n### Python\n\nAdd the `EdgeContextFactory` to application startup:\n\n```python\nfrom baseplate import Baseplate\nfrom baseplate.lib.secrets import secrets_store_from_config\nfrom reddit_edgecontext import EdgeContextFactory\n\n\ndef make_processor(app_config):\n secrets = secrets_store_from_config(app_config, timeout=60)\n edgecontext_factory = EdgeContextFactory(secrets)\n\n # pass edgecontext_factory to your framework's integration\n # for Thrift: baseplate.frameworks.thrift.baseplateify_processor\n # for Pyramid: baseplate.frameworks.pyramid.BaseplateConfigurator\n```\n\nThen read fields while handling requests:\n\n```python\ndef my_view(request):\n return request.edge_context.user.id\n```\n\nSee [the documentation] for all the available fields.\n\n[the documentation]: https://reddit-edgecontext.readthedocs.io/en/latest/\n\n### Go\n\nUse [`edgecontext.Factory`] to create an [`ecinterface.Factory`] implementation\nthat's expected by [`baseplate.New`]:\n\n```go\nctx, bp, err := baseplate.New(context.Background(), baseplate.NewArgs{\n ConfigPath: configPath,\n ServiceCfg: &cfg, // or nil if you don't have additional config to parse\n EdgeContextFactory: edgecontext.Factory(edgecontext.Config{\n Logger: log.ErrorWithSentryWrapper(),\n }),\n})\n```\n\nWhen using it, get the [`*EdgeRequestContext`] object out of context:\n\n```go\nif ec, ok := edgecontext.GetEdgeContext(ctx); ok {\n user := ec.User()\n loid, ok := user.LoID()\n // Do something with loid\n}\n```\n\n[`edgecontext.Factory`]: https://pkg.go.dev/github.com/reddit/edgecontext/lib/go/edgecontext#Factory\n[`ecinterface.Factory`]: https://pkg.go.dev/github.com/reddit/baseplate.go/ecinterface#Factory\n[`baseplate.New`]: https://pkg.go.dev/github.com/reddit/baseplate.go#New\n[`*EdgeRequestContext`]: https://pkg.go.dev/github.com/reddit/edgecontext/lib/go/edgecontext#EdgeRequestContext\n\n## Development\n\nA Dockerfile is provided to get a development environment running. To use it,\nbuild the base Docker image:\n\n```console\n$ docker build -t edgecontext .\n```\n\nAnd then fire up the environment and use the provided Makefile targets to do\ncommon tasks:\n\n```console\n$ docker run -it -v $PWD:/src --user \"$(id -u):$(id -g)\" -w /src edgecontext\n$ make fmt\n```\n\nThe following make targets are provided:\n\n* `fmt`: Apply automatic formatting to the source code.\n* `thrift`: Generate code from the Thrift IDL. Run `fmt` after doing this.\n* `lint`: Run linters on the code.\n* `test`: Run the test suite.\n* `docs`: Build docs.\n * Python output can be found in `lib/py/build/html/`.\n\nThe generated Thrift code is committed to the Git repo, so if you change\n`edgecontext.thrift` make sure to run `make thrift fmt` and commit those\nchanges as well.\n\nFor Go, we do the same linting checks as Baseplate.go,\nso please follow Baseplate.go's [Editor] guide to make sure you are doing the\nsame linting locally correctly.\nPlease also follow Baseplate.go's [Style] guide for code style.\n\n[Editor]: https://github.com/reddit/baseplate.go/blob/master/Editor.md\n[Style]: https://github.com/reddit/baseplate.go/blob/master/Style.md\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "reddit edge request context baggage",
"version": "1.8.0",
"project_urls": {
"Documentation": "https://reddit-edgecontext.readthedocs.io/",
"Homepage": "https://github.com/reddit/edgecontext"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "205225cfac236043c49e476933c1a841f0b5bc7665efdaecd1cc527fcefd3218",
"md5": "43797f9db87b7cf144f995f18c7ce228",
"sha256": "634116f92ff486fe9afb2b25267e07ef5e9ce499b92fffc08467793535e5fa63"
},
"downloads": -1,
"filename": "reddit_edgecontext-1.8.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "43797f9db87b7cf144f995f18c7ce228",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 12336,
"upload_time": "2025-07-11T21:27:19",
"upload_time_iso_8601": "2025-07-11T21:27:19.689762Z",
"url": "https://files.pythonhosted.org/packages/20/52/25cfac236043c49e476933c1a841f0b5bc7665efdaecd1cc527fcefd3218/reddit_edgecontext-1.8.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6f6a8632746c7fa7ea6b90397364bdd0aaadba4c6aa20ddbc152cf23206e52fa",
"md5": "0db23c8c54c1f4e85658af1e26a17c61",
"sha256": "27690a858b38adef03aea9ffc797a0b18cbd1030364e76b8539a315b8303bad7"
},
"downloads": -1,
"filename": "reddit_edgecontext-1.8.0.tar.gz",
"has_sig": false,
"md5_digest": "0db23c8c54c1f4e85658af1e26a17c61",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 22115,
"upload_time": "2025-07-11T21:27:20",
"upload_time_iso_8601": "2025-07-11T21:27:20.747490Z",
"url": "https://files.pythonhosted.org/packages/6f/6a/8632746c7fa7ea6b90397364bdd0aaadba4c6aa20ddbc152cf23206e52fa/reddit_edgecontext-1.8.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-11 21:27:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "reddit",
"github_project": "edgecontext",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "reddit-edgecontext"
}