Skip to content

Instantly share code, notes, and snippets.

@nook24
Created April 23, 2021 08:56
Show Gist options
  • Save nook24/e5138f3e6be19a440443e3e525e906c2 to your computer and use it in GitHub Desktop.
Save nook24/e5138f3e6be19a440443e3e525e906c2 to your computer and use it in GitHub Desktop.
# !/usr/bin/python
import json
import traceback
import requests
import urllib3
# Tested with Python 3
# Run like: python3 umlauts.py
#
# pip3 install requests urllib3
#
class HostUmlauts():
def run(self):
####### CHANGE CONFIG #######
self.apikey = "d2083e61fee067077ac239b7d89084f31c16a66f5e2908f6f74009492b600d3d90db2c5f6175ff199c31e1adbdcf6cf089ecadaf1c93c776bc19c7f41feb42b58f2637883b1a1273035f0ade592415f5"
self.oitc_url = "https://demo.openitcockpit.io"
self.host_id = "1"
#############################
####### DO NOT MODIFY BELOW THIS LINE #######
self.host_id = str(self.host_id)
host = self.load_host()
host["Host"]["name"] = "%s and some umlauts: üäö" % host["Host"]["name"]
host["Host"]["description"] = "%s and some umlauts: üäö" % host["Host"]["description"]
self.update_host(host)
def load_host(self) -> dict:
source = self.send_get_request(
'/hosts/edit/' + self.host_id + '.json?angular=true')
if "host" not in source:
print("Error while loading host")
exit(1)
return source["host"]
def update_host(self, host: dict):
result = self.send_post_request(
url="/hosts/edit/" + self.host_id + ".json?angular=true",
data=host
)
if "id" not in result:
print("Error while updating host")
exit(1)
def send_get_request(self, url):
try:
urllib3.disable_warnings()
except:
print(traceback.format_exc())
url = self.oitc_url + url
headers = {
'Authorization': 'X-OITC-API ' + self.apikey
}
try:
response = requests.get(
url,
headers=headers,
verify=False,
)
# response_body = response.content.decode('utf-8')
# print(response_body)
if response.content.decode('utf-8').strip() != '':
return json.loads(response.content.decode('utf-8'))
except Exception as e:
print(str(e))
print(traceback.format_exc())
def send_post_request(self, url, data):
try:
urllib3.disable_warnings()
except:
print(traceback.format_exc())
url = self.oitc_url + url
headers = {
'Content-Type': 'application/json',
'Authorization': 'X-OITC-API ' + self.apikey
}
try:
response = requests.post(
url,
json=data,
headers=headers,
verify=False,
)
# response_body = response.content.decode('utf-8')
# print(response_body)
if response.status_code != 200:
print("Error whiel saving data :(")
print(response.content.decode('utf-8'))
if response.content.decode('utf-8').strip() != '':
return json.loads(response.content.decode('utf-8'))
except Exception as e:
print(str(e))
print(traceback.format_exc())
if __name__ == '__main__':
HostUmlauts().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment