diff --git a/CRUD-Python-CobiGen/files/context.xml b/CRUD-Python-CobiGen/files/context.xml new file mode 100644 index 00000000..4ecb8857 --- /dev/null +++ b/CRUD-Python-CobiGen/files/context.xml @@ -0,0 +1,168 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/CRUD-Python-CobiGen/files/initialCommit.md b/CRUD-Python-CobiGen/files/initialCommit.md new file mode 100644 index 00000000..d14d0d9d --- /dev/null +++ b/CRUD-Python-CobiGen/files/initialCommit.md @@ -0,0 +1,5 @@ +In order to upload the table declared in our "Model.py" we need a migration script. This command requires an attached message whenever it runs, for instance: + +`flask db migrate -m` "`first migration`" + +In this case, the terminal should print in return `Detected added table user`, among others. We can now upload the migration script. \ No newline at end of file diff --git a/CRUD-Python-CobiGen/files/postmanTest.md b/CRUD-Python-CobiGen/files/postmanTest.md new file mode 100644 index 00000000..018c3b5d --- /dev/null +++ b/CRUD-Python-CobiGen/files/postmanTest.md @@ -0,0 +1,22 @@ +We can test our application using Postman. Paste the following path on a new Postman tab: + +https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user + +Our table is empty for the moment, so we will select the POST method to add new data. We can submit JSON requests by clicking on the "Body" tab and selecting "raw". For example, submit: + + + { "name" : "elena", "age" : 26, "phone" : "555123", "email" : "elena@devon.com" } + + +Our new user should now be accessible from the database under "id" number 1. Check this by submitting a GET request to the following path: + +https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/1 + + +We will now edit our entry by sending a PUT request. Submit the following body to https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/1: + + { "phone" : "777123" } + +Now send another GET request and check whether the column was properly updated. + +Lastly, you can check the DELETE method by sending this request to https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/1. Then send a GET request to https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user. The application returns "User list is empty". diff --git a/CRUD-Python-CobiGen/files/user.yml b/CRUD-Python-CobiGen/files/user.yml new file mode 100644 index 00000000..01117901 --- /dev/null +++ b/CRUD-Python-CobiGen/files/user.yml @@ -0,0 +1,39 @@ +openapi: 3.0.0 +servers: + - url: 'https://localhost:8081/server/services/rest' + description: Just some data +info: + title: devonfw Example + description: Example of a API definition + version: 1.0.0 + x-rootpackage: com.devonfw.poc.jwtsample +paths: + /: + post: + responses: + '200': + description: Any +components: + schemas: + User: + x-component: usermanagement + description: Entity definiton of User + type: object + properties: + name: + type: string + maxLength: 100 + uniqueItems: true + age: + type: integer + phone : + type: string + maxLength: 100 + uniqueItems: true + email: + type: string + maxLength: 100 + uniqueItems: true + required: + - name + - email \ No newline at end of file diff --git a/CRUD-Python-CobiGen/index.asciidoc b/CRUD-Python-CobiGen/index.asciidoc new file mode 100644 index 00000000..f8ba074e --- /dev/null +++ b/CRUD-Python-CobiGen/index.asciidoc @@ -0,0 +1,60 @@ += Create a CRUD Python Flask application using CobiGen's OpenAPI plugin +==== +The aim of this tutorial is to create a basic CRUD Python Flask application from a single OpenAPI .yml file declaring an entity. + +The code will be generated using devonfw's CobiGen. This will result in a basic application layout, the declaration of a Flask-SQLAlchemy table with the entity's attributes as columns, and a service returning JSON objects for the GET, POST, PUT and DELETE methods. +==== + +[step] +-- +installDevonfwIde(["java","mvn"]) +installCobiGen() +executeCommand("devon","devon") +-- + +[step] +-- +adaptTemplatesCobiGen() +changeWorkspace(".cobigen/templates/CobiGen_Templates/src/main/templates") +cloneRepository("", "https://github.com/devonfw-forge/cobigen-python-templates.git") +-- + +[step] +-- +changeFile("context.xml", { "file": "files/context.xml"}) +-- + +[step] +-- +executeCommand("cd /root/devonfw/workspaces/main","cd /root/devonfw/workspaces/main") +changeWorkspace("devonfw/workspaces/main") +createFile("cobigen-python-example/user.yml", "files/user.yml") +-- + +[step] +-- +executeCommand("cobigen generate user.yml -i 1","cobigen generate user.yml -i 1",{"dir":"cobigen-python-example"}) +-- + +[step] +-- +executeCommand("python3 -m pip install virtualenv","python3 -m pip install virtualenv") +executeCommand("python3 -m virtualenv venv","python3 -m virtualenv venv") +executeCommand("activate.ps1","source activate", {"dir":"venv/bin"} ) +executeCommand("cd /root/devonfw/workspaces/main/cobigen-python-example","cd /root/devonfw/workspaces/main/cobigen-python-example") +executeCommand("pip install -r requirements.txt","pip install -r requirements.txt") +-- + +[step] +-- +executeCommand("flask db init","flask db init") +displayContent("Initial commit", [{ "file": "files/initialCommit.md" }]) +executeCommand("flask db upgrade","flask db upgrade") +-- + +[step] +-- +executeCommand("flask run --host=0.0.0.0 --port=80","flask run --host=0.0.0.0 --port=80") +displayContent("Postman test", [{ "file": "files/postmanTest.md" }]) +-- +