Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while building a RESTApi using flask_restful

Flask RESTApi newbie here

I am trying to build a RESTapi service in Flask (and I am trying to save the output as a .txt file) using flask_restful for a code of mine using the pydifact module as follows:

import datetime
from pydifact.message import Message
from pydifact.segments import Segment
from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)

api = Api(app)


class RestAPI(Resource):
    def get(self, ABSENDER_ID, EMPFÄNGER_ID, ERSTELLUNG_DATUM_ZEIT, REFERENCE):
        MSCONS = Message()

        def erstellung_datum_zeit(dt_time):
            # Needed for the UNB segment
            dt_time = dt_time.strftime('%Y%m%d%H%M')
            return dt_time

        def UNA_UNB_segment(absender_id, empfänger_id, erst_datum, ref):

            MSCONS.add_segment(Segment('UNA', ":+.? '"))
            MSCONS.add_segment(Segment('UNB', ['UNOC', '3'], [absender_id, '14'], [
                               empfänger_id, '500'], [erst_datum[2:8], erst_datum[8:]], ref, '', 'TL'))

        ERSTELLUNG_DATUM_ZEIT = str(
            erstellung_datum_zeit(datetime.datetime.now()))
        UNA_UNB_segment(ABSENDER_ID, EMPFÄNGER_ID,
                        ERSTELLUNG_DATUM_ZEIT, REFERENCE)
        result = MSCONS.serialize()
        final_result = result

        PATH_FOR_TXT = r'C:\Users\kashy\OneDrive\Desktop\Codes\mscons.txt'
        textfile = open(PATH_FOR_TXT, 'w')
        textfile.write(result)
        textfile.close()

        return {'result': final_result}


api.add_resource(
    RestAPI,
    '/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>')

if __name__ == '__main__':
    app.run(debug=True)

The ABSENDER_ID, EMPFÄNGER_ID, ERSTELLUNG_DATUM_ZEIT, REFERENCE should all be user inputs and they should be all in string format.

When I do /RestAPI/<str:ABSENDER_ID>/<str:EMPFÄNGER_ID/<str:ERSTELLUNG_DATUM_ZEIT/<str:REFERENCE>, i get the following error:

C:\Users\kashy\OneDrive\Desktop\Codes\pydifact> & C:/Users/kashy/Anaconda3/envs/py36/python.exe c:/Users/kashy/OneDrive/Desktop/Codes/api.py
Traceback (most recent call last):
  File "c:/Users/kashy/OneDrive/Desktop/Codes/api.py", line 44, in <module>
    self.url_map.add(rule)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 1401, in add
    rule.bind(self)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 730, in bind
    self.compile()
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 790, in compile
    _build_regex(self.rule if self.is_leaf else self.rule.rstrip("/"))
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 779, in _build_regex
    convobj = self.get_converter(variable, converter, c_args, c_kwargs)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 738, in get_converter
    raise LookupError("the converter %r does not exist" % converter_name)
LookupError: the converter 'str' does not exist

and when I do

/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>, I get the following error:

PS C:\Users\kashy\OneDrive\Desktop\Codes\pydifact> & C:/Users/kashy/Anaconda3/envs/py36/python.exe c:/Users/kashy/OneDrive/Desktop/Codes/api.py
Traceback (most recent call last):
  File "c:/Users/kashy/OneDrive/Desktop/Codes/api.py", line 44, in <module>
    '/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>')
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask_restful\__init__.py", line 382, in add_resource
    self._register_view(self.app, resource, *urls, **kwargs)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask_restful\__init__.py", line 448, in _register_view
    app.add_url_rule(rule, view_func=resource_func, **kwargs)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask\app.py", line 98, in wrapper_func
    return f(self, *args, **kwargs)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\flask\app.py", line 1277, in add_url_rule
    self.url_map.add(rule)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 1401, in add
    rule.bind(self)
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 730, in bind
    self.compile()
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 790, in compile
    _build_regex(self.rule if self.is_leaf else self.rule.rstrip("/"))
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 766, in _build_regex
    for converter, arguments, variable in parse_rule(rule):
  File "C:\Users\kashy\Anaconda3\envs\py36\lib\site-packages\werkzeug\routing.py", line 226, in parse_rule
    raise ValueError("malformed url rule: %r" % rule)
ValueError: malformed url rule: '/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE>'

I am totally new to this and just started learning it using the Building a REST API using Python and Flask | Flask-RESTful tutorial.

Can anyone please tell me what is the mistake I am doing?

like image 230
reinhardt Avatar asked Dec 08 '22 11:12

reinhardt


1 Answers

Your url routes have problem. In the first one, it should be string instead of str and in the second one you have a missing > at the end of int:EMPFÄNGER_ID and int:ERSTELLUNG_DATUM_ZEIT

Correct ones should be:

/RestAPI/<string:ABSENDER_ID>/<string:EMPFANGER_ID>/<string:ERSTELLUNG_DATUM_ZEIT>/<string:REFERENCE>

and

/RestAPI/<int:ABSENDER_ID>/<int:EMPFANGER_ID>/<int:ERSTELLUNG_DATUM_ZEIT>/<int:REFERENCE>

Note: I replaced Ä with A in urls above because that might also cause malformed url rule issue.

like image 75
Zeeshan Avatar answered Dec 28 '22 06:12

Zeeshan