-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuipath.py
118 lines (102 loc) · 3.76 KB
/
uipath.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
# -*- coding: utf-8 -*-
import datetime
import gettext
import json
import os
import requests
from distutils.util import strtobool
from requests_ntlm import HttpNtlmAuth
def start_jobs(process_name):
o = orchestrator()
if o.account_authenticate():
filters = "Name eq '{}'".format(process_name)
odata = o.get_odata("Releases", filters)
if odata:
release_key = odata["value"][0]["Key"]
job_id = o.start_jobs(release_key)
if job_id:
message = _("{} started. Job ID is {}").format(
process_name, o.job_id)
return message
message = _("{} can not started. message is {}").format(
process_name, o.message)
return message
def get_odata(name, filters):
o = orchestrator()
if o.account_authenticate():
odata = o.get_odata(name, filters)
if odata:
return odata["value"], None
return None, o.message
class orchestrator:
def __init__(self):
self.url = os.environ["orchestrator_url"]
self.tenancy_name = os.environ["orchestrator_tenancy_name"]
self.username = os.environ["orchestrator_username"]
self.password = os.environ["orchestrator_password"]
self.api_key = os.environ["orchestrator_api_key"]
self.ntlm_authentication = strtobool(
os.environ["orchestrator_ntlm_authentication"])
self.message = None
self.session = requests.Session()
self.session.headers.update({
"Content-Type": "application/json",
"Accept": "application/json"
})
def account_authenticate(self):
if self.ntlm_authentication:
self.session.auth = HttpNtlmAuth(self.username, self.password)
return True
params = {"api_key": self.api_key}
response = self.session.post(
self.url + "/api/Account/Authenticate",
json.dumps({
"tenancyName": self.tenancy_name,
"usernameOrEmailAddress": self.username,
"password": self.password
}),
params=params)
if response.status_code == 200:
self.session.headers["Authorization"] = "Bearer {}".format(
response.json()["result"])
return True
self.message = _("{} failed").format("/api/Account/Authenticate")
return False
def get_odata(self, name, filters=None):
params = {}
if self.api_key:
params["api_key"] = self.api_key
if filters:
params["$filter"] = filters
response = self.session.get(self.url + "/odata/" + name, params=params)
if response.status_code == 200:
odata = response.json()
if len(odata["value"]):
return odata
self.message = _("{} not found").format(name)
else:
self.message = _("{} failed").format(name)
return None
def start_jobs(self, release_key):
params = {}
if self.api_key:
params["api_key"] = self.api_key
response = self.session.post(
self.url +
"/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs",
json.dumps({
"startInfo": {
"ReleaseKey": release_key
}
}),
params=params)
if response.status_code == 201:
if "value" in response.json():
self.job_id = str(response.json()["value"][0]["Id"])
return True
if "message" in response.json():
self.message = response.json()["message"]
else:
self.message = _("{} failed").format(
"/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs")
return False