api.py 9.5 KB

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