public mmria.common.model.couchdb.session_response get_session() { mmria.common.model.couchdb.session_response result = null; string URL = this.mmria_url + "/api/sessionDB"; //string urlParameters = "?api_key=123"; string urlParameters = ""; HttpClient client = new HttpClient(); client.BaseAddress = new Uri(URL); var curl = new cURL("GET", null, URL, null, null); try { string json_result = curl.execute(); result = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.model.couchdb.session_response> (json_result); } catch (Exception ex) { Console.WriteLine("{0}", ex); } return(result); }
public IEnumerable <mmria.common.model.couchdb.login_response> login(string p_user_id, string p_password) { this.user_name = p_user_id; this.password = p_password; string login_url = this.mmria_url + "/api/sessionDB/"; var login_data = new { userid = this.user_name, password = this.password }; var login_curl = new cURL("POST", null, login_url, Newtonsoft.Json.JsonConvert.SerializeObject(login_data)); string login_result_string = null; dynamic json_response = null; try { login_result_string = login_curl.execute(); json_response = Newtonsoft.Json.JsonConvert.DeserializeObject <IEnumerable <mmria.common.model.couchdb.login_response> >(login_result_string); } catch (Exception ex) { System.Console.WriteLine(ex); } if (json_response[0].ok == true) { if (json_response[0].name != null) { this.roles.AddRange(json_response[0].roles as IList <string>); this.auth_token = json_response[0].auth_session; } } return(null); }
private string get_revision(string p_document_url) { string result = null; var document_curl = new cURL("GET", null, p_document_url, null, this.user_name, this.password); string document_json = null; try { document_json = document_curl.execute(); var request_result = Newtonsoft.Json.JsonConvert.DeserializeObject <System.Dynamic.ExpandoObject> (document_json); IDictionary <string, object> updater = request_result as IDictionary <string, object>; result = updater ["_rev"].ToString(); } catch (Exception ex) { if (!(ex.Message.IndexOf("(404) Object Not Found") > -1)) { //System.Console.WriteLine ("c_sync_document.get_revision"); //System.Console.WriteLine (ex); } } return(result); }
public mmria.common.model.couchdb.document_put_response set_case(string case_json) { mmria.common.model.couchdb.document_put_response result = null; string URL = this.mmria_url + "/api/case"; dynamic json_response = null; var curl = new cURL("GET", null, URL, null, null); var byteArray = System.Text.Encoding.ASCII.GetBytes(string.Format("{0}:{1}", this.user_name, this.password)); curl.AddHeader("Basic", Convert.ToBase64String(byteArray)); //curl.AddHeader ("AuthSession", this.auth_token); try { string json_result = curl.execute(); json_response = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.model.couchdb.document_put_response> (json_result); } catch (Exception ex) { Console.WriteLine("{0}", ex); } if (json_response.ok == true) { if (json_response.auth_session != null && !string.IsNullOrWhiteSpace(json_response.auth_session)) { this.auth_token = json_response.auth_session; } } else { } return(json_response); }
public mmria.common.metadata.app get_metadata() { mmria.common.metadata.app result = null; string URL = this.mmria_url + "/api/metadata"; //string urlParameters = "?api_key=123"; string urlParameters = ""; var curl = new cURL("GET", null, URL, null, null); try { string json_result = curl.execute(); result = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.metadata.app> (json_result); } catch (Exception ex) { Console.WriteLine("{0}", ex); } return(result); }
public dynamic get_all_cases(string p_database_url) { bool is_offline_mode = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["is_offline_mode"]); if (is_offline_mode) { return(get_all_cases()); } this.database_url = p_database_url; /* * var credential = new System.Net.NetworkCredential * { * UserName = "******", * Password = "******" * }; * * var httpClientHandler = new System.Net.Http.HttpClientHandler * { * Credentials = credential, * PreAuthenticate = false, * Proxy = new WebProxy("http://127.0.0.1:8888"), * UseProxy = true, * }; * * HttpClient client = new HttpClient(httpClientHandler); */ System.Dynamic.ExpandoObject result = null; string URL = this.database_url + "/mmrds/_all_docs"; string urlParameters = "?include_docs=true"; var curl = new cURL("GET", null, URL, null, null); var byteArray = System.Text.Encoding.ASCII.GetBytes(string.Format("{0}:{1}", this.user_name, this.password)); curl.AddHeader("Basic", Convert.ToBase64String(byteArray)); //curl.AddHeader ("AuthSession", this.auth_token); try { string json_result = curl.execute(); result = Newtonsoft.Json.JsonConvert.DeserializeObject <System.Dynamic.ExpandoObject> (json_result); } catch (Exception ex) { Console.WriteLine("{0}", ex); } /* * * HttpClient client = new HttpClient(); * client.BaseAddress = new Uri(URL); * * var byteArray = System.Text.Encoding.ASCII.GetBytes(string.Format("{0}:{1}", this.user_name, this.password)); * client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); * * * // Add an Accept header for JSON format. * client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); * * // List data response. * HttpResponseMessage response = client.GetAsync(urlParameters).Result; // Blocking call! * if (response.IsSuccessStatusCode) * { * // Parse the response body. Blocking! * result = response.Content.ReadAsAsync<System.Dynamic.ExpandoObject>().Result; * } * else * { * Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); * }*/ return(result); }
public async Task Execute(string[] args) { if (args.Length > 1) { for (var i = 1; i < args.Length; i++) { string arg = args[i]; int index = arg.IndexOf(':'); string val = arg.Substring(index + 1, arg.Length - (index + 1)).Trim(new char[] { '\"' }); if (arg.ToLower().StartsWith("auth_token")) { this.auth_token = val; } else if (arg.ToLower().StartsWith("user_name")) { this.user_name = val; } else if (arg.ToLower().StartsWith("password")) { this.password = val; } else if (arg.ToLower().StartsWith("source_file_path")) { this.source_file_path = val; } else if (arg.ToLower().StartsWith("url")) { this.mmria_url = val; } } } if (string.IsNullOrWhiteSpace(this.source_file_path)) { System.Console.WriteLine("missing source_file_path"); System.Console.WriteLine(" form source_file_path:[file path]"); System.Console.WriteLine(" example 1 source_file_path:c:/temp/"); System.Console.WriteLine(" example 2 source_file_path:\"c:/temp folder/\""); System.Console.WriteLine(" example 3 source_file_path:mapping-file-set\\\""); System.Console.WriteLine(" mmria-console.exe export user_name:user1 password:secret url:http://localhost:12345 source_file_path:\"c:\\temp folder\\\""); return; } if (string.IsNullOrWhiteSpace(this.mmria_url)) { System.Console.WriteLine("missing url"); System.Console.WriteLine(" form url:[website_url]"); System.Console.WriteLine(" example url:http://localhost:12345"); return; } if (string.IsNullOrWhiteSpace(this.user_name)) { System.Console.WriteLine("missing user_name"); System.Console.WriteLine(" form user_name:[user_name]"); System.Console.WriteLine(" example user_name:user1"); return; } if (string.IsNullOrWhiteSpace(this.password)) { System.Console.WriteLine("missing password"); System.Console.WriteLine(" form password:[password]"); System.Console.WriteLine(" example password:secret"); return; } if (!Directory.Exists(this.source_file_path)) { System.Console.WriteLine($"source_file_path does NOT exist: {this.source_file_path}"); return; } string login_url = this.mmria_url + "/api/session/"; var login_data = new { userid = this.user_name, password = this.password }; var login_curl = new cURL("POST", null, login_url, Newtonsoft.Json.JsonConvert.SerializeObject(login_data)); string login_result_string = null; try { login_result_string = await login_curl.executeAsync(); } catch (Exception ex) { System.Console.WriteLine(ex); } var login_result_data = Newtonsoft.Json.JsonConvert.DeserializeObject <IList <System.Dynamic.ExpandoObject> >(login_result_string); IDictionary <string, object> login_result_dictionary = login_result_data[0] as IDictionary <string, object>; string auth_session = null; //{"ok":true,"userCtx":{"name":null,"roles":[]},"info":{"authentication_db":"_users","authentication_handlers":["oauth","cookie","default"]}} if (!login_result_dictionary.ContainsKey("ok") && login_result_dictionary["ok"].ToString().ToLower() != "true") { System.Console.WriteLine($"Unable to login to {this.mmria_url}:\n{login_result_dictionary}"); return; } auth_session = login_result_dictionary ["auth_session"].ToString(); /*var case_view_url = location.protocol + '//' + location.host + '/api/case_view' + g_ui.case_view_request.get_query_string(); * get_query_string : function(){ * var result = []; * result.push("?skip=" + (this.page - 1) * this.take); * result.push("take=" + this.take); * result.push("sort=" + this.sort); */ string document_url = this.mmria_url + $"/api/case_view?take={int.MaxValue}"; var document_curl = new cURL("GET", null, document_url, null, null, null); document_curl.AddCookie("AuthSession", auth_session); string document_json = null; document_json = document_curl.execute(); //System.IO.File.WriteAllText(this.source_file_path + "test.json",document_json); mmria.common.model.couchdb.case_view_response case_view_response = Newtonsoft.Json.JsonConvert.DeserializeObject <mmria.common.model.couchdb.case_view_response>(document_json); foreach (mmria.common.model.couchdb.case_view_item cvi in case_view_response.rows) { try { var get_case_curl = new cURL("GET", null, this.mmria_url + $"/api/case?case_id={cvi.id}", null, null, null); get_case_curl.AddCookie("AuthSession", auth_session); //get_case_curl.AddCookie("AuthSession", auth_session); document_json = get_case_curl.execute(); System.IO.File.WriteAllText(this.source_file_path + $"/{cvi.id}.json", document_json); } catch (Exception ex) { System.Console.Write(ex); } } /* * foreach (var file_name in System.IO.Directory.GetFiles(this.source_file_path)) * { * * * string global_record_id = null; * * string case_json_string = System.IO.File.ReadAllText(file_name); * * var case_data = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Dynamic.ExpandoObject>(case_json_string); * IDictionary<string, object> case_data_dictionary = case_data as IDictionary<string, object>; * * if (case_data_dictionary.ContainsKey ("_id")) * { * global_record_id = case_data_dictionary ["_id"].ToString(); * } * * * try * { * // check if doc exists * string document_url = this.mmria_url + "/api/case?case_id=" + global_record_id; * var document_curl = new cURL("GET", null, document_url, null, null, null); * document_curl.AddCookie("AuthSession", auth_session); * string document_json = null; * * document_json = document_curl.execute(); * var result_expando = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Dynamic.ExpandoObject>(document_json); * var result = result_expando as IDictionary<string, object>; * if (result != null && result.ContainsKey ("_rev")) * { * case_data_dictionary ["_rev"] = result ["_rev"]; * * * case_json_string = Newtonsoft.Json.JsonConvert.SerializeObject (case_data, new Newtonsoft.Json.JsonSerializerSettings () { * Formatting = Newtonsoft.Json.Formatting.Indented, * DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, * DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc * }); * //System.Console.WriteLine ("json\n{0}", case_json_string); * } * * var update_curl = new cURL ("POST", null, this.mmria_url + "/api/case", case_json_string, null, null); * update_curl.AddCookie("AuthSession", auth_session); * try * { * string update_result_string = update_curl.execute (); * * /* * var update_result_expando = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Dynamic.ExpandoObject>(update_result_string); * var update_result = result_expando as IDictionary<string, object>; * if (update_result != null && update_result.ContainsKey ("_rev")) * { * * case_data_dictionary ["_rev"] = result ["_rev"]; * * * case_json_string = Newtonsoft.Json.JsonConvert.SerializeObject (case_data, new Newtonsoft.Json.JsonSerializerSettings () { * Formatting = Newtonsoft.Json.Formatting.Indented, * DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat, * DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc * }); * System.Console.WriteLine ("json\n{0}", case_json_string); * * } * / * System.Console.WriteLine ($"Succesfully imported id {global_record_id}"); * //System.Console.WriteLine (update_result_string); * * } * catch (Exception ex) * { * System.Console.WriteLine ($"Id {global_record_id}"); * System.Console.WriteLine (ex); * } * * } * catch (Exception ex) * { * System.Console.WriteLine("Get case"); * System.Console.WriteLine(ex); * System.Console.WriteLine("json\n{0}", case_json_string); * * } * } */ Console.WriteLine("Export Finished"); }