From 2416c47f679f7a5e2c2e36ecc1689de4dd097939 Mon Sep 17 00:00:00 2001 From: 0llino Date: Sun, 8 Jan 2023 20:00:24 +0100 Subject: [PATCH] added the client.py and several functionalities. Not finished yet tho. --- src/client.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 src/client.py diff --git a/src/client.py b/src/client.py new file mode 100755 index 0000000..dded8aa --- /dev/null +++ b/src/client.py @@ -0,0 +1,71 @@ +from fileinput import filename +from operator import delitem +from typing import final +import requests +import sys +import json +from optparse import OptionParser +from datetime import datetime +import datetime +import hashlib +import base64 + + +def send_request(apiurl, headers, method="GET"): + """ Sends request & catch data """ + match method: + case "POST": + req = requests.post(apiurl, headers=headers) + case "GET": + req = requests.get(apiurl, headers=headers) + + data = req.json() + + for element in data: + try: + print("") + print("TITRE : " + element['titre']) + print("ID MIRABEL : " + str(element['mirabelid'])) + print("URL : " + element['url']) + except TypeError: + continue + +if __name__ == "__main__": + """ Main function, printing helper & getting arguments """ + headers = {'Accept': 'application/json'} + url = "http://localhost:8080" + + + parser = OptionParser(add_help_option = False) + parser.add_option ('-h', '--help', action = 'help', + help = "The purpose of the script is being the client of an API server, named hephaistos. Please provide an argument.") + parser.add_option("-t", "--titre", dest="givenTitle", default=None, + help="Use this argument to query the article you want", metavar="") + parser.add_option("-f", "--favoris", dest="givenFavouriteNumber", default=None, + help="Use this argument to add/register an article into your favorite list", metavar="
") + parser.add_option("-l", "--liste", dest="list", default=False, action="store_true", + help="Use this argument to list all your registered favourites") + parser.add_option("-H", "--historique", dest="histo", default=False, action="store_true", + help="Use this argument to list all your previous requests") + +(options, args) = parser.parse_args() + +if ( options.givenTitle is not None): + apiurl = url + "/api/titre/" + options.givenTitle + send_request(apiurl, headers) + +elif ( options.givenFavouriteNumber is not None ): + apiurl = url + "/api/favoris/" + options.givenFavouriteNumber + send_request(apiurl, headers, method="POST") + +elif ( options.list is not False ): + apiurl = url + "/api/favoris" + send_request(apiurl, headers) + +elif (options.histo is not False): + apiurl = url + "/api/historique" + send_request(apiurl, headers) + + +if len(sys.argv[1:]) == 0: + parser.print_help()