-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgqlc_research.py
150 lines (134 loc) · 4.45 KB
/
sgqlc_research.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import ssl
import urllib.request as UR
import yaml
from sgqlc.endpoint.http import HTTPEndpoint
from sgqlc.operation import Operation
from utils.env import Env
from utils.switch import Switch
from schema.platform_schema import Mutation, Query
# # 这是简单的发GraphQL请求示例,不依赖自动生成的schema
# def login_simple():
# url = "https://test2.teletraan.io/graphql"
# headers = {'accept': 'application/json', 'Content-Type': 'application/json'}
# query = 'mutation login($input: LoginInput!) {login(input: $input) {token}}'
# variables = '''
# {
# "input": {
# "account": "admin",
# "password": "teletraan@2022"
# }
# }
# '''
# endpoint = HTTPEndpoint(url, headers)
# data = endpoint(query, variables)
# print(data, type(data))
# 走代理时,全局取消证书验证,避免报错
class Request:
get_env = Env()
url = get_env.get_env()
account = get_env.get_account()
password = get_env.get_pwd()
def __init__(self, proxy_=None):
switch = Switch()
is_switch_on = switch.is_proxy_on()
if is_switch_on is True:
ssl._create_default_https_context = ssl._create_unverified_context
proxy_ = "127.0.0.1:8080"
else:
pass
if proxy_:
proxy = {
"http": 'http://' + proxy_,
"https": 'http://' + proxy_
}
else:
proxy = None
proxy_support = UR.ProxyHandler(proxy)
# build a new opener that adds authentication and caching FTP handlers
opener = UR.build_opener(proxy_support, UR.CacheFTPHandler)
# install it
UR.install_opener(opener)
# 这是sgqlc提供的更优雅的请求方式
def login_right(self):
headers = {'accept': 'application/json', 'Content-Type': 'application/json'}
endpoint = HTTPEndpoint(url=self.url, base_headers=headers, timeout=3)
variables = {
"account": self.account,
"password": self.password
}
op = Operation(Mutation)
login = op.login(input=variables)
# # 指定返回token,不要userid
# # login.token()
data = endpoint(op)
# res = (op + data).login
'''
res = AuthInfo(token=34CkAS3qWpt5xw0O7QX5teMjKUwUnoTh)
如果res = op + data
res = Mutation(⬆️res)
'''
return data
def query_project(self, order_by=["-created_at"], **kwargs):
token = 'Token ' + self.login_right()
headers = {'accept': 'application/json', 'Content-Type': 'application/json', 'authorization': token}
endpoint = HTTPEndpoint(url=self.url, base_headers=headers, timeout=3)
op = Operation(Query)
pro = op.product_project_list(limit=10, filter=eval(f"{kwargs}"), order_by=order_by)
pro.data.attachment()
data = endpoint(op)
res = (op + data).product_project_list.data
print(res)
def json_to_yaml(self):
j = {
"id": 292,
"operateType": "WITHDRAW",
"opinion": ""
}
with open("case_data/temp_v.yaml", "w") as f:
yaml.safe_dump(data=j, stream=f, allow_unicode=True)
def yaml_to_json(self):
y = yaml.safe_load(open("case_data/variables_teamsit2.yaml"))
res = y["project"]["add_product_task"]
print(res)
def change(self):
j = {
"project": {
"id": 178
},
"task": [
{
"executive": {
"id": "b8e2c7cc-6198-48ab-9faa-e2d9cb410f4b"
},
"name": "task_name_a_QbcF1h",
"participant": [
{
"id": "b8e2c7cc-6198-48ab-9faa-e2d9cb410f4b"
}
],
"planEndAt": 1644163200000,
"planStartAt": 1644163200000
}
]
}
j["task"][0]["name"] = "啊?"
print(j)
def yy(self):
for i in [1, 3]:
res = []
if i == 3:
for j in range(3):
res.append(j)
j += 1
break
elif i != 3:
continue
return res
if __name__ == "__main__":
r = Request()
# print(r.login_right())
# r.yaml_to_json()
# r.query_project()
# c = r.yy()
# print(c)
r.json_to_yaml()