Code Monkey home page Code Monkey logo

Comments (8)

dmig-alarstudios avatar dmig-alarstudios commented on May 18, 2024

I expected to get nothing here

from starlette-context.

tomwojcik avatar tomwojcik commented on May 18, 2024

If you got this error, it means one of three things.

Anyway, please share the code with me. You said here's a testcase without providing a test case.

from starlette-context.

dmig-alarstudios avatar dmig-alarstudios commented on May 18, 2024

My case is third: I tried to access context outside of the request-response cycle. During another middleware install.

from starlette-context.

VukW avatar VukW commented on May 18, 2024

I've also get the third case, but i want to use context optionally, only if it already exists.
Are the any ways to check if context is already available?

from starlette-context.

tomwojcik avatar tomwojcik commented on May 18, 2024
  1. If you want to use it in another middleware, it needs to be added after the ContextMiddleware is added to the asgi app. For example request/response logger.
  2. If you want to access it in a route, it will always be available as all requests that hit routes are 'past middlewares'.
  3. If you access it outside of the request-response cycle, it will never be available, as we use this new pythonic ContextVar and that's how this thing works.

I think that's the price you need to pay for using asgi apps. You can't rely on threads and that's the beauty/ugly of using async code.

Therefore, even if there is an option to check if it's available, it makes no sense to implement it as you either use it properly or not.

You can handle the exception that is raised - RuntimeError - if you want to access it only if it's available.

from starlette-context.

VukW avatar VukW commented on May 18, 2024

Sorry if I've described not enough clearly
I want to use context in the function that may be called outside of any request.
If my function is called inside request, I want to get request-id (using your cool RequestIdPlugin) and use it somehow. However, if there is no request, and no any context - I want just to execute all my another staff, context absence is not a problem for that case.

Now I've used just try-except:

request_id is None
try:
  request_id = context.data['X-Request-ID']
except RuntimeError:
   pass

but I'll be glad to use any explicit check instead of catching a whole class of RuntimeErrors , like this:

request_id = None
if context_exist():
   request_id = ...

from starlette-context.

tomwojcik avatar tomwojcik commented on May 18, 2024

I still don't understand what is the case when that'd be useful and when one might not know whether context is available or not but I will implement this. Makes sense to have it instead of try/except.
Will do that over the weekend. Thanks for reporting this feature request.

Also, I tried to mimic dict as much as possible. The only thing you can't do with context, is unpack it with **context, you would need to do **context.data (same with serialization). All other dict operations like context['X-Request-ID'] should work without accessing .data property directly.

But whatever fits you best : )

from starlette-context.

VukW avatar VukW commented on May 18, 2024

Thank you for merging!
I'm sorry I've not implemented tests by myself, by some unknown reasons I was not successful in running tests on my machine.

Also, thank you for the package, it is as simple as useful for me:)

from starlette-context.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.