api.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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. python3 api.py cardsbyswimlane SWIMLANEID LISTID
  37. python3 api.py getcard BOARDID LISTID CARDID
  38. python3 api.py addlabel BOARDID LISTID CARDID LABELID
  39. python3 api.py addcardwithlabel AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION LABELIDS
  40. python3 api.py editboardtitle BOARDID NEWBOARDTITLE
  41. Admin API:
  42. python3 api.py users # All users
  43. python3 api.py boards # All Public Boards
  44. python3 api.py newuser USERNAME EMAIL PASSWORD
  45. """
  46. if arguments == 0:
  47. print(syntax)
  48. exit
  49. # TODO:
  50. # print(" python3 api.py attachmentjson BOARDID ATTACHMENTID # One attachment as JSON base64")
  51. # print(" python3 api.py attachmentbinary BOARDID ATTACHMENTID # One attachment as binary file")
  52. # print(" python3 api.py attachmentdownload BOARDID ATTACHMENTID # One attachment as file")
  53. # print(" python3 api.py attachmentsdownload BOARDID # All attachments as files")
  54. # ------- SETTINGS START -------------
  55. # Username is your Wekan username or email address.
  56. # OIDC/OAuth2 etc uses email address as username.
  57. username = 'testtest'
  58. password = 'testtest'
  59. wekanurl = 'http://localhost:4000/'
  60. # ------- SETTINGS END -------------
  61. """
  62. === ADD CUSTOM FIELD TO BOARD ===
  63. Type: text, number, date, dropdown, checkbox, currency, stringtemplate.
  64. python3 api.py addcustomfieldtoboard cmx3gmHLKwAXLqjxz LcDW4QdooAx8hsZh8 "SomeField" "date" "" true true true true
  65. === USERS ===
  66. python3 api.py users
  67. => abcd1234
  68. === BOARDS ===
  69. python3 api.py boards abcd1234
  70. === SWIMLANES ===
  71. python3 api.py swimlanes dYZ
  72. [{"_id":"Jiv","title":"Default"}
  73. ]
  74. === LISTS ===
  75. python3 api.py lists dYZ
  76. []
  77. There is no lists, so create a list:
  78. === CREATE LIST ===
  79. python3 api.py createlist dYZ 'Test'
  80. {"_id":"7Kp"}
  81. # python3 api.py addcard AUTHORID BOARDID SWIMLANEID LISTID CARDTITLE CARDDESCRIPTION
  82. python3 api.py addcard ppg dYZ Jiv 7Kp 'Test card' 'Test description'
  83. === LIST ATTACHMENTS WITH DOWNLOAD URLs ====
  84. python3 api.py listattachments BOARDID
  85. """
  86. # ------- API URL GENERATION START -----------
  87. loginurl = 'users/login'
  88. wekanloginurl = wekanurl + loginurl
  89. apiboards = 'api/boards/'
  90. apiattachments = 'api/attachments/'
  91. apiusers = 'api/users'
  92. apiuser = 'api/user'
  93. apiallusers = 'api/allusers'
  94. e = 'export'
  95. s = '/'
  96. l = 'lists'
  97. sw = 'swimlane'
  98. sws = 'swimlanes'
  99. cs = 'cards'
  100. cf = 'custom-fields'
  101. bs = 'boards'
  102. apbs = 'allpublicboards'
  103. atl = 'attachmentslist'
  104. at = 'attachment'
  105. ats = 'attachments'
  106. users = wekanurl + apiusers
  107. user = wekanurl + apiuser
  108. allusers = wekanurl + apiallusers
  109. # ------- API URL GENERATION END -----------
  110. # ------- LOGIN TOKEN START -----------
  111. data = {"username": username, "password": password}
  112. body = requests.post(wekanloginurl, json=data)
  113. d = body.json()
  114. apikey = d['token']
  115. # ------- LOGIN TOKEN END -----------
  116. if arguments == 10:
  117. if sys.argv[1] == 'addcustomfieldtoboard':
  118. # ------- ADD CUSTOM FIELD TO BOARD START -----------
  119. authorid = sys.argv[2]
  120. boardid = sys.argv[3]
  121. name = sys.argv[4]
  122. type1 = sys.argv[5]
  123. settings = str(json.loads(sys.argv[6]))
  124. # There is error: Settings must be object. So this does not work yet.
  125. #settings = {'currencyCode': 'EUR'}
  126. print(type(settings))
  127. showoncard = sys.argv[7]
  128. automaticallyoncard = sys.argv[8]
  129. showlabelonminicard = sys.argv[9]
  130. showsumattopoflist = sys.argv[10]
  131. customfieldtoboard = wekanurl + apiboards + boardid + s + cf
  132. # Add Custom Field to Board
  133. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  134. 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)}
  135. body = requests.post(customfieldtoboard, data=post_data, headers=headers)
  136. print(body.text)
  137. # ------- ADD CUSTOM FIELD TO BOARD END -----------
  138. if arguments == 8:
  139. if sys.argv[1] == 'addcardwithlabel':
  140. # ------- ADD CARD WITH LABEL START -----------
  141. authorid = sys.argv[2]
  142. boardid = sys.argv[3]
  143. swimlaneid = sys.argv[4]
  144. listid = sys.argv[5]
  145. cardtitle = sys.argv[6]
  146. carddescription = sys.argv[7]
  147. labelIds = sys.argv[8] # Aggiunto labelIds
  148. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  149. # Add card
  150. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  151. post_data = {
  152. 'authorId': '{}'.format(authorid),
  153. 'title': '{}'.format(cardtitle),
  154. 'description': '{}'.format(carddescription),
  155. 'swimlaneId': '{}'.format(swimlaneid),
  156. 'labelIds': labelIds
  157. }
  158. body = requests.post(cardtolist, data=post_data, headers=headers)
  159. print(body.text)
  160. # If ok id card
  161. if body.status_code == 200:
  162. card_data = body.json()
  163. new_card_id = card_data.get('_id')
  164. # Updating card
  165. if new_card_id:
  166. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + new_card_id
  167. put_data = {'labelIds': labelIds}
  168. body = requests.put(edcard, data=put_data, headers=headers)
  169. print("=== EDIT CARD ===\n")
  170. body = requests.get(edcard, headers=headers)
  171. data2 = body.text.replace('}', "}\n")
  172. print(data2)
  173. else:
  174. print("Error obraining ID.")
  175. else:
  176. print("Error adding card.")
  177. # ------- ADD CARD WITH LABEL END -----------
  178. if arguments == 7:
  179. if sys.argv[1] == 'addcard':
  180. # ------- ADD CARD START -----------
  181. authorid = sys.argv[2]
  182. boardid = sys.argv[3]
  183. swimlaneid = sys.argv[4]
  184. listid = sys.argv[5]
  185. cardtitle = sys.argv[6]
  186. carddescription = sys.argv[7]
  187. cardtolist = wekanurl + apiboards + boardid + s + l + s + listid + s + cs
  188. # Add card
  189. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  190. post_data = {'authorId': '{}'.format(authorid), 'title': '{}'.format(cardtitle), 'description': '{}'.format(carddescription), 'swimlaneId': '{}'.format(swimlaneid)}
  191. body = requests.post(cardtolist, data=post_data, headers=headers)
  192. print(body.text)
  193. # ------- ADD CARD END -----------
  194. if arguments == 6:
  195. if sys.argv[1] == 'editcard':
  196. # ------- EDIT CARD START -----------
  197. boardid = sys.argv[2]
  198. listid = sys.argv[3]
  199. cardid = sys.argv[4]
  200. newcardtitle = sys.argv[5]
  201. newcarddescription = sys.argv[6]
  202. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  203. print(edcard)
  204. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  205. put_data = {'title': '{}'.format(newcardtitle), 'description': '{}'.format(newcarddescription)}
  206. body = requests.put(edcard, data=put_data, headers=headers)
  207. print("=== EDIT CARD ===\n")
  208. body = requests.get(edcard, headers=headers)
  209. data2 = body.text.replace('}',"}\n")
  210. print(data2)
  211. # ------- EDIT CARD END -----------
  212. if sys.argv[1] == 'editcustomfield':
  213. # ------- EDIT CUSTOMFIELD START -----------
  214. boardid = sys.argv[2]
  215. listid = sys.argv[3]
  216. cardid = sys.argv[4]
  217. customfieldid = sys.argv[5]
  218. newcustomfieldvalue = sys.argv[6]
  219. edfield = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid + s + 'customFields' + s + customfieldid
  220. #print(edfield)
  221. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  222. post_data = {'_id': '{}'.format(customfieldid), 'value': '{}'.format(newcustomfieldvalue)}
  223. #print(post_data)
  224. body = requests.post(edfield, data=post_data, headers=headers)
  225. print("=== EDIT CUSTOMFIELD ===\n")
  226. data2 = body.text.replace('}',"}\n")
  227. print(data2)
  228. # ------- EDIT CUSTOMFIELD END -----------
  229. if arguments == 5:
  230. if sys.argv[1] == 'addlabel':
  231. # ------- EDIT CARD ADD LABEL START -----------
  232. boardid = sys.argv[2]
  233. listid = sys.argv[3]
  234. cardid = sys.argv[4]
  235. labelIds = sys.argv[5]
  236. edcard = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  237. print(edcard)
  238. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  239. put_data = {'labelIds': labelIds}
  240. body = requests.put(edcard, data=put_data, headers=headers)
  241. print("=== ADD LABEL ===\n")
  242. body = requests.get(edcard, headers=headers)
  243. data2 = body.text.replace('}',"}\n")
  244. print(data2)
  245. # ------- EDIT CARD ADD LABEL END -----------
  246. if arguments == 4:
  247. if sys.argv[1] == 'newuser':
  248. # ------- CREATE NEW USER START -----------
  249. username = sys.argv[2]
  250. email = sys.argv[3]
  251. password = sys.argv[4]
  252. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  253. post_data = {'username': '{}'.format(username),'email': '{}'.format(email),'password': '{}'.format(password)}
  254. body = requests.post(users, data=post_data, headers=headers)
  255. print("=== CREATE NEW USER ===\n")
  256. print(body.text)
  257. # ------- CREATE NEW USER END -----------
  258. if sys.argv[1] == 'getcard':
  259. # ------- LIST OF CARD START -----------
  260. boardid = sys.argv[2]
  261. listid = sys.argv[3]
  262. cardid = sys.argv[4]
  263. listone = wekanurl + apiboards + boardid + s + l + s + listid + s + cs + s + cardid
  264. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  265. print("=== INFO OF ONE LIST ===\n")
  266. print("URL:", listone) # Stampa l'URL per debug
  267. try:
  268. response = requests.get(listone, headers=headers)
  269. print("=== RESPONSE ===\n")
  270. print("Status Code:", response.status_code) # Stampa il codice di stato per debug
  271. if response.status_code == 200:
  272. data2 = response.text.replace('}', "}\n")
  273. print(data2)
  274. else:
  275. print(f"Error: {response.status_code}")
  276. print(f"Response: {response.text}")
  277. except Exception as e:
  278. print(f"Error in the GET request: {e}")
  279. # ------- LISTS OF CARD END -----------
  280. if arguments == 3:
  281. if sys.argv[1] == 'editboardtitle':
  282. # ------- EDIT BOARD TITLE START -----------
  283. boardid = sys.argv[2]
  284. boardtitle = sys.argv[3]
  285. edboardtitle = wekanurl + apiboards + boardid + s + 'title'
  286. print(edboardtitle)
  287. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  288. post_data = {'title': boardtitle}
  289. body = requests.put(edboardtitle, json=post_data, headers=headers)
  290. print("=== EDIT BOARD TITLE ===\n")
  291. #body = requests.get(edboardtitle, headers=headers)
  292. data2 = body.text.replace('}',"}\n")
  293. print(data2)
  294. if body.status_code == 200:
  295. print("Succesfull!")
  296. else:
  297. print(f"Error: {body.status_code}")
  298. print(body.text)
  299. # ------- EDIT BOARD TITLE END -----------
  300. if sys.argv[1] == 'createlist':
  301. # ------- CREATE LIST START -----------
  302. boardid = sys.argv[2]
  303. listtitle = sys.argv[3]
  304. list = wekanurl + apiboards + boardid + s + l
  305. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  306. post_data = {'title': '{}'.format(listtitle)}
  307. body = requests.post(list, data=post_data, headers=headers)
  308. print("=== CREATE LIST ===\n")
  309. print(body.text)
  310. # ------- CREATE LIST END -----------
  311. if sys.argv[1] == 'list':
  312. # ------- LIST OF BOARD START -----------
  313. boardid = sys.argv[2]
  314. listid = sys.argv[3]
  315. listone = wekanurl + apiboards + boardid + s + l + s + listid
  316. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  317. print("=== INFO OF ONE LIST ===\n")
  318. body = requests.get(listone, headers=headers)
  319. data2 = body.text.replace('}',"}\n")
  320. print(data2)
  321. # ------- LISTS OF BOARD END -----------
  322. if sys.argv[1] == 'customfield':
  323. # ------- INFO OF CUSTOM FIELD START -----------
  324. boardid = sys.argv[2]
  325. customfieldid = sys.argv[3]
  326. customfieldone = wekanurl + apiboards + boardid + s + cf + s + customfieldid
  327. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  328. print("=== INFO OF ONE CUSTOM FIELD ===\n")
  329. body = requests.get(customfieldone, headers=headers)
  330. data2 = body.text.replace('}',"}\n")
  331. print(data2)
  332. # ------- INFO OF CUSTOM FIELD END -----------
  333. if sys.argv[1] == 'cardsbyswimlane':
  334. # ------- RETRIEVE CARDS BY SWIMLANE ID START -----------
  335. boardid = sys.argv[2]
  336. swimlaneid = sys.argv[3]
  337. cardsbyswimlane = wekanurl + apiboards + boardid + s + sws + s + swimlaneid + s + cs
  338. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  339. print("=== CARDS BY SWIMLANE ID ===\n")
  340. print("URL:", cardsbyswimlane) # Debug
  341. try:
  342. body = requests.get(cardsbyswimlane, headers=headers)
  343. print("Status Code:", body.status_code) # Debug
  344. data = body.text.replace('}', "}\n")
  345. print("Data:", data)
  346. except Exception as e:
  347. print("Error GET:", e)
  348. # ------- RETRIEVE CARDS BY SWIMLANE ID END -----------
  349. if arguments == 2:
  350. # ------- BOARDS LIST START -----------
  351. userid = sys.argv[2]
  352. boards = users + s + userid + s + bs
  353. if sys.argv[1] == 'boards':
  354. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  355. #post_data = {'userId': '{}'.format(userid)}
  356. body = requests.get(boards, headers=headers)
  357. print("=== BOARDS ===\n")
  358. data2 = body.text.replace('}',"}\n")
  359. print(data2)
  360. # ------- BOARDS LIST END -----------
  361. if sys.argv[1] == 'board':
  362. # ------- BOARD INFO START -----------
  363. boardid = sys.argv[2]
  364. board = wekanurl + apiboards + boardid
  365. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  366. body = requests.get(board, headers=headers)
  367. print("=== BOARD ===\n")
  368. data2 = body.text.replace('}',"}\n")
  369. print(data2)
  370. # ------- BOARD INFO END -----------
  371. if sys.argv[1] == 'customfields':
  372. # ------- CUSTOM FIELDS OF BOARD START -----------
  373. boardid = sys.argv[2]
  374. boardcustomfields = wekanurl + apiboards + boardid + s + cf
  375. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  376. body = requests.get(boardcustomfields, headers=headers)
  377. print("=== CUSTOM FIELDS OF BOARD ===\n")
  378. data2 = body.text.replace('}',"}\n")
  379. print(data2)
  380. # ------- CUSTOM FIELDS OF BOARD END -----------
  381. if sys.argv[1] == 'swimlanes':
  382. boardid = sys.argv[2]
  383. swimlanes = wekanurl + apiboards + boardid + s + sws
  384. # ------- SWIMLANES OF BOARD START -----------
  385. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  386. print("=== SWIMLANES ===\n")
  387. body = requests.get(swimlanes, headers=headers)
  388. data2 = body.text.replace('}',"}\n")
  389. print(data2)
  390. # ------- SWIMLANES OF BOARD END -----------
  391. if sys.argv[1] == 'lists':
  392. # ------- LISTS OF BOARD START -----------
  393. boardid = sys.argv[2]
  394. lists = wekanurl + apiboards + boardid + s + l
  395. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  396. print("=== LISTS ===\n")
  397. body = requests.get(lists, headers=headers)
  398. data2 = body.text.replace('}',"}\n")
  399. print(data2)
  400. # ------- LISTS OF BOARD END -----------
  401. if sys.argv[1] == 'listattachments':
  402. # ------- LISTS OF ATTACHMENTS START -----------
  403. boardid = sys.argv[2]
  404. listattachments = wekanurl + apiboards + boardid + s + ats
  405. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  406. print("=== LIST OF ATTACHMENTS ===\n")
  407. body = requests.get(listattachments, headers=headers)
  408. data2 = body.text.replace('}',"}\n")
  409. print(data2)
  410. # ------- LISTS OF ATTACHMENTS END -----------
  411. if arguments == 1:
  412. if sys.argv[1] == 'users':
  413. # ------- LIST OF USERS START -----------
  414. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  415. print(users)
  416. print("=== USERS ===\n")
  417. body = requests.get(users, headers=headers)
  418. data2 = body.text.replace('}',"}\n")
  419. print(data2)
  420. # ------- LIST OF USERS END -----------
  421. if sys.argv[1] == 'user':
  422. # ------- LIST OF ALL USERS START -----------
  423. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  424. print(user)
  425. print("=== USER ===\n")
  426. body = requests.get(user, headers=headers)
  427. data2 = body.text.replace('}',"}\n")
  428. print(data2)
  429. # ------- LIST OF ALL USERS END -----------
  430. if sys.argv[1] == 'boards':
  431. # ------- LIST OF PUBLIC BOARDS START -----------
  432. headers = {'Accept': 'application/json', 'Authorization': 'Bearer {}'.format(apikey)}
  433. print("=== PUBLIC BOARDS ===\n")
  434. listpublicboards = wekanurl + apiboards
  435. body = requests.get(listpublicboards, headers=headers)
  436. data2 = body.text.replace('}',"}\n")
  437. print(data2)
  438. # ------- LIST OF PUBLIC BOARDS END -----------