Calling flask restful API resource methods

前端 未结 3 1094
眼角桃花
眼角桃花 2021-02-01 22:52

I\'m creating an API with Flask that is being used for a mobile platform, but I also want the application itself to digest the API in order to render web content. I\'m wondering

3条回答
  •  余生分开走
    2021-02-01 23:19

    The obvious way for your application to consume the API is to invoke it like any other client. The fact that the application would be acting as a server and a client at the same time does not matter, the client portion can place requests into localhost and the server part will get them in the same way it gets external requests. To generate HTTP requests you can use requests, or urllib2 from the standard library.

    But while the above method will work just fine it seems overkill to me. In my opinion a better approach is to expose the common functionality of your application in a way that both the regular application and the API can invoke. For example, you could have a package called FooLib that implements all the shared logic, then FooAPI becomes a thin wrapper around FooLib, and both FooAPI and FooApp call FooLib to get things done.

提交回复
热议问题