screen.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #-*- coding:utf-8 -*-
  2. import json
  3. from rrd.config import API_ADDR
  4. from rrd import corelib
  5. class DashboardScreen(object):
  6. def __init__(self, id, pid, name):
  7. self.id = str(id)
  8. self.pid = str(pid)
  9. self.name = name
  10. def __repr__(self):
  11. return "<DashboardScreen id=%s, name=%s, pid=%s>" %(self.id, self.name, self.pid)
  12. __str__ = __repr__
  13. @classmethod
  14. def get(cls, id):
  15. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/screen/%s" %(id,))
  16. if r.status_code != 200:
  17. raise Exception(r.text)
  18. j = r.json()
  19. if j:
  20. row = [j["id"], j["pid"], j["name"]]
  21. return cls(*row)
  22. @classmethod
  23. def gets_by_pid(cls, pid):
  24. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/screens/pid/%s" %(pid,))
  25. if r.status_code != 200:
  26. raise Exception(r.text)
  27. j = r.json() or []
  28. return [cls(*[x["id"], x["pid"], x["name"]]) for x in j]
  29. @classmethod
  30. def gets_all(cls, limit=500):
  31. r = corelib.auth_requests("GET", API_ADDR + "/dashboard/screens?limit=%s" %(limit,))
  32. if r.status_code != 200:
  33. raise Exception(r.text)
  34. j = r.json() or []
  35. return [cls(*[x["id"], x["pid"], x["name"]]) for x in j]
  36. @classmethod
  37. def add(cls, pid, name):
  38. d = {"pid": pid, "name": name}
  39. r = corelib.auth_requests("POST", API_ADDR + "/dashboard/screen", data = d)
  40. if r.status_code != 200:
  41. raise Exception(r.text)
  42. j = r.json()
  43. return cls(*[j["id"], j["pid"], j["name"]])
  44. @classmethod
  45. def remove(cls, id):
  46. h = {"Content-type": "application/json"}
  47. r = corelib.auth_requests("DELETE", API_ADDR + "/dashboard/screen/%s" %(id,), headers=h)
  48. if r.status_code != 200:
  49. raise Exception(r.text)
  50. return r.json()
  51. def update(self, pid=None, name=None):
  52. d = {}
  53. if pid:
  54. d["pid"] = pid
  55. if name:
  56. d["name"] = name
  57. r = corelib.auth_requests("PUT", API_ADDR + "/dashboard/screen/%s" %self.id, data = d)
  58. if r.status_code != 200:
  59. raise Exception(r.text)
  60. return r.json()