Git Product home page Git Product logo

Comments (8)

grktsh avatar grktsh commented on August 23, 2024 1

Yes, but SomeResource(...) is lambda: SomeResource(...) since it is instantiated in the factory.

I agree to add helpers to avoid extensions.IMPLEMENTATION. I'd like to use OpenAPI terms. For example:

resolve_security_scheme(spec_dict, 'api_key', api_key_validator)
resolve_path_item(spec_dict, '/some/path', lambda: SomeResource(...))

from falcon-oas.

sisp avatar sisp commented on August 23, 2024 1

Yes, but SomeResource(...) is lambda: SomeResource(...) since it is instantiated in the factory.

Good point. In fact, this can be hidden in resolve_path_item:

def resolve_path_item(spec_dict, path, resource):
    spec_dict['paths'][path][extensions.IMPLEMENTATION] = lambda: resource

resolve_path_item(spec_dict, '/some/path', SomeResource(...))

I think it's a bit more intuitive without the explicit lambda.

from falcon-oas.

grktsh avatar grktsh commented on August 23, 2024 1

Either is fine.
If 2, I'd like to avoid mutating the given spec_dict and mutate Spec.data directly or by Spec.__getitem__.
I don't worry about the one-liner since I actually use multiple middlewares:

oas = OAS(spec_dict, ...)
app = oas.create_api(middleware=[..., oas.middleware, ...])

from falcon-oas.

grktsh avatar grktsh commented on August 23, 2024

How about mutating spec_dict to add x-falcon-oas-implementation key and the handler value before passing it to the factory? In this case it is necessary to add the guard clause (maybe if not isinstance(name, six.string_types): return name) to import_string function but this approach is simple and also applicable to the request handlers.

from falcon-oas.

sisp avatar sisp commented on August 23, 2024

Do you mean something like this?

from falcon_oas import extensions

def api_key_validator(value, scopes, request):
    # ...

spec_dict = load_spec(...)
spec_dict['components']['securitySchemes']['api_key'][extensions.IMPLEMENTATION] = api_key_validator
spec_dict['paths']['/some/path'][extensions.IMPLEMENTATION] = SomeResource(...)

api = OAS(spec_dict).create_api()

# ...

As you said, then import_string would return name as is when it is not a string.

from falcon-oas.

sisp avatar sisp commented on August 23, 2024

How about adding helpers to make it a bit more convenient?

set_security(spec_dict, 'api_key', api_key_validator)
set_resource(spec_dict, '/some/path', SomeResource())

from falcon-oas.

sisp avatar sisp commented on August 23, 2024

I like it! I'll close #26 in favor of a new PR, that implements your suggestion, later today.

from falcon-oas.

sisp avatar sisp commented on August 23, 2024

One more thing: Which option would you prefer?

  1. Make the two helpers independent functions that modify the spec_dict explicitly (as discussed above).
  2. Make the two helpers instance methods of the OAS class to hide the implementation detail that spec_dict (or rather self.spec then) is mutated.
    oas = OAS(spec_dict)
    oas.resolve_security_scheme('api_key', api_key_validator)
    oas.resolve_path_item('/some/path', SomeResource(...))
    api = oas.create_api()
    If you're concerned about losing the one-liner api = OAS(spec_dict).create_api(), the two methods could return self allowing:
    api = OAS(spec_dict)
        .resolve_security_scheme('api_key', api_key_validator)
        .resolve_path_item('/some/path', SomeResource(...))
        .create_api()

I tend to prefer 2. because it feels more like a real feature to me rather than an officially supported "trick".

from falcon-oas.

Related Issues (7)

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.