Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用微服务的Python Falcon微服务?_Python_Rest_Falconframework - Fatal编程技术网

使用微服务的Python Falcon微服务?

使用微服务的Python Falcon微服务?,python,rest,falconframework,Python,Rest,Falconframework,我刚刚开始学习Falcon,我正在尝试做一些我认为应该非常简单和基本的事情。我只是希望我的一个服务向另一个服务发送请求 让我用一个小的代码示例使它变得更简单。这里有两个微服务。服务1存储一个数字,服务2增加一个存储的数字 class NumberResource: def on_post(self, req, resp): self.value = req.media.get('value') resp.media = {'value': self.val

我刚刚开始学习Falcon,我正在尝试做一些我认为应该非常简单和基本的事情。我只是希望我的一个服务向另一个服务发送请求

让我用一个小的代码示例使它变得更简单。这里有两个微服务。服务1存储一个数字,服务2增加一个存储的数字

class NumberResource:
    def on_post(self, req, resp):
        self.value = req.media.get('value')
        resp.media = {'value': self.value}

    def on_get(self, req, resp):
        resp.media = {'value': self.value}

class NextNumberResource:
    def on_get(self, req, resp):
        sibling = {}
        # TODO sibling = get("/number")
        resp.media = {'value': sibling.value+1}

api = falcon.API()
api.add_route('/number', NumberResource())
api.add_route('/next', NextNumberResource())
现在我可以存储数字17:

curl http://localhost:8000/number -d '{"value":17}'
我可以检索存储的值:

curl http://localhost:8000/number
{"value": 17}
我想填写TODO行,以便检索计算值:

curl http://localhost:8000/next
{"value": 18}

最好的方法是什么?

最好的方法是通过Python中的OOP模式。一个例子可能是这样的(尽管它需要更多的防御性编程+序列化/反序列化)


最好的方法是通过Python中的OOP模式。一个例子可能是这样的(尽管它需要更多的防御性编程+序列化/反序列化)


在定义之前,您正在使用
SomeLogicHandler
。这真的对你有用吗?谢谢你的更新。我通常将所有逻辑处理程序解耦到一个单独的包中,并将它们导入API路由处理程序中。我在上面的回复中没有提到这一点。我会更新的。再次感谢。在定义之前,您正在使用
SomeLogicHandler
。这真的对你有用吗?谢谢你的更新。我通常将所有逻辑处理程序解耦到一个单独的包中,并将它们导入API路由处理程序中。我在上面的回复中没有提到这一点。我会更新的。再次感谢。
class SomeLogicHandler():

    myValue #Important to have myValue as class variable and not as instance variable.

    def __init__(self):
        super(SomeLogicHandler, self).__init()
        self.updateMyValue = self._updateMyValue
        self.getMyValue = self._getMyValue

    def _updateMyValue(self, value):
        self.myValue+= value

    def _getMyValue(self):
        return {value: self.myValue}

class ApiRouteHandler(SomeLogicHandler):

    def __init__(self):
        super(ApiRouteHandler, self).__init__()

    def on_post(self, req, resp):
        #post implementation.
        self.updateMyValue(req.media.get('value'))


class ApiRouteHandler2(SomeLogicHandler):

    def __init__(self):
        super(ApiRouteHandler2, self).__init__()

    def on_get(self, req, resp):
        #get implementation.
        resp.body = self.getMyValue()