|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "e81cfb25-3536-44cd-ace0-1061764fcf98", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "#### Importo le librerie che mi servono\n", |
| 9 | + "#### Il client utilizzerà la libreria requests per interagire con il server" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "id": "b03cdde7-7728-4400-93f3-4e1909d43c68", |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "import requests" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "id": "78e8e4cb-6d72-4615-87bd-0ee0cae1de02", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "#### E' necessario specificare il link host (BASE_URL) in quanto il client deve sapere dove inviare le richieste HTTP\n", |
| 28 | + "#### In questo caso il link è composto da una prima parte (127.0.0.1, ovvero il pc locale) e da una seconda parte (:5000, ovvero la porta del server creata da flask)." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 2, |
| 34 | + "id": "4aa76c23-4cb5-4a17-abcd-536a0bc16341", |
| 35 | + "metadata": {}, |
| 36 | + "outputs": [], |
| 37 | + "source": [ |
| 38 | + "BASE_URL = \"http://127.0.0.1:5000\"" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "markdown", |
| 43 | + "id": "96891375-25aa-4c91-b442-8120712abaf9", |
| 44 | + "metadata": {}, |
| 45 | + "source": [ |
| 46 | + "#### A questo punto definisco delle UDF come quelle già create nella serve app.\n", |
| 47 | + "#### A differenza di quelle lato server, queste del client servono per inviare richieste al server, mentre quelle definite nello script server app servono per gestire le richieste ricevute." |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": 3, |
| 53 | + "id": "cd317608-0df7-4cdd-9097-7e4cc70c2a86", |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "def create_contact(nome, cognome, email, telefono):\n", |
| 58 | + " data = {\"Nome\": nome, \"Cognome\": cognome, \"Email\": email, \"Telefono\": telefono}\n", |
| 59 | + " response = requests.post(f\"{BASE_URL}/contacts\", json=data)\n", |
| 60 | + " print(response.json())\n", |
| 61 | + " \n", |
| 62 | + "def get_contact(contact_id):\n", |
| 63 | + " response = requests.get(f\"{BASE_URL}/contacts/{contact_id}\")\n", |
| 64 | + " print(response.json())\n", |
| 65 | + "\n", |
| 66 | + "def update_contact(contact_id, nome=None, cognome=None, email=None, telefono=None):\n", |
| 67 | + " data = {\"Nome\": nome, \"Cognome\": cognome, \"Email\": email, \"Telefono\": telefono}\n", |
| 68 | + " response = requests.put(f\"{BASE_URL}/contacts/{contact_id}\", json=data)\n", |
| 69 | + " print(response.json())\n", |
| 70 | + "\n", |
| 71 | + "def delete_contact(contact_id):\n", |
| 72 | + " response = requests.delete(f\"{BASE_URL}/contacts/{contact_id}\")\n", |
| 73 | + " print(response.json())\n", |
| 74 | + " \n", |
| 75 | + "def get_contacts():\n", |
| 76 | + " response = requests.get(f\"{BASE_URL}/contacts\")\n", |
| 77 | + " print(response.json())" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "markdown", |
| 82 | + "id": "bedb868d-af41-4391-9dd7-8393c015b77b", |
| 83 | + "metadata": {}, |
| 84 | + "source": [ |
| 85 | + "#### Eseguo ora alcuni test per verificare se il server funziona, il client riesce a fare correttamente operazioni CRUD\n", |
| 86 | + "#### Creo un nuovo contatto specificando Nome, Cognome, Mail e Numero di cellulare:" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 4, |
| 92 | + "id": "96e9154d-87f7-44cb-bc3d-8adcb733693f", |
| 93 | + "metadata": {}, |
| 94 | + "outputs": [ |
| 95 | + { |
| 96 | + "name": "stdout", |
| 97 | + "output_type": "stream", |
| 98 | + "text": [ |
| 99 | + "{'id': 1}\n" |
| 100 | + ] |
| 101 | + } |
| 102 | + ], |
| 103 | + "source": [ |
| 104 | + "create_contact(\"Alessandra\", \"Manolas\", \"[email protected]\", \"393313378245\")" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "markdown", |
| 109 | + "id": "0bde6e8b-02a1-4be5-9660-00ca2f12cff8", |
| 110 | + "metadata": {}, |
| 111 | + "source": [ |
| 112 | + "#### Provo ora invece a modificare un contatto già esistente in base ala user id, indicando successivamente i nuovi dettagli di contatto:" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": 6, |
| 118 | + "id": "5636999e-66b3-4514-972f-735280de3b7b", |
| 119 | + "metadata": {}, |
| 120 | + "outputs": [ |
| 121 | + { |
| 122 | + "name": "stdout", |
| 123 | + "output_type": "stream", |
| 124 | + "text": [ |
| 125 | + "{'Cognome': 'Manolo', 'Email': '[email protected]', 'Nome': 'Alexandro', 'Telefono': '393313478242'}\n" |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + "source": [ |
| 130 | + "update_contact(1, \"Alexandro\", \"Manolo\", \"[email protected]\", \"393313478242\")" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "id": "5f9d89de-cb79-47d1-a8d3-43f080b655d0", |
| 136 | + "metadata": {}, |
| 137 | + "source": [ |
| 138 | + "#### Provo ora invece ad eliminare un contatto indicando la user id:" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 8, |
| 144 | + "id": "99a9940b-95cc-4462-8562-e1f891ef57fb", |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [ |
| 147 | + { |
| 148 | + "name": "stdout", |
| 149 | + "output_type": "stream", |
| 150 | + "text": [ |
| 151 | + "{'message': 'Contatto eliminato'}\n" |
| 152 | + ] |
| 153 | + } |
| 154 | + ], |
| 155 | + "source": [ |
| 156 | + "delete_contact(1)" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "markdown", |
| 161 | + "id": "92bd388c-69a9-4f2a-9798-c96a61dc1e5e", |
| 162 | + "metadata": {}, |
| 163 | + "source": [ |
| 164 | + "### Infine, provo a eseguire contemporaneamente due operazioni di CRUD, una per creare un contatto nuovo ed una per vedere tutti i contatti presenti in questo momento nel server:" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "code", |
| 169 | + "execution_count": 9, |
| 170 | + "id": "0fdb76fc-fb56-4894-a642-0102435d738d", |
| 171 | + "metadata": {}, |
| 172 | + "outputs": [ |
| 173 | + { |
| 174 | + "name": "stdout", |
| 175 | + "output_type": "stream", |
| 176 | + "text": [ |
| 177 | + "{'id': 3}\n", |
| 178 | + "{'2': {'Cognome': 'Rossi', 'Email': '[email protected]', 'Nome': 'Mario', 'Telefono': '1234567890'}, '3': {'Cognome': 'Rossi', 'Email': '[email protected]', 'Nome': 'Mario', 'Telefono': '1234567890'}}\n" |
| 179 | + ] |
| 180 | + } |
| 181 | + ], |
| 182 | + "source": [ |
| 183 | + "# Esempio di utilizzo\n", |
| 184 | + "if __name__ == \"__main__\":\n", |
| 185 | + " create_contact(\"Mario\", \"Rossi\", \"[email protected]\", \"1234567890\")\n", |
| 186 | + " get_contacts()" |
| 187 | + ] |
| 188 | + } |
| 189 | + ], |
| 190 | + "metadata": { |
| 191 | + "kernelspec": { |
| 192 | + "display_name": "Python 3 (ipykernel)", |
| 193 | + "language": "python", |
| 194 | + "name": "python3" |
| 195 | + }, |
| 196 | + "language_info": { |
| 197 | + "codemirror_mode": { |
| 198 | + "name": "ipython", |
| 199 | + "version": 3 |
| 200 | + }, |
| 201 | + "file_extension": ".py", |
| 202 | + "mimetype": "text/x-python", |
| 203 | + "name": "python", |
| 204 | + "nbconvert_exporter": "python", |
| 205 | + "pygments_lexer": "ipython3", |
| 206 | + "version": "3.11.7" |
| 207 | + } |
| 208 | + }, |
| 209 | + "nbformat": 4, |
| 210 | + "nbformat_minor": 5 |
| 211 | +} |
0 commit comments