api.py 13 KB

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