api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. # addcustomfieldtoboard: 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 = str(json.loads(sys.argv[6]))
  131. # There is error: Settings must be object. So this does not work yet.
  132. #settings = {'currencyCode': 'EUR'}
  133. print(type(settings))
  134. showoncard = sys.argv[7]
  135. automaticallyoncard = sys.argv[8]
  136. showlabelonminicard = sys.argv[9]
  137. showsumattopoflist = sys.argv[10]
  138. customfieldtoboard = wekanurl + apiboards + boardid + s + cf
  139. # Add Custom Field to Board
  140. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  141. 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)}
  142. body = requests.post(customfieldtoboard, data=post_data, headers=headers)
  143. print(body.text)
  144. # ------- ADD CUSTOM FIELD TO BOARD END -----------
  145. if arguments == 7:
  146. if sys.argv[1] == 'addcard':
  147. # ------- ADD CARD START -----------
  148. authorid = sys.argv[2]
  149. boardid = sys.argv[3]
  150. swimlaneid = sys.argv[4]
  151. listid = sys.argv[5]
  152. cardtitle = sys.argv[6]
  153. carddescription = sys.argv[7]
  154. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  155. # Add card
  156. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  157. post_data = {'authorId': '{}'.format(authorid), 'title': '{}'.format(cardtitle), 'description': '{}'.format(carddescription), 'swimlaneId': '{}'.format(swimlaneid)}
  158. body = requests.post(cardtolist, data=post_data, headers=headers)
  159. print(body.text)
  160. # ------- ADD CARD END -----------
  161. if arguments == 6:
  162. if sys.argv[1] == 'editcard':
  163. # ------- EDIT CARD START -----------
  164. boardid = sys.argv[2]
  165. listid = sys.argv[3]
  166. cardid = sys.argv[4]
  167. newcardtitle = sys.argv[5]
  168. newcarddescription = sys.argv[6]
  169. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  170. print(edcard)
  171. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  172. put_data = {'title': '{}'.format(newcardtitle), 'description': '{}'.format(newcarddescription)}
  173. body = requests.put(edcard, data=put_data, headers=headers)
  174. print("=== EDIT CARD ===\n")
  175. body = requests.get(edcard, headers=headers)
  176. data2 = body.text.replace('}',"}\n")
  177. print(data2)
  178. # ------- EDIT CARD END -----------
  179. if arguments == 3:
  180. if sys.argv[1] == 'createlist':
  181. # ------- CREATE LIST START -----------
  182. boardid = sys.argv[2]
  183. listtitle = sys.argv[3]
  184. list = wekanurl + apiboards + boardid + s + l
  185. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  186. post_data = {'title': '{}'.format(listtitle)}
  187. body = requests.post(list, data=post_data, headers=headers)
  188. print("=== CREATE LIST ===\n")
  189. print(body.text)
  190. # ------- CREATE LIST END -----------
  191. if sys.argv[1] == 'list':
  192. # ------- LIST OF BOARD START -----------
  193. boardid = sys.argv[2]
  194. listid = sys.argv[3]
  195. listone = wekanurl + apiboards + boardid + s + l + s + listid
  196. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  197. print("=== INFO OF ONE LIST ===\n")
  198. body = requests.get(listone, headers=headers)
  199. data2 = body.text.replace('}',"}\n")
  200. print(data2)
  201. # ------- LISTS OF BOARD END -----------
  202. if sys.argv[1] == 'customfield':
  203. # ------- INFO OF CUSTOM FIELD START -----------
  204. boardid = sys.argv[2]
  205. customfieldid = sys.argv[3]
  206. customfieldone = wekanurl + apiboards + boardid + s + cf + s + customfieldid
  207. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  208. print("=== INFO OF ONE CUSTOM FIELD ===\n")
  209. body = requests.get(customfieldone, headers=headers)
  210. data2 = body.text.replace('}',"}\n")
  211. print(data2)
  212. # ------- INFO OF CUSTOM FIELD END -----------
  213. if arguments == 2:
  214. # ------- BOARDS LIST START -----------
  215. userid = sys.argv[2]
  216. boards = users + s + userid + s + bs
  217. if sys.argv[1] == 'boards':
  218. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  219. #post_data = {'userId': '{}'.format(userid)}
  220. body = requests.get(boards, headers=headers)
  221. print("=== BOARDS ===\n")
  222. data2 = body.text.replace('}',"}\n")
  223. print(data2)
  224. # ------- BOARDS LIST END -----------
  225. if sys.argv[1] == 'board':
  226. # ------- BOARD INFO START -----------
  227. boardid = sys.argv[2]
  228. board = wekanurl + apiboards + boardid
  229. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  230. body = requests.get(board, headers=headers)
  231. print("=== BOARD ===\n")
  232. data2 = body.text.replace('}',"}\n")
  233. print(data2)
  234. # ------- BOARD INFO END -----------
  235. if sys.argv[1] == 'customfields':
  236. # ------- CUSTOM FIELDS OF BOARD START -----------
  237. boardid = sys.argv[2]
  238. boardcustomfields = wekanurl + apiboards + boardid + s + cf
  239. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  240. body = requests.get(boardcustomfields, headers=headers)
  241. print("=== CUSTOM FIELDS OF BOARD ===\n")
  242. data2 = body.text.replace('}',"}\n")
  243. print(data2)
  244. # ------- CUSTOM FIELDS OF BOARD END -----------
  245. if sys.argv[1] == 'swimlanes':
  246. boardid = sys.argv[2]
  247. swimlanes = wekanurl + apiboards + boardid + s + sws
  248. # ------- SWIMLANES OF BOARD START -----------
  249. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  250. print("=== SWIMLANES ===\n")
  251. body = requests.get(swimlanes, headers=headers)
  252. data2 = body.text.replace('}',"}\n")
  253. print(data2)
  254. # ------- SWIMLANES OF BOARD END -----------
  255. if sys.argv[1] == 'lists':
  256. # ------- LISTS OF BOARD START -----------
  257. boardid = sys.argv[2]
  258. lists = wekanurl + apiboards + boardid + s + l
  259. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  260. print("=== LISTS ===\n")
  261. body = requests.get(lists, headers=headers)
  262. data2 = body.text.replace('}',"}\n")
  263. print(data2)
  264. # ------- LISTS OF BOARD END -----------
  265. if sys.argv[1] == 'listattachments':
  266. # ------- LISTS OF ATTACHMENTS START -----------
  267. boardid = sys.argv[2]
  268. listattachments = wekanurl + apiboards + boardid + s + ats
  269. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  270. print("=== LIST OF ATTACHMENTS ===\n")
  271. body = requests.get(listattachments, headers=headers)
  272. data2 = body.text.replace('}',"}\n")
  273. print(data2)
  274. # ------- LISTS OF ATTACHMENTS END -----------
  275. if arguments == 1:
  276. if sys.argv[1] == 'users':
  277. # ------- LIST OF USERS START -----------
  278. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  279. print(users)
  280. print("=== USERS ===\n")
  281. body = requests.get(users, headers=headers)
  282. data2 = body.text.replace('}',"}\n")
  283. print(data2)
  284. # ------- LIST OF USERS END -----------
  285. if sys.argv[1] == 'boards':
  286. # ------- LIST OF PUBLIC BOARDS START -----------
  287. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  288. print("=== PUBLIC BOARDS ===\n")
  289. listpublicboards = wekanurl + apiboards
  290. body = requests.get(listpublicboards, headers=headers)
  291. data2 = body.text.replace('}',"}\n")
  292. print(data2)
  293. # ------- LIST OF PUBLIC BOARDS END -----------