How to change app.py behavior

hi
i want to create a custom route for my function
i found in frappe/app.py
there is application method that direct the request if it start with /api for example
i want to add a condition there of find a way to direct the request from routes start with /v2 for example to my custom app method … without change at frappe core …

What’s the route you’re trying to redirect?
You may be able to do this with a hookable method.

hi @tmatteson
the task i should create a route for a rest api for the company
besaclly it should be v1/[doctype]/[name]
the stander frappe way it give you the routes for functions or rest if it start with api/method i want v1 instaid
if there is a hook point to redirect a specific route to a function please share it with me

I think the route approach is too many changes. Some API designs allow you to specify a version which is sometimes a semver (1.1.1) or a date(2018-10-01). You can trigger on that instead. This would definately be hookable with normal doc events. Amazon does it as a date for what its worth.

hi tylor
i don’t think it’s hook-able to do something like this (flask way for example )
@app.route(’/form-example’, methods=[‘GET’, ‘POST’]) #allow both GET and POST requests

I think we’re talking about different things. You want to do some CRUD operations on a document, right? I’m saying that you can hook those with the existing route (domain.com/api/resource/Attendence) and make you V1/ V2/ VX decision by putting a flag in the JSON and having that run with one of the hooked methods.

you are totally right , yes we can but the company want me to do the api this way www.company.com/v1/Farmer <- Rest api
i can do it your way but will use the api/resource or api/method …
i manage to do it by change method in app.application
https://github.com/frappe/frappe/blob/develop/frappe/app.py#L57
add if statement to map to my custom doctype api methods … but i don’t want to touch the frappe app i want to customize it with out changing code in it … with a hook for example or override app.py method

@ahmadRagheb

you can do it overriding the function into the first __init__.py of your module

Check this

https://github.com/mxmo-co/jstudio/blob/master/studio/init.py#L195https://github.com/mxmo-co/jstudio/blob/master/studio/__init__.py#L195

I’m creating an handling for /api/action

3 Likes

The real magic is here

1 Like

@tmatteson yes you’re partially right, the magic is there, but it only works if it’s done on first __init__.py

It’s because how python packaging system works.

@max_morais_dmm thanks a lot it’s great, that’s what i needed exactly <3

This topic is interesting. And the solution might work for others (i.e me).
But the link is not working anymore (GitHub says Not found).
Perhaps you can put it on this page in the code tag?
Thank you.

1 Like

the concept calls monkey patches in Python …
you can do the same concept in JS

@ahmadRagheb @max_morais_dmm

the links are dead. can you provide the links that work?

i use this as a reference , and the same structure when I want to monkey patch anything .

[ElasticRun / latte]
follow the monkey patch folder you will understand everything

@ahmadRagheb
thanks for quick reply


which file to refer to give support for /api/action?

in the latte app they didn’t override the api path
you will have to do it by yourself , it’s the same concept if you understand monkeypatch itself . you have to look here

you need to override this function with your function that will handle /action

Did you use mock library to patch or some other way?

An example code would be nice since all the links are down.
@max_morais_dmm @ahmadRagheb @tmatteson

Hi,
I want to achieve same. But the above file is missing

@lokesh I didn’t try to do this in version-13. i will give it a shot tomorrow and if it works I will write here how I did it.

btw this link still working
https://github.com/ElasticRun/latte/blob/master/latte/init.py