api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #! /usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # vi:ts=4:et
  4. # Wekan API Python CLI, originally from here, where is more details:
  5. # https://github.com/wekan/wekan/wiki/New-card-with-Python3-and-REST-API
  6. try:
  7. # python 3
  8. from urllib.parse import urlencode
  9. except ImportError:
  10. # python 2
  11. from urllib import urlencode
  12. import json
  13. import requests
  14. import sys
  15. arguments = len(sys.argv) - 1
  16. if arguments == 0:
  17. print("=== Wekan API Python CLI: Shows IDs for addcard ===")
  18. print("AUTHORID is USERID that writes card.")
  19. print("If *nix: chmod +x api.py => ./api.py users")
  20. print("Syntax:")
  21. print(" python3 api.py users # All users")
  22. print(" python3 api.py boards # All Public Boards")
  23. print(" python3 api.py boards USERID # Boards of USERID")
  24. print(" python3 api.py board BOARDID # Info of BOARDID")
  25. print(" python3 api.py customfields BOARDID # Custom Fields of BOARDID")
  26. print(" python3 api.py customfield BOARDID CUSTOMFIELDID # Info of CUSTOMFIELDID")
  27. print(" python3 api.py swimlanes BOARDID # Swimlanes of BOARDID")
  28. print(" python3 api.py lists BOARDID # Lists of BOARDID")
  29. print(" python3 api.py list BOARDID LISTID # Info of LISTID")
  30. print(" python3 api.py createlist BOARDID LISTTITLE # Create list")
  31. print(" python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION")
  32. print(" python3 api.py editcard BOARDID LISTID CARDID NEWCARDTITLE NEWCARDDESCRIPTION")
  33. print(" python3 api.py listattachments BOARDID # List attachments")
  34. # TODO:
  35. # print(" python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64")
  36. # print(" python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file")
  37. # print(" python3 api.py attachmentdownload BOARDID ATTACHMENTID # One attachment as file")
  38. # print(" python3 api.py attachmentsdownload BOARDID # All attachments as files")
  39. exit
  40. # ------- SETTINGS START -------------
  41. # Username is your Wekan username or email address.
  42. # OIDC/OAuth2 etc uses email address as username.
  43. username = 'testtest'
  44. password = 'testtest'
  45. wekanurl = 'http://localhost:4000/'
  46. # ------- SETTINGS END -------------
  47. """
  48. EXAMPLE:
  49. python3 api.py
  50. OR:
  51. chmod +x api.py
  52. ./api.py
  53. === Wekan API Python CLI: Shows IDs for addcard ===
  54. AUTHORID is USERID that writes card.
  55. Syntax:
  56. python3 api.py users # All users
  57. python3 api.py boards USERID # Boards of USERID
  58. python3 api.py board BOARDID # Info of BOARDID
  59. python3 api.py customfields BOARDID # Custom Fields of BOARDID
  60. python3 api.py customfield BOARDID CUSTOMFIELDID # Info of CUSTOMFIELDID
  61. python3 api.py swimlanes BOARDID # Swimlanes of BOARDID
  62. python3 api.py lists BOARDID # Lists of BOARDID
  63. python3 api.py list BOARDID LISTID # Info of LISTID
  64. python3 api.py createlist BOARDID LISTTITLE # Create list
  65. python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  66. python3 api.py editcard BOARDID LISTID CARDID NEWCARDTITLE NEWCARDDESCRIPTION
  67. python3 api.py listattachments BOARDID # List attachments
  68. python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64
  69. python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file
  70. === USERS ===
  71. python3 api.py users
  72. => abcd1234
  73. === BOARDS ===
  74. python3 api.py boards abcd1234
  75. === SWIMLANES ===
  76. python3 api.py swimlanes dYZ
  77. [{"_id":"Jiv","title":"Default"}
  78. ]
  79. === LISTS ===
  80. python3 api.py lists dYZ
  81. []
  82. There is no lists, so create a list:
  83. === CREATE LIST ===
  84. python3 api.py createlist dYZ 'Test'
  85. {"_id":"7Kp"}
  86. # python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  87. python3 api.py addcard ppg dYZ Jiv 7Kp 'Test card' 'Test description'
  88. === LIST ATTACHMENTS WITH DOWNLOAD URLs ====
  89. python3 api.py listattachments BOARDID
  90. """
  91. # ------- API URL GENERATION START -----------
  92. loginurl = 'users/login'
  93. wekanloginurl = wekanurl + loginurl
  94. apiboards = 'api/boards/'
  95. apiattachments = 'api/attachments/'
  96. apiusers = 'api/users'
  97. e = 'export'
  98. s = '/'
  99. l = 'lists'
  100. sw = 'swimlane'
  101. sws = 'swimlanes'
  102. cs = 'cards'
  103. cf = 'custom-fields'
  104. bs = 'boards'
  105. atl = 'attachmentslist'
  106. at = 'attachment'
  107. ats = 'attachments'
  108. users = wekanurl + apiusers
  109. # ------- API URL GENERATION END -----------
  110. # ------- LOGIN TOKEN START -----------
  111. data = {"username": username, "password": password}
  112. body = requests.post(wekanloginurl, data=data)
  113. d = body.json()
  114. apikey = d['token']
  115. # ------- LOGIN TOKEN END -----------
  116. if arguments == 7:
  117. if sys.argv[1] == 'addcard':
  118. # ------- WRITE TO CARD START -----------
  119. authorid = sys.argv[2]
  120. boardid = sys.argv[3]
  121. swimlaneid = sys.argv[4]
  122. listid = sys.argv[5]
  123. cardtitle = sys.argv[6]
  124. carddescription = sys.argv[7]
  125. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  126. # Write to card
  127. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  128. post_data = {'authorId': '{}'.format(authorid), 'title': '{}'.format(cardtitle), 'description': '{}'.format(carddescription), 'swimlaneId': '{}'.format(swimlaneid)}
  129. body = requests.post(cardtolist, data=post_data, headers=headers)
  130. print(body.text)
  131. # ------- WRITE TO CARD END -----------
  132. if arguments == 6:
  133. if sys.argv[1] == 'editcard':
  134. # ------- LIST OF BOARD START -----------
  135. boardid = sys.argv[2]
  136. listid = sys.argv[3]
  137. cardid = sys.argv[4]
  138. newcardtitle = sys.argv[5]
  139. newcarddescription = sys.argv[6]
  140. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  141. print(edcard)
  142. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  143. put_data = {'title': '{}'.format(newcardtitle), 'description': '{}'.format(newcarddescription)}
  144. body = requests.put(edcard, data=put_data, headers=headers)
  145. print("=== EDIT CARD ===\n")
  146. body = requests.get(edcard, headers=headers)
  147. data2 = body.text.replace('}',"}\n")
  148. print(data2)
  149. # ------- LISTS OF BOARD END -----------
  150. if arguments == 3:
  151. if sys.argv[1] == 'createlist':
  152. # ------- CREATE LIST START -----------
  153. boardid = sys.argv[2]
  154. listtitle = sys.argv[3]
  155. list = wekanurl + apiboards + boardid + s + l
  156. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  157. post_data = {'title': '{}'.format(listtitle)}
  158. body = requests.post(list, data=post_data, headers=headers)
  159. print("=== CREATE LIST ===\n")
  160. print(body.text)
  161. # ------- CREATE LIST END -----------
  162. if sys.argv[1] == 'list':
  163. # ------- LIST OF BOARD START -----------
  164. boardid = sys.argv[2]
  165. listid = sys.argv[3]
  166. listone = wekanurl + apiboards + boardid + s + l + s + listid
  167. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  168. print("=== INFO OF ONE LIST ===\n")
  169. body = requests.get(listone, headers=headers)
  170. data2 = body.text.replace('}',"}\n")
  171. print(data2)
  172. # ------- LISTS OF BOARD END -----------
  173. if sys.argv[1] == 'customfield':
  174. # ------- LIST OF BOARD START -----------
  175. boardid = sys.argv[2]
  176. customfieldid = sys.argv[3]
  177. customfieldone = wekanurl + apiboards + boardid + s + cf + s + customfieldid
  178. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  179. print("=== INFO OF ONE CUSTOM FIELD ===\n")
  180. body = requests.get(customfieldone, headers=headers)
  181. data2 = body.text.replace('}',"}\n")
  182. print(data2)
  183. # ------- LISTS OF BOARD END -----------
  184. if arguments == 2:
  185. # ------- BOARDS LIST START -----------
  186. userid = sys.argv[2]
  187. boards = users + s + userid + s + bs
  188. if sys.argv[1] == 'boards':
  189. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  190. #post_data = {'userId': '{}'.format(userid)}
  191. body = requests.get(boards, headers=headers)
  192. print("=== BOARDS ===\n")
  193. data2 = body.text.replace('}',"}\n")
  194. print(data2)
  195. # ------- BOARDS LIST END -----------
  196. if sys.argv[1] == 'board':
  197. # ------- BOARD INFO START -----------
  198. boardid = sys.argv[2]
  199. board = wekanurl + apiboards + boardid
  200. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  201. body = requests.get(board, headers=headers)
  202. print("=== BOARD ===\n")
  203. data2 = body.text.replace('}',"}\n")
  204. print(data2)
  205. # ------- BOARD INFO END -----------
  206. if sys.argv[1] == 'customfields':
  207. # ------- CUSTOM FIELDS INFO START -----------
  208. boardid = sys.argv[2]
  209. boardcustomfields = wekanurl + apiboards + boardid + s + cf
  210. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  211. body = requests.get(boardcustomfields, headers=headers)
  212. print("=== CUSTOM FIELDS OF BOARD ===\n")
  213. data2 = body.text.replace('}',"}\n")
  214. print(data2)
  215. # ------- CUSTOM FIELDS INFO END -----------
  216. if sys.argv[1] == 'swimlanes':
  217. boardid = sys.argv[2]
  218. swimlanes = wekanurl + apiboards + boardid + s + sws
  219. # ------- SWIMLANES OF BOARD START -----------
  220. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  221. print("=== SWIMLANES ===\n")
  222. body = requests.get(swimlanes, headers=headers)
  223. data2 = body.text.replace('}',"}\n")
  224. print(data2)
  225. # ------- SWIMLANES OF BOARD END -----------
  226. if sys.argv[1] == 'lists':
  227. # ------- LISTS OF BOARD START -----------
  228. boardid = sys.argv[2]
  229. lists = wekanurl + apiboards + boardid + s + l
  230. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  231. print("=== LISTS ===\n")
  232. body = requests.get(lists, headers=headers)
  233. data2 = body.text.replace('}',"}\n")
  234. print(data2)
  235. # ------- LISTS OF BOARD END -----------
  236. if sys.argv[1] == 'listattachments':
  237. # ------- LISTS OF ATTACHMENTS START -----------
  238. boardid = sys.argv[2]
  239. listattachments = wekanurl + apiboards + boardid + s + ats
  240. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  241. print("=== LIST OF ATTACHMENTS ===\n")
  242. body = requests.get(listattachments, headers=headers)
  243. data2 = body.text.replace('}',"}\n")
  244. print(data2)
  245. # ------- LISTS OF ATTACHMENTS END -----------
  246. if arguments == 1:
  247. if sys.argv[1] == 'users':
  248. # ------- LIST OF USERS START -----------
  249. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  250. print(users)
  251. print("=== USERS ===\n")
  252. body = requests.get(users, headers=headers)
  253. data2 = body.text.replace('}',"}\n")
  254. print(data2)
  255. # ------- LIST OF USERS END -----------
  256. if sys.argv[1] == 'boards':
  257. # ------- LIST OF PUBLIC BOARDS START -----------
  258. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  259. print("=== PUBLIC BOARDS ===\n")
  260. listpublicboards = wekanurl + apiboards
  261. body = requests.get(listpublicboards, headers=headers)
  262. data2 = body.text.replace('}',"}\n")
  263. print(data2)
  264. # ------- LIST OF PUBLIC BOARDS END -----------