Code Monkey home page Code Monkey logo

Comments (12)

thomaxxl avatar thomaxxl commented on August 13, 2024

I implemented a new decorator: jsonapi_rpc.
Can you try the following please?

from safrs.db import jsonapi_rpc
...
@classmethod
@jsonapi_rpc(http_methods = ['GET'])
def search(*args):
...

?

I am going to keep the jsonapi_rpc name but the generated swagger schema will need to be improved.

from safrs.

DennyWeinberg avatar DennyWeinberg commented on August 13, 2024

OK, I will try this out. Thanks.
I will let you know if this fits.

from safrs.

thomaxxl avatar thomaxxl commented on August 13, 2024

An example is implemented here: https://github.com/thomaxxl/safrs/blob/master/examples/demo_relationship.py

from safrs.

thomaxxl avatar thomaxxl commented on August 13, 2024

solved

from safrs.

harshal1916 avatar harshal1916 commented on August 13, 2024

Hi my i am using Flask-Python for rest api creation,
i have my two different .py(i.e models.py,Init.py).
I have "EmployeeInfo" class in my "model.py" file and "get_dept_info" in my init.py file.

I cannot change the structure. Following Are my class and method declaration.

class EmployeeInfo(SAFRSBase,db.Model):
'''
description: EmployeeInfo
'''
tablename = 'employee_info'
employee_id = db.Column(db.String(45), primary_key=True, nullable=False)
first_name = db.Column(db.String(30), nullable=False)

@classmethod
@jsonapi_rpc(http_methods=['POST','GET'])
@login_required
def get_dept_info():
"""
Find different type of dept present in employee_info table
"""
try:

    with get_session() as session:
        # employee_info

        dept_details_list = session.query(EmployeeInfo.dept_id, EmployeeInfo.dept_name).distinct().all()

        if dept_details_list:

            # Adding an option of 'Other' for 'dept_name'.
            # This option will be used for scenarios where visitors does not know about
            # department of employee (contact person)
            dept_details_list.append({'dept_name': 'Other', 'dept_id': 999})

            dept_details_schema_list = DeptDetailsSchema(many=True)

            response = dept_details_schema_list.dump(dept_details_list)  # python object [List, Dictionary]
            # response = users_schema.dumps(all_users)  # json encoded string object

            return make_response(jsonify(response), 200)

        else:
            message = 'No departments found in EmployeeInfo table'
            return make_400_response(message, 400)

except Exception as e:
    print(e)
    message = 'Exception Occurred : Failed to fetch department details'
    message = utils.generate_exception_message(message, e)

    return make_400_response(message, 400)

I want only methods created by me in the swaggerUI.
but by using following decorations on the method i am not able to the see the api endpoint in the swagger.

from safrs.

thomaxxl avatar thomaxxl commented on August 13, 2024

Hi,

can you open a new issue for new problems please?
The syntax of your get_dept_info is missing an argument, f.i.:

    @classmethod
    @jsonapi_rpc(http_methods=['POST','GET'])
    def get_dept_info(cls):
        return {1:1}

from safrs.

harshal1916 avatar harshal1916 commented on August 13, 2024

Hi my i am using Flask-Python for rest api creation,
i have my two different .py(i.e models.py,Init.py).
I have "EmployeeInfo" class in my "model.py" file and "get_dept_info" in my init.py file.

I cannot change the structure. Following Are my class and method declaration.

class EmployeeInfo(SAFRSBase,db.Model):
'''
description: EmployeeInfo
'''
tablename = 'employee_info'
employee_id = db.Column(db.String(45), primary_key=True, nullable=False)
first_name = db.Column(db.String(30), nullable=False)

@classmethod
@jsonapi_rpc(http_methods=['POST','GET'])
@login_required
def get_dept_info():
"""
Find different type of dept present in employee_info table
"""
try:

    with get_session() as session:
        # employee_info

        dept_details_list = session.query(EmployeeInfo.dept_id, EmployeeInfo.dept_name).distinct().all()

        if dept_details_list:

            # Adding an option of 'Other' for 'dept_name'.
            # This option will be used for scenarios where visitors does not know about
            # department of employee (contact person)
            dept_details_list.append({'dept_name': 'Other', 'dept_id': 999})

            dept_details_schema_list = DeptDetailsSchema(many=True)

            response = dept_details_schema_list.dump(dept_details_list)  # python object [List, Dictionary]
            # response = users_schema.dumps(all_users)  # json encoded string object

            return make_response(jsonify(response), 200)

        else:
            message = 'No departments found in EmployeeInfo table'
            return make_400_response(message, 400)

except Exception as e:
    print(e)
    message = 'Exception Occurred : Failed to fetch department details'
    message = utils.generate_exception_message(message, e)

    return make_400_response(message, 400)

I want only methods created by me in the swaggerUI.
but by using following decorations on the method i am not able to the see the api endpoint in the swagger.

from safrs.

thomaxxl avatar thomaxxl commented on August 13, 2024

Hi,

Can you check if it works for you?
https://gist.github.com/thomaxxl/8558210e3064cc205f290259493292fb

from safrs.

harshal1916 avatar harshal1916 commented on August 13, 2024

@thomaxxl thank you for the quick reply i will try this solution, though i have some questions

  1. I am using Flask core functionality and blueprint for creation of project, we are not using restful/restplus anywhere in the project. will this plugin support that architecture.

  2. My model class and my method are are separate files how can this code will be able to handle that.

from safrs.

thomaxxl avatar thomaxxl commented on August 13, 2024
  1. Yes, safrs just registers a different blueprint at the prefix which can be provided as an arg to SAFRSAPI
  2. Yes, you have you assign the method to the class before exposing it

from safrs.

harshal1916 avatar harshal1916 commented on August 13, 2024

@thomaxxl i tried those things but i have one main question to ask

I only want Methods that are exposed externally i don't want any automatically generated api endpoint i want following structure is it possible to get the same directly or do we need to change the code a bit.
2020-04-29

from safrs.

thomaxxl avatar thomaxxl commented on August 13, 2024

Hi,

you'd have to modify the _api.Api class for that:
the endpoints are created with the Api.expose_* methods (which call Api.add_resource())

so, if you add a return directly after expose_methods(), that would be what you want.

...
        # Expose the methods first
        self.expose_methods(url_prefix, tags=tags)
        return # << Add the return here
...

Safrs is mainly designed to provide crud operations though, so I'm not sure if this is the right framework for what you're trying to do.

from safrs.

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.