118 lines
3.6 KiB
Python
118 lines
3.6 KiB
Python
import json
|
||
import requests
|
||
import urllib3
|
||
import time
|
||
from requests.structures import CaseInsensitiveDict
|
||
import base64
|
||
import pandas as pd
|
||
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QVBoxLayout, QPushButton, QMessageBox
|
||
|
||
request_url="http://140.125.21.70:7071"
|
||
|
||
#筆記:
|
||
#Parking_spaces_API是通訊底層,headers是令牌,fun是funtion名稱,在使用api時,Parking_spaces_API(fun,headers)
|
||
#response函式庫使用,request.get(總URL,data=要放的data)
|
||
#http://localhost:7700=>request_url 這個URL已經是固定,所以在底層時就會先寫死
|
||
#/api/Model名稱//{id}=>fun
|
||
|
||
#通訊底層
|
||
class WMS_API():
|
||
def __init__(self, Fun, headers, *args):
|
||
self.headers = headers
|
||
self.Fun = Fun
|
||
|
||
def post(self, data):
|
||
data_json = json.dumps(data)
|
||
response = requests.post(request_url + self.Fun, data=data_json, headers=self.headers, verify=False)
|
||
return response
|
||
|
||
def get(self):
|
||
response = requests.get(request_url + self.Fun, headers=self.headers, verify=False)
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
df = pd.DataFrame.from_records(data)
|
||
return df
|
||
else:
|
||
return
|
||
|
||
def delete(self):
|
||
response = requests.delete(request_url + self.Fun, headers=self.headers, verify=False)
|
||
return response
|
||
|
||
def getSingleData(self):
|
||
response = requests.get(request_url + self.Fun, headers=self.headers, verify=False)
|
||
if response.status_code == 200:
|
||
data = response.json()
|
||
df = pd.DataFrame.from_records(data, index=[0])
|
||
return df
|
||
else:
|
||
return
|
||
|
||
#電料類
|
||
class Vip_api():
|
||
def __init__(self,):
|
||
self.token=None
|
||
pass
|
||
def verify(self,username,password):
|
||
API = "/Users/Verifypassword"
|
||
data = {
|
||
'username': username,
|
||
'password': password
|
||
}
|
||
headers = {'Content-type': 'application/json'}
|
||
response=WMS_API(API,headers).post(data)
|
||
if response.status_code == 200:
|
||
self.token = response.json().get('token')
|
||
return True
|
||
else:
|
||
return False
|
||
def get_token(self):
|
||
return self.token
|
||
def get_all_data(self,model_name):
|
||
API = "/api/"+model_name
|
||
headers = CaseInsensitiveDict()
|
||
headers['Accept'] = 'application/json'
|
||
data = WMS_API(API, headers).get()
|
||
return data
|
||
|
||
def createitem(self,model_name,item,account,location,itemimage):
|
||
self.token=self.get_token()
|
||
print(self.token)
|
||
API= "/api/"+model_name+"/"+item
|
||
headers = {
|
||
'Content-type': 'application/json',
|
||
"Authorization": self.token
|
||
}
|
||
print(headers)
|
||
data = {
|
||
"item": item,
|
||
"account": account,
|
||
"location": location,
|
||
"itemimage": itemimage
|
||
}
|
||
|
||
data=WMS_API(API,headers).post(data)
|
||
print(data.status_code)
|
||
|
||
#登入系統JWT
|
||
# class User():
|
||
# def __init__(self,):
|
||
#
|
||
# def verify(self,username,password):
|
||
# API = "/Users/Verifypassword"
|
||
# data = {
|
||
# 'username': username,
|
||
# 'password': password
|
||
# }
|
||
# headers = {'Content-type': 'application/json'}
|
||
# response=WMS_API(API,headers).post(data)
|
||
# if response.status_code == 200:
|
||
# self.token = response.json().get('token')
|
||
# print("Login successful. Token saved.")
|
||
# return True
|
||
# else:
|
||
# print("Login failed.")
|
||
# return False
|
||
# def get_token(self):
|
||
# return self.token
|