FreeMoney.BitcoinNotificationService.InitializeAgent C# (CSharp) Method

InitializeAgent() private method

private InitializeAgent ( int num_confirmations_required, string pingback_url ) : bool
num_confirmations_required int
pingback_url string
return bool
        private bool InitializeAgent(int num_confirmations_required, string pingback_url)
        {
            m_log.Info("[FreeMoney] Initializing notification agent.");

            if (m_config["bitcoin_ping_service_1_agent_id"] != "") {
                m_agent_id = m_config["bitcoin_ping_service_1_agent_id"];
            }

            string agent_name = AgentName(num_confirmations_required);
            //Console.WriteLine("Made agent with name" + agent_name);

            string url = m_config["bitcoin_ping_service_1_base_url"];
            if (url == "") {
                //print "Could not make URL";
                return false;
            }

            HttpWebRequest httpWebRequest=(HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Headers.Add("Authorization: "+ m_config["bitcoin_ping_service_1_accesskey"]);
            //httpWebRequest.Method = "POST";
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse ();

            if (httpWebResponse.StatusCode != HttpStatusCode.OK) {
                return false;
            }

            string response;
            using (StreamReader streamReader = new StreamReader (httpWebResponse.GetResponseStream ())) {
                response = streamReader.ReadToEnd ();
                streamReader.Close ();
            }

            try {

                //Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(post_body);
                //JObject jo = JObject.Parse(response);
                JArray agents = JArray.Parse(response);
                //JArray agents = (JArray)jo["signed_data"];

                foreach (JToken agent in agents )
                {
                    if ((string)agent["name"] == agent_name) {
                        m_agent_id = agent["id"].ToString();
                        m_log.Info("[FreeMoney] Got agent "+agent_name+", set agent id to "+m_agent_id);
                        //"urlnotification_set"
                        JArray notifications = (JArray)agent["urlnotification_set"];
                        if (notifications.Count == 0) {
                            m_log.Info("[FreeMoney] No URLs set, creating for "+agent_name);
                            return SetAgentCallback(num_confirmations_required, pingback_url);
                        }
                        m_log.Info("[FreeMoney] URL set OK, InitializeAgent completed.");
                        return true;
                    }

                }

            } catch (Exception e) {
                //Console.WriteLine("Parsing failed in agent initialization");
                m_log.Info("[FreeMoney] Could not understand response when trying to initialize agent.");
                //Console.WriteLine(e.ToString());
                //Console.WriteLine(response);
                return false;
            }

            return ( CreateAgent(agent_name) && SetAgentCallback(num_confirmations_required, pingback_url) );
        }