api.py 10 KB

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