api.py 11 KB

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