api.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. print(" python3 api.py newuser USERNAME EMAIL PASSWORD")
  38. # TODO:
  39. # print(" python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64")
  40. # print(" python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file")
  41. # print(" python3 api.py attachmentdownload BOARDID ATTACHMENTID # One attachment as file")
  42. # print(" python3 api.py attachmentsdownload BOARDID # All attachments as files")
  43. exit
  44. # ------- SETTINGS START -------------
  45. # Username is your Wekan username or email address.
  46. # OIDC/OAuth2 etc uses email address as username.
  47. username = 'testtest'
  48. password = 'testtest'
  49. wekanurl = 'http://localhost:4000/'
  50. # ------- SETTINGS END -------------
  51. """
  52. EXAMPLE:
  53. python3 api.py
  54. OR:
  55. chmod +x api.py
  56. ./api.py
  57. === Wekan API Python CLI: Shows IDs for addcard ===
  58. AUTHORID is USERID that writes card.
  59. Syntax:
  60. python3 api.py users # All users
  61. python3 api.py boards USERID # Boards of USERID
  62. python3 api.py board BOARDID # Info of BOARDID
  63. python3 api.py customfields BOARDID # Custom Fields of BOARDID
  64. python3 api.py customfield BOARDID CUSTOMFIELDID # Info of CUSTOMFIELDID
  65. python3 api.py addcustomfieldtoboard AUTHORID BOARDID NAME TYPE SETTINGS SHOWONCARD AUTOMATICALLYONCARD SHOWLABELONMINICARD SHOWSUMATTOPOFLIST # Add Custom Field to Board
  66. python3 api.py swimlanes BOARDID # Swimlanes of BOARDID
  67. python3 api.py lists BOARDID # Lists of BOARDID
  68. python3 api.py list BOARDID LISTID # Info of LISTID
  69. python3 api.py createlist BOARDID LISTTITLE # Create list
  70. python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  71. python3 api.py editcard BOARDID LISTID CARDID NEWCARDTITLE NEWCARDDESCRIPTION
  72. python3 api.py listattachments BOARDID # List attachments
  73. python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64
  74. python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file
  75. === ADD CUSTOM FIELD TO BOARD ===
  76. Type: text, number, date, dropdown, checkbox, currency, stringtemplate.
  77. python3 api.py addcustomfieldtoboard cmx3gmHLKwAXLqjxz LcDW4QdooAx8hsZh8 "SomeField" "date" "" true true true true
  78. === USERS ===
  79. python3 api.py users
  80. => abcd1234
  81. === BOARDS ===
  82. python3 api.py boards abcd1234
  83. === SWIMLANES ===
  84. python3 api.py swimlanes dYZ
  85. [{"_id":"Jiv","title":"Default"}
  86. ]
  87. === LISTS ===
  88. python3 api.py lists dYZ
  89. []
  90. There is no lists, so create a list:
  91. === CREATE LIST ===
  92. python3 api.py createlist dYZ 'Test'
  93. {"_id":"7Kp"}
  94. # python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  95. python3 api.py addcard ppg dYZ Jiv 7Kp 'Test card' 'Test description'
  96. === LIST ATTACHMENTS WITH DOWNLOAD URLs ====
  97. python3 api.py listattachments BOARDID
  98. """
  99. # ------- API URL GENERATION START -----------
  100. loginurl = 'users/login'
  101. wekanloginurl = wekanurl + loginurl
  102. apiboards = 'api/boards/'
  103. apiattachments = 'api/attachments/'
  104. apiusers = 'api/users'
  105. e = 'export'
  106. s = '/'
  107. l = 'lists'
  108. sw = 'swimlane'
  109. sws = 'swimlanes'
  110. cs = 'cards'
  111. cf = 'custom-fields'
  112. bs = 'boards'
  113. atl = 'attachmentslist'
  114. at = 'attachment'
  115. ats = 'attachments'
  116. users = wekanurl + apiusers
  117. # ------- API URL GENERATION END -----------
  118. # ------- LOGIN TOKEN START -----------
  119. data = {"username": username, "password": password}
  120. body = requests.post(wekanloginurl, json=data)
  121. d = body.json()
  122. apikey = d['token']
  123. # ------- LOGIN TOKEN END -----------
  124. if arguments == 10:
  125. if sys.argv[1] == 'addcustomfieldtoboard':
  126. # ------- ADD CUSTOM FIELD TO BOARD START -----------
  127. authorid = sys.argv[2]
  128. boardid = sys.argv[3]
  129. name = sys.argv[4]
  130. type1 = sys.argv[5]
  131. settings = str(json.loads(sys.argv[6]))
  132. # There is error: Settings must be object. So this does not work yet.
  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 == 4:
  181. if sys.argv[1] == 'newuser':
  182. # ------- CREATE NEW USER START -----------
  183. username = sys.argv[2]
  184. email = sys.argv[3]
  185. password = sys.argv[4]
  186. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  187. post_data = {'username': '{}'.format(username),'email': '{}'.format(email),'password': '{}'.format(password)}
  188. body = requests.post(users, data=post_data, headers=headers)
  189. print("=== CREATE NEW USER ===\n")
  190. print(body.text)
  191. # ------- CREATE NEW USER END -----------
  192. if arguments == 3:
  193. if sys.argv[1] == 'createlist':
  194. # ------- CREATE LIST START -----------
  195. boardid = sys.argv[2]
  196. listtitle = sys.argv[3]
  197. list = wekanurl + apiboards + boardid + s + l
  198. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  199. post_data = {'title': '{}'.format(listtitle)}
  200. body = requests.post(list, data=post_data, headers=headers)
  201. print("=== CREATE LIST ===\n")
  202. print(body.text)
  203. # ------- CREATE LIST END -----------
  204. if sys.argv[1] == 'list':
  205. # ------- LIST OF BOARD START -----------
  206. boardid = sys.argv[2]
  207. listid = sys.argv[3]
  208. listone = wekanurl + apiboards + boardid + s + l + s + listid
  209. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  210. print("=== INFO OF ONE LIST ===\n")
  211. body = requests.get(listone, headers=headers)
  212. data2 = body.text.replace('}',"}\n")
  213. print(data2)
  214. # ------- LISTS OF BOARD END -----------
  215. if sys.argv[1] == 'customfield':
  216. # ------- INFO OF CUSTOM FIELD START -----------
  217. boardid = sys.argv[2]
  218. customfieldid = sys.argv[3]
  219. customfieldone = wekanurl + apiboards + boardid + s + cf + s + customfieldid
  220. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  221. print("=== INFO OF ONE CUSTOM FIELD ===\n")
  222. body = requests.get(customfieldone, headers=headers)
  223. data2 = body.text.replace('}',"}\n")
  224. print(data2)
  225. # ------- INFO OF CUSTOM FIELD END -----------
  226. if arguments == 2:
  227. # ------- BOARDS LIST START -----------
  228. userid = sys.argv[2]
  229. boards = users + s + userid + s + bs
  230. if sys.argv[1] == 'boards':
  231. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  232. #post_data = {'userId': '{}'.format(userid)}
  233. body = requests.get(boards, headers=headers)
  234. print("=== BOARDS ===\n")
  235. data2 = body.text.replace('}',"}\n")
  236. print(data2)
  237. # ------- BOARDS LIST END -----------
  238. if sys.argv[1] == 'board':
  239. # ------- BOARD INFO START -----------
  240. boardid = sys.argv[2]
  241. board = wekanurl + apiboards + boardid
  242. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  243. body = requests.get(board, headers=headers)
  244. print("=== BOARD ===\n")
  245. data2 = body.text.replace('}',"}\n")
  246. print(data2)
  247. # ------- BOARD INFO END -----------
  248. if sys.argv[1] == 'customfields':
  249. # ------- CUSTOM FIELDS OF BOARD START -----------
  250. boardid = sys.argv[2]
  251. boardcustomfields = wekanurl + apiboards + boardid + s + cf
  252. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  253. body = requests.get(boardcustomfields, headers=headers)
  254. print("=== CUSTOM FIELDS OF BOARD ===\n")
  255. data2 = body.text.replace('}',"}\n")
  256. print(data2)
  257. # ------- CUSTOM FIELDS OF BOARD END -----------
  258. if sys.argv[1] == 'swimlanes':
  259. boardid = sys.argv[2]
  260. swimlanes = wekanurl + apiboards + boardid + s + sws
  261. # ------- SWIMLANES OF BOARD START -----------
  262. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  263. print("=== SWIMLANES ===\n")
  264. body = requests.get(swimlanes, headers=headers)
  265. data2 = body.text.replace('}',"}\n")
  266. print(data2)
  267. # ------- SWIMLANES OF BOARD END -----------
  268. if sys.argv[1] == 'lists':
  269. # ------- LISTS OF BOARD START -----------
  270. boardid = sys.argv[2]
  271. lists = wekanurl + apiboards + boardid + s + l
  272. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  273. print("=== LISTS ===\n")
  274. body = requests.get(lists, headers=headers)
  275. data2 = body.text.replace('}',"}\n")
  276. print(data2)
  277. # ------- LISTS OF BOARD END -----------
  278. if sys.argv[1] == 'listattachments':
  279. # ------- LISTS OF ATTACHMENTS START -----------
  280. boardid = sys.argv[2]
  281. listattachments = wekanurl + apiboards + boardid + s + ats
  282. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  283. print("=== LIST OF ATTACHMENTS ===\n")
  284. body = requests.get(listattachments, headers=headers)
  285. data2 = body.text.replace('}',"}\n")
  286. print(data2)
  287. # ------- LISTS OF ATTACHMENTS END -----------
  288. if arguments == 1:
  289. if sys.argv[1] == 'users':
  290. # ------- LIST OF USERS START -----------
  291. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  292. print(users)
  293. print("=== USERS ===\n")
  294. body = requests.get(users, headers=headers)
  295. data2 = body.text.replace('}',"}\n")
  296. print(data2)
  297. # ------- LIST OF USERS END -----------
  298. if sys.argv[1] == 'boards':
  299. # ------- LIST OF PUBLIC BOARDS START -----------
  300. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  301. print("=== PUBLIC BOARDS ===\n")
  302. listpublicboards = wekanurl + apiboards
  303. body = requests.get(listpublicboards, headers=headers)
  304. data2 = body.text.replace('}',"}\n")
  305. print(data2)
  306. # ------- LIST OF PUBLIC BOARDS END -----------