api.py 14 KB

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