api.py 13 KB

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