-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_gui_test
91 lines (74 loc) · 2.9 KB
/
api_gui_test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
import requests
from getpass import getpass
import tkinter as tk
from tkinter import messagebox
from functools import partial
from requests import auth
# Window
tkWindow = tk.Tk()
tkWindow.geometry('600x150')
tkWindow.title('XRD Talaria')
# NOMAD API base URL
base_url = "http://intra8.pdi-berlin.de/nomad-oasis/api/v1"
login_url = f"{base_url}/auth/token"
# Step 1: User Authentication
def authenticate(username_entry, password_entry):
userid = username_entry.get()
password = password_entry.get()
response = requests.get(
login_url, params=dict(username=userid, password=password))
print(userid, password)
if response.status_code == 200:
messagebox.showinfo("Authentication successful!")
return response.json()['access_token']
else:
messagebox.showerror(f"Authentication failed: {response.status_code} - {response.text}")
return None
# Step 2: File Upload
def upload_file(token):
# Prompt for the file path
file_path = input("Enter the path of the file you want to upload: ")
try:
# Open the file in binary mode
with open(file_path, 'rb') as file:
# Set the upload URL and headers
upload_url = f"{base_url}/uploads"
headers = {
"Authorization": f"Bearer {token}"
}
files = {
"file": file
}
# Send the POST request to upload the file
response = requests.post(upload_url, headers=headers, files=files)
if response.status_code == 200:
print("File uploaded successfully!")
print(f"Response: {response.status_code} - {response.text}")
else:
print(f"File upload failed: {response.status_code} - {response.text}")
except FileNotFoundError:
print("File not found. Please check the path and try again.")
#class buttonpress(function, *args):
# global value
# def value(): return function(*args)
# Username label and text entry box
usernameLabel = tk.Label(tkWindow, text="User Name").grid(row=0, column=0)
username_entry = tk.Entry(tkWindow)
username_entry.grid(row=0, column=1)
# Password label and password entry box
passwordLabel = tk.Label(tkWindow,text="Password").grid(row=1, column=0)
password_entry = tk.Entry(tkWindow, show='*')
password_entry.grid(row=1, column=1)
# Authenticate user
authenticate_with_arg = partial(authenticate, username_entry, password_entry)
# Login button
#login_button = tk.Button(tkWindow, text="Login", command= lambda: buttonpress(authenticate, username_entry, password_entry))
login_button = tk.Button(tkWindow, text="Login", command= authenticate_with_arg)
login_button.grid(row=4, column=0)
exit_button = tk.Button(tkWindow, text="Exit", command=tkWindow.destroy)
exit_button.grid(row=4, column=1)
if login_button==None:
# Upload file
upload_file(login_button)
tkWindow.mainloop()