rsafeclient.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. extern crate rsafe;
  2. use std::io;
  3. use rsafe::*;
  4. fn main() {
  5. //--------------------------------------------------------------------------------
  6. // First we register with Safe Launcher
  7. //--------------------------------------------------------------------------------
  8. // change this with your own aaplication details
  9. let appdetails = auth::AppDetails {
  10. name: "appname".to_string(),
  11. version: "0.0.1".to_string(),
  12. vendor: "vendorname".to_string(),
  13. id: "myID".to_string(),
  14. permissions: vec! []
  15. };
  16. // register our app with the safe launcher
  17. let safe_register_resp = auth::register( appdetails );
  18. //handle errors
  19. match safe_register_resp {
  20. Err(e) => { // something went wrong : launcher is not running , user didn't allow application in launcher ...
  21. println!("{:?}\nUnable to Connect to Launcher \nMake sure Safe Launcher is running", e);
  22. },
  23. Ok(credentials) => {
  24. // --------------------------------------------------------------------------------
  25. // We are Registered, let's play with Safe
  26. // --------------------------------------------------------------------------------
  27. println!("Succesfully Registered");
  28. // --------------------------------------------------------------------------------
  29. // Test Token
  30. // --------------------------------------------------------------------------------
  31. print!("App: Testing Token... ");
  32. let auth_check = auth::check ( &credentials );
  33. println! ( "{:?}", auth_check );
  34. // --------------------------------------------------------------------------------
  35. // Create a Directory - NFS operation
  36. // --------------------------------------------------------------------------------
  37. // use this to debug
  38. let createdir = true;
  39. if createdir
  40. {
  41. // Create base64 string for metadata
  42. //let meta = "ABCD";
  43. //let meta_b64 = encode(&meta).unwrap();
  44. //
  45. //we leave metadata empty for now
  46. let meta_b64 = String::new();
  47. // populate the struct as per API doc:
  48. let create_dir_data = nfs::CreateDirData {
  49. dirPath: "/testdir".to_string(),
  50. isPrivate: true,
  51. metadata: meta_b64,
  52. isVersioned: false,
  53. isPathShared: false
  54. };
  55. let nfs_create_dir = nfs::create_dir ( create_dir_data, &credentials );
  56. println! ( "{:?}", nfs_create_dir );
  57. }
  58. // --------------------------------------------------------------------------------
  59. // Read a Directory - NFS operation
  60. // --------------------------------------------------------------------------------
  61. // use this to debug
  62. let readdir = true;
  63. if readdir
  64. {
  65. // populate the struct as per API doc:
  66. let read_dir_data = nfs::ReadDirData {
  67. dirPath: "/".to_string(),
  68. isPathShared: false
  69. };
  70. let nfs_read_dir = nfs::read_dir ( read_dir_data, &credentials );
  71. println!(" ls resp = {:?}", nfs_read_dir );
  72. }
  73. /*
  74. // --------------------------------------------------------------------------------
  75. // Move a Directory - NFS operation
  76. // --------------------------------------------------------------------------------
  77. // use this to debug
  78. let movedir = false;
  79. if movedir
  80. {
  81. // populate the struct as per API doc:
  82. let move_dir_data = nfs::MoveDirData {
  83. srcPath: "/testdir".to_string(),
  84. destPath: "/destinationdir".to_string(),
  85. retainSource: false,
  86. isSrcPathShared: false,
  87. isDestPathShared: false
  88. };
  89. //for now, this returns 400, inconditionnally
  90. let nfs_move_dir = nfs::move_dir ( move_dir_data, &credentials );
  91. println!(" ls resp = {:?}", nfs_move_dir );
  92. }
  93. */
  94. // --------------------------------------------------------------------------------
  95. // Create a File - NFS operation
  96. // --------------------------------------------------------------------------------
  97. // use this to debug
  98. let createfile = true;
  99. if createfile
  100. {
  101. // Create base64 string for metadata
  102. //let meta = "ABCD";
  103. //let meta_b64 = encode(&meta).unwrap();
  104. //
  105. //we leave metadata empty for now
  106. let meta_b64 = String::new();
  107. // populate the struct as per API doc:
  108. let create_file_data = nfs::CreateFileData {
  109. filePath: "/testfile.txt".to_string(),
  110. isPrivate: true,
  111. metadata: meta_b64,
  112. isVersioned: false,
  113. isPathShared: false
  114. };
  115. let nfs_create_file = nfs::create_file ( create_file_data, &credentials );
  116. println!(" create file = {:?}", &nfs_create_file );
  117. }
  118. // --------------------------------------------------------------------------------
  119. // Move a File - NFS operation
  120. // --------------------------------------------------------------------------------
  121. // use this to debug
  122. let movefile = true;
  123. if movefile
  124. {
  125. // populate the struct as per API doc:
  126. let move_file_data = nfs::MoveFileData {
  127. srcPath: "/testfile.txt".to_string(),
  128. destPath: "/testdir".to_string(),
  129. retainSource: false,
  130. isSrcPathShared: false,
  131. isDestPathShared: false
  132. };
  133. let nfs_move_file = nfs::move_file ( move_file_data, &credentials );
  134. println!(" ls resp = {:?}", nfs_move_file );
  135. }
  136. // --------------------------------------------------------------------------------
  137. // Write to a File - NFS operation
  138. // --------------------------------------------------------------------------------
  139. // use this to debug
  140. let writefile = true;
  141. if writefile
  142. {
  143. // populate the struct as per API doc:
  144. let write_file_data = nfs::WriteFileData {
  145. filePath: "/testdir/testfile.txt".to_string(),
  146. isPathShared: false,
  147. fileContent : "This is just a sample text!!!".to_string(),
  148. offset : 0 // seems to be unsupported
  149. };
  150. let nfs_write_file = nfs::write_file ( write_file_data, &credentials );
  151. println!(" write file = {:?}", &nfs_write_file );
  152. }
  153. // --------------------------------------------------------------------------------
  154. // Read a File - NFS operation
  155. // --------------------------------------------------------------------------------
  156. // use this to debug
  157. let readfile = true;
  158. if readfile
  159. {
  160. // are offset and length really supported ??
  161. // populate the struct as per API doc:
  162. let read_file_data = nfs::ReadFileData {
  163. filePath: "/testdir/testfile.txt".to_string(),
  164. isPathShared: false,
  165. offset: 0, // seems to be unsupported
  166. length: 0, // seems to be unsupported , use length: 0 to read the entire file
  167. };
  168. let nfs_read_file = nfs::read_file ( read_file_data, &credentials);
  169. println!(" ls resp = {:?}", &nfs_read_file.unwrap() );
  170. }
  171. // --------------------------------------------------------------------------------
  172. // Delete a File - NFS operation
  173. // --------------------------------------------------------------------------------
  174. // use this to debug
  175. let deletefile = true;
  176. if deletefile
  177. {
  178. // populate the struct as per API doc:
  179. let delete_file_data = nfs::DeleteFileData {
  180. filePath: "/testdir/testfile.txt".to_string(),
  181. isPathShared: false
  182. };
  183. let nfs_delete_file = nfs::delete_file ( delete_file_data, &credentials );
  184. println!(" delete resp = {:?}", &nfs_delete_file );
  185. }
  186. // --------------------------------------------------------------------------------
  187. // Delete a Directory - NFS operation
  188. // --------------------------------------------------------------------------------
  189. // use this to debug
  190. let deletedir = true;
  191. if deletedir
  192. {
  193. // populate the struct as per API doc:
  194. let delete_dir_data = nfs::ReadDirData {
  195. dirPath: "/testdir".to_string(),
  196. isPathShared: false
  197. };
  198. let nfs_delete_dir = nfs::delete_dir ( delete_dir_data, &credentials );
  199. println!(" delete resp = {:?}", &nfs_delete_dir );
  200. }
  201. /*
  202. // --------------------------------------------------------------------------------
  203. // Register a Long Name - DNS operation
  204. // --------------------------------------------------------------------------------
  205. // use this to debug
  206. let registerlongname = true;
  207. if registerlongname
  208. {
  209. let longName = "testlongname5".to_string();
  210. let dns_register_long_name = dns::register_long_name ( longName, &credentials );
  211. println!(" register long name resp = {:?}", &dns_register_long_name );
  212. }
  213. // --------------------------------------------------------------------------------
  214. // Create a Directory - NFS operation
  215. // --------------------------------------------------------------------------------
  216. // use this to debug
  217. let createdir = true;
  218. if createdir
  219. {
  220. // Create base64 string for metadata
  221. //let meta = "ABCD";
  222. //let meta_b64 = encode(&meta).unwrap();
  223. //
  224. //we leave metadata empty for now
  225. let meta_b64 = String::new();
  226. // populate the struct as per API doc:
  227. let create_dir_data = nfs::CreateDirData {
  228. dirPath: "/ww2".to_string(),
  229. isPrivate: true,
  230. metadata: meta_b64,
  231. isVersioned: false,
  232. isPathShared: false
  233. };
  234. let nfs_create_dir = nfs::create_dir ( create_dir_data, &credentials );
  235. println! ( "{:?}", nfs_create_dir );
  236. }
  237. // --------------------------------------------------------------------------------
  238. // Register a service for a long name. - DNS operation
  239. // --------------------------------------------------------------------------------
  240. // use this to debug
  241. let registerservice = true;
  242. if registerservice
  243. {
  244. let register_service_data = dns::RegisterServiceData
  245. {
  246. longName: "testlongname5".to_string(),
  247. serviceName: "ww2".to_string(),
  248. serviceHomeDirPath: "/ww2".to_string(),
  249. isPathShared: false
  250. };
  251. let dns_register_service = dns::register_service ( register_service_data, &credentials );
  252. println!(" register service resp = {:?}", &dns_register_service );
  253. }
  254. // --------------------------------------------------------------------------------
  255. // Delete a Directory - NFS operation
  256. // --------------------------------------------------------------------------------
  257. // use this to debug
  258. let deletedir = true;
  259. if deletedir
  260. {
  261. // populate the struct as per API doc:
  262. let delete_dir_data = nfs::ReadDirData {
  263. dirPath: "/www".to_string(),
  264. isPathShared: false
  265. };
  266. let nfs_delete_dir = nfs::delete_dir ( delete_dir_data, &credentials );
  267. println!(" delete resp = {:?}", &nfs_delete_dir );
  268. }
  269. */
  270. // --------------------------------------------------------------------------------
  271. // Do Something
  272. // --------------------------------------------------------------------------------
  273. println! ("Hellooo!");
  274. // hit enter to quit
  275. let mut enter = String::new();
  276. println! ("Press enter to quit");
  277. io::stdin().read_line(&mut enter)
  278. .expect("Failed to read line");
  279. // --------------------------------------------------------------------------------
  280. // Clear Token
  281. // --------------------------------------------------------------------------------
  282. print!("App: Releasing Token... ");
  283. let deauth = auth::unregister ( &credentials );
  284. println! ( "quit {:?}", deauth );
  285. if deauth.unwrap() == 200 {
  286. println!( "Token released, bye" );
  287. }
  288. } // end of Ok(credentials)
  289. } // end of match
  290. return
  291. }