added the client.py and several functionalities. Not finished yet tho.

This commit is contained in:
0llino 2023-01-08 20:00:24 +01:00
parent daa34863d5
commit 2416c47f67

71
src/client.py Executable file
View file

@ -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="<titre>")
parser.add_option("-f", "--favoris", dest="givenFavouriteNumber", default=None,
help="Use this argument to add/register an article into your favorite list", metavar="<article number>")
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()