Flask RESTApi novato aquí
Estoy tratando de construir un servicio RESTapi en Flask (y estoy tratando de guardar el resultado como un archivo .txt ) usando flask_restful para un código mío usando el módulo pydifact de la siguiente manera:
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) ABSENDER_ID, EMPFÄNGER_ID, ERSTELLUNG_DATUM_ZEIT, REFERENCE deben ser entradas del usuario y deben estar en formato de cadena.
Cuando hago /RestAPI/<str:ABSENDER_ID>/<str:EMPFÄNGER_ID/<str:ERSTELLUNG_DATUM_ZEIT/<str:REFERENCE> , aparece el siguiente 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 existy cuando lo hago
/RestAPI/<int:ABSENDER_ID>/<int:EMPFÄNGER_ID/<int:ERSTELLUNG_DATUM_ZEIT/<int:REFERENCE> , aparece el siguiente 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>'Soy totalmente nuevo en esto y acabo de empezar a aprenderlo con la creación de una API REST con Python y Flask | Tutorial Flask-RESTful .
¿Alguien puede decirme cuál es el error que estoy cometiendo?
Tus rutas de URL tienen un problema. En el primero, debería ser una string en lugar de str y en el segundo falta > al final de int:EMPFÄNGER_ID e int:ERSTELLUNG_DATUM_ZEIT
Los correctos deben ser:
/RestAPI/<string:ABSENDER_ID>/<string:EMPFANGER_ID>/<string:ERSTELLUNG_DATUM_ZEIT>/<string:REFERENCE>y
/RestAPI/<int:ABSENDER_ID>/<int:EMPFANGER_ID>/<int:ERSTELLUNG_DATUM_ZEIT>/<int:REFERENCE> Nota: Ä con A en las URL anteriores porque eso también podría causar un problema de regla de URL mal formada.