ProcessResponse() public static method

public static ProcessResponse ( HTTP responseInfo, string label = null ) : bool
responseInfo TrakHound.Tools.Web.HTTP
label string
return bool
Exemplo n.º 1
0
        public static bool Remove(UserConfiguration userConfig, List <MessageInfo> messageInfos)
        {
            Uri apiHost = ApiConfiguration.AuthenticationApiHost;

            string url = new Uri(apiHost, "messages/remove/index.php").ToString();

            var postDatas = new NameValueCollection();

            postDatas["token"]     = userConfig.SessionToken;
            postDatas["sender_id"] = UserManagement.SenderId.Get();

            if (messageInfos != null)
            {
                string json = JSON.FromList <MessageInfo>(messageInfos);
                if (!string.IsNullOrEmpty(json))
                {
                    postDatas["messages"] = json;
                }
            }

            string response = HTTP.POST(url, postDatas);

            if (response != null)
            {
                return(ApiError.ProcessResponse(response, "Remove Messages"));
            }

            return(false);
        }
Exemplo n.º 2
0
        public static bool Send(UserConfiguration userConfig, List <BugInfo> bugInfos)
        {
            string json = JSON.FromList <BugInfo>(bugInfos);

            if (!string.IsNullOrEmpty(json))
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                string url = new Uri(apiHost, "bugs/send/index.php").ToString();

                var postDatas = new NameValueCollection();
                if (userConfig != null)
                {
                    postDatas["token"]     = userConfig.SessionToken;
                    postDatas["sender_id"] = UserManagement.SenderId.Get();
                }

                postDatas["bugs"] = json;

                string response = HTTP.POST(url, postDatas);
                if (response != null)
                {
                    return(ApiError.ProcessResponse(response, "Send Bug Report"));
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        // Example URL (GET)
        // -----------------------------------------------------

        // ../api/devices/list?token=01234&sender_id=56789                    (List all devices for the user)
        // OR
        // ../api/devices/list?token=01234&sender_id=56789&unique_id=ABCDEFG   (List only the device with the specified 'unique_id')

        // -----------------------------------------------------

        // Example Post Data
        // -----------------------------------------------------

        // name = 'token'
        // value = 01234 (Session Token)

        // name = 'sender_id'
        // value = 56789 (Sender ID)

        // (Optional)
        // name = 'devices'
        // value =  [
        //	{ "unique_id": "ABCDEFG" },
        //  { "unique_id": "HIJKLMN" }
        //	]
        // -----------------------------------------------------

        /// <summary>
        /// List a single DeviceDescription
        /// </summary>
        /// <param name="userConfig">UserConfiguration object for the current user</param>
        /// <param name="deviceUniqueId">The Unique ID of the device to return</param>
        /// <returns></returns>
        public static DeviceDescription List(UserConfiguration userConfig, string deviceUniqueId, Uri apiHost)
        {
            if (!string.IsNullOrEmpty(deviceUniqueId))
            {
                string url = new Uri(apiHost, "devices/list/index.php").ToString();

                if (userConfig != null)
                {
                    string format = "{0}?token={1}&sender_id={2}&unique_id={3}";

                    string token    = userConfig.SessionToken;
                    string senderId = UserManagement.SenderId.Get();

                    url = string.Format(format, url, token, senderId, deviceUniqueId);
                }

                string response = HTTP.GET(url);
                if (response != null)
                {
                    bool success = ApiError.ProcessResponse(response, "List Devices");
                    if (success)
                    {
                        var deviceInfos = JSON.ToType <List <Data.DeviceInfo> >(response);
                        if (deviceInfos != null && deviceInfos.Count > 0)
                        {
                            return(new DeviceDescription(deviceInfos[0]));
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        // Example URL (GET)
        // -----------------------------------------------------

        // ../api/devices/check?token=01234&sender_id=56789                   (Check all devices for the user)
        // OR
        // ../api/devices/check?token=01234&sender_id=56789&unique_id=ABCDEFG   (Check only the device with the specified 'unique_id')

        // -----------------------------------------------------

        // Example Post Data
        // -----------------------------------------------------

        // name = 'token'
        // value = 01234 (Session Token)

        // name = 'sender_id'
        // value = 56789 (Sender ID)

        // (Optional)
        // name = 'devices'
        // value =  [
        //	{ "unique_id": "ABCDEFG" },
        //  { "unique_id": "HIJKLMN" }
        //	]
        // -----------------------------------------------------

        /// <summary>
        /// Check the status (Update Id) of a single Device
        /// </summary>
        /// <param name="userConfig">UserConfiguration object for the current user</param>
        /// <param name="deviceUniqueId">The Unique ID of the device to return</param>
        /// <returns></returns>
        public static CheckInfo Check(UserConfiguration userConfig, string deviceUniqueId)
        {
            if (!string.IsNullOrEmpty(deviceUniqueId))
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                string url = new Uri(apiHost, "devices/check/index.php").ToString();

                string format = "{0}?token={1}&sender_id={2}{3}";

                string token    = userConfig.SessionToken;
                string senderId = UserManagement.SenderId.Get();
                string uniqueId = "unique_id=" + deviceUniqueId;

                url = string.Format(format, url, token, senderId, uniqueId);

                string response = HTTP.GET(url);
                if (response != null)
                {
                    bool success = ApiError.ProcessResponse(response, "Check Device");
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Check the status (Update Id) for a list of devices
        /// </summary>
        /// <param name="userConfig">UserConfiguration object for the current user</param>
        /// <returns></returns>
        public static List <CheckInfo> Check(UserConfiguration userConfig)
        {
            Uri apiHost = ApiConfiguration.AuthenticationApiHost;

            string url = new Uri(apiHost, "devices/check/index.php").ToString();

            string format = "{0}?token={1}&sender_id={2}";

            string token    = userConfig.SessionToken;
            string senderId = UserManagement.SenderId.Get();

            url = string.Format(format, url, token, senderId);

            string response = HTTP.GET(url);

            if (response != null)
            {
                bool success = ApiError.ProcessResponse(response, "Check Devices");
                if (success)
                {
                    if (response == "No Devices Found")
                    {
                        return(null);
                    }
                    else
                    {
                        return(JSON.ToType <List <CheckInfo> >(response));
                    }
                }
            }

            return(null);
        }
Exemplo n.º 6
0
        public static UploadFileInfo[] Upload(UserConfiguration userConfig, HTTP.FileContentData[] fileContentDatas)
        {
            if (userConfig != null)
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                string url = new Uri(apiHost, "files/upload/index.php").ToString();

                var postDatas = new NameValueCollection();
                postDatas["token"]     = userConfig.SessionToken;
                postDatas["sender_id"] = UserManagement.SenderId.Get();

                var httpInfo = new HTTP.HTTPInfo();
                httpInfo.Url         = url;
                httpInfo.ContentData = HTTP.PostContentData.FromNamedValueCollection(postDatas);
                httpInfo.FileData    = fileContentDatas;

                string response = HTTP.POST(httpInfo);
                if (response != null)
                {
                    bool success = ApiError.ProcessResponse(response, "Upload File");
                    if (success)
                    {
                        var uploadFileInfos = JSON.ToType <List <UploadFileInfo> >(response);
                        if (uploadFileInfos != null)
                        {
                            return(uploadFileInfos.ToArray());
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 7
0
        // Example POST Data
        // -----------------------------------------------------

        // name = 'token'
        // value = Session Token

        // name = 'sender_id'
        // value = Sender ID

        // name = 'devices'
        // value =  [{
        //
        //	 "unique_id": "987654321",
        //	 "data": [
        //		{ "address": "/ClientEnabled", "value": "true", "" },
        //		{ "address": "/ServerEnabled", "value": "true", "" },
        //		{ "address": "/UniqueId", "value": "987654321", "" }
        //		]
        //	},
        //	{
        //	 "unique_id": "123456789",
        //	 "data": [
        //		{ "address": "/ClientEnabled", "value": "true", "" },
        //		{ "address": "/ServerEnabled", "value": "true", "" },
        //		{ "address": "/UniqueId", "value": "123456789", "" }
        //		]
        // }]
        // -----------------------------------------------------

        public static bool Update(UserConfiguration userConfig, DeviceConfiguration deviceConfig)
        {
            bool result = false;

            if (userConfig != null)
            {
                var table = deviceConfig.ToTable();
                if (table != null)
                {
                    var infos = new List <DeviceInfo>();
                    infos.Add(new DeviceInfo(deviceConfig.UniqueId, table));

                    string json = JSON.FromObject(infos);
                    if (json != null)
                    {
                        Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                        string url = new Uri(apiHost, "devices/update/index.php").ToString();

                        var postDatas = new NameValueCollection();
                        postDatas["token"]     = userConfig.SessionToken;
                        postDatas["sender_id"] = UserManagement.SenderId.Get();
                        postDatas["devices"]   = json;

                        string response = HTTP.POST(url, postDatas);
                        if (response != null)
                        {
                            result = ApiError.ProcessResponse(response, "Update Devices");
                        }
                    }
                }
            }

            return(result);
        }
Exemplo n.º 8
0
        public static bool Update(UserConfiguration userConfig, List <DeviceInfo> deviceInfos, bool replace = true)
        {
            bool result = false;

            if (userConfig != null)
            {
                string json = JSON.FromObject(deviceInfos);
                if (json != null)
                {
                    Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                    string url = new Uri(apiHost, "devices/update/index.php").ToString();

                    var postDatas = new NameValueCollection();
                    postDatas["token"]     = userConfig.SessionToken;
                    postDatas["sender_id"] = UserManagement.SenderId.Get();
                    postDatas["devices"]   = json;
                    postDatas["replace"]   = replace.ToString();

                    string response = HTTP.POST(url, postDatas);
                    if (response != null)
                    {
                        result = ApiError.ProcessResponse(response, "Update Devices");
                    }
                }
            }

            return(result);
        }
Exemplo n.º 9
0
        public static List <DeviceConfiguration> Get(UserConfiguration userConfig, string[] deviceUniqueIds)
        {
            if (deviceUniqueIds != null && deviceUniqueIds.Length > 0)
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                string url = new Uri(apiHost, "devices/get/index.php").ToString();

                var getDeviceInfos = new List <GetDeviceInfo>();
                foreach (var deviceUniqueId in deviceUniqueIds)
                {
                    getDeviceInfos.Add(new GetDeviceInfo(deviceUniqueId));
                }

                string json = JSON.FromObject(getDeviceInfos);
                if (json != null)
                {
                    var postDatas = new NameValueCollection();
                    postDatas["token"]     = userConfig.SessionToken;
                    postDatas["sender_id"] = UserManagement.SenderId.Get();
                    postDatas["devices"]   = json;

                    string response = HTTP.POST(url, postDatas);
                    if (response != null)
                    {
                        bool success = ApiError.ProcessResponse(response, "Get Devices");
                        if (success)
                        {
                            var deviceInfos = JSON.ToType <List <DeviceInfo> >(response);
                            if (deviceInfos != null)
                            {
                                var deviceConfigs = new List <DeviceConfiguration>();

                                foreach (var deviceInfo in deviceInfos)
                                {
                                    var table = deviceInfo.ToTable();
                                    if (table != null)
                                    {
                                        var xml = DeviceConfiguration.TableToXml(table);
                                        if (xml != null)
                                        {
                                            var deviceConfig = DeviceConfiguration.Read(xml);
                                            if (deviceConfig != null && !string.IsNullOrEmpty(deviceConfig.UniqueId))
                                            {
                                                deviceConfigs.Add(deviceConfig);
                                            }
                                        }
                                    }
                                }

                                return(deviceConfigs);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 10
0
        public static List <DeviceDescription> List(UserConfiguration userConfig, string[] deviceUniqueIds, Uri apiHost)
        {
            if (deviceUniqueIds != null && deviceUniqueIds.Length > 0)
            {
                string url = new Uri(apiHost, "devices/list/index.php").ToString();

                var getDeviceInfos = new List <GetDeviceInfo>();
                foreach (var deviceUniqueId in deviceUniqueIds)
                {
                    getDeviceInfos.Add(new GetDeviceInfo(deviceUniqueId));
                }

                string json = JSON.FromObject(getDeviceInfos);
                if (json != null)
                {
                    var postDatas = new NameValueCollection();

                    if (userConfig != null)
                    {
                        postDatas["token"]     = userConfig.SessionToken;
                        postDatas["sender_id"] = UserManagement.SenderId.Get();
                    }

                    postDatas["devices"] = json;

                    string response = HTTP.POST(url, postDatas);
                    if (response != null)
                    {
                        bool success = ApiError.ProcessResponse(response, "List Devices");
                        if (success)
                        {
                            var deviceInfos = JSON.ToType <List <Data.DeviceInfo> >(response);
                            if (deviceInfos != null)
                            {
                                var devices = new List <DeviceDescription>();

                                foreach (var deviceInfo in deviceInfos)
                                {
                                    devices.Add(new DeviceDescription(deviceInfo));
                                }

                                return(devices);
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Get a list of DeviceConfigurations of all of the user's devices
        /// </summary>
        /// <param name="userConfig">UserConfiguration object for the current user</param>
        /// <returns></returns>
        public static List <DeviceConfiguration> Get(UserConfiguration userConfig)
        {
            Uri apiHost = ApiConfiguration.AuthenticationApiHost;

            string url = new Uri(apiHost, "devices/get/index.php").ToString();

            string format = "{0}?token={1}&sender_id={2}";

            string token    = userConfig.SessionToken;
            string senderId = UserManagement.SenderId.Get();

            url = string.Format(format, url, token, senderId);

            string response = HTTP.GET(url);

            if (response != null)
            {
                bool success = ApiError.ProcessResponse(response, "Get Devices");
                if (success)
                {
                    var deviceInfos = JSON.ToType <List <DeviceInfo> >(response);
                    if (deviceInfos != null)
                    {
                        var deviceConfigs = new List <DeviceConfiguration>();

                        foreach (var deviceInfo in deviceInfos)
                        {
                            var table = deviceInfo.ToTable();
                            if (table != null)
                            {
                                var xml = DeviceConfiguration.TableToXml(table);
                                if (xml != null)
                                {
                                    var deviceConfig = DeviceConfiguration.Read(xml);
                                    if (deviceConfig != null && !string.IsNullOrEmpty(deviceConfig.UniqueId))
                                    {
                                        deviceConfigs.Add(deviceConfig);
                                    }
                                }
                            }
                        }

                        return(deviceConfigs);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 12
0
        public static List <MessageInfo> Get(UserConfiguration userConfig, List <string> messageIds)
        {
            Uri apiHost = ApiConfiguration.AuthenticationApiHost;

            string url = new Uri(apiHost, "messages/get/index.php").ToString();

            string format = "{0}?token={1}&sender_id={2}{3}";

            string token    = userConfig.SessionToken;
            string senderId = UserManagement.SenderId.Get();
            string devices  = "";

            // List Message IDs to Get
            // If no Messages are listed then ALL Messages are retrieved
            if (messageIds != null)
            {
                string json = JSON.FromList <string>(messageIds);
                if (!string.IsNullOrEmpty(json))
                {
                    devices = "messages=" + json;
                }
            }

            url = string.Format(format, url, token, senderId, devices);

            var httpInfo = new HTTP.HTTPInfo();

            httpInfo.Url         = url;
            httpInfo.MaxAttempts = 1;

            string response = HTTP.GET(httpInfo);

            if (response != null)
            {
                bool success = ApiError.ProcessResponse(response, "Get Messages");
                if (success)
                {
                    var messageInfos = JSON.ToType <List <MessageInfo> >(response);
                    if (messageInfos != null)
                    {
                        return(messageInfos);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 13
0
        /// <summary>
        /// Remove all of the user's devices
        /// </summary>
        /// <param name="userConfig">UserConfiguration object for the current user</param>
        /// <returns></returns>
        public static bool Remove(UserConfiguration userConfig)
        {
            if (userConfig != null)
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                string url = new Uri(apiHost, "devices/remove/index.php").ToString();

                var postDatas = new NameValueCollection();
                postDatas["token"]     = userConfig.SessionToken;
                postDatas["sender_id"] = UserManagement.SenderId.Get();

                string response = HTTP.POST(url, postDatas);
                if (response != null)
                {
                    return(ApiError.ProcessResponse(response, "Remove Devices"));
                }
            }

            return(false);
        }
Exemplo n.º 14
0
        /// <summary>
        /// Get a list of DeviceDescriptions of all of the user's devices
        /// </summary>
        /// <param name="userConfig">UserConfiguration object for the current user</param>
        /// <returns></returns>
        public static List <DeviceDescription> List(UserConfiguration userConfig, Uri apiHost)
        {
            string url = new Uri(apiHost, "devices/list/index.php").ToString();

            if (userConfig != null)
            {
                string format = "{0}?token={1}&sender_id={2}";

                string token    = userConfig.SessionToken;
                string senderId = UserManagement.SenderId.Get();

                url = string.Format(format, url, token, senderId);
            }

            string response = HTTP.GET(url);

            if (response != null)
            {
                bool success = ApiError.ProcessResponse(response, "List Devices");
                if (success)
                {
                    var deviceInfos = JSON.ToType <List <Data.DeviceInfo> >(response);
                    if (deviceInfos != null)
                    {
                        var devices = new List <DeviceDescription>();

                        foreach (var deviceInfo in deviceInfos)
                        {
                            devices.Add(new DeviceDescription(deviceInfo));
                        }

                        return(devices);
                    }
                }
            }

            return(null);
        }
Exemplo n.º 15
0
        public static bool Update(UserConfiguration userConfig, List <DeviceInfo> deviceInfos)
        {
            var json = Data.DeviceInfo.ListToJson(deviceInfos);

            if (!string.IsNullOrEmpty(json))
            {
                Uri apiHost = ApiConfiguration.DataApiHost;

                string url = new Uri(apiHost, "data/update/index.php").ToString();

                var postDatas = new NameValueCollection();
                postDatas["token"]     = userConfig.SessionToken;
                postDatas["sender_id"] = UserManagement.SenderId.Get();
                postDatas["devices"]   = json;

                string response = HTTP.POST(url, postDatas);
                if (response != null)
                {
                    return(ApiError.ProcessResponse(response, "Update Device Data"));
                }
            }

            return(false);
        }
Exemplo n.º 16
0
        public static Image DownloadImagev1(UserConfiguration userConfig, string fileId, bool useCache)
        {
            Image result = null;

            if (useCache)
            {
                if (cachedImages == null)
                {
                    LoadCachedImages();
                }

                var cachedImage = cachedImages.Find(o => o.Id == fileId);
                if (cachedImage != null)
                {
                    result = cachedImage.Image;
                }
            }

            if (result == null && userConfig != null)
            {
                Uri apiHost = ApiConfiguration.AuthenticationApiHost;

                string url = new Uri(apiHost, "files/download/index.php").ToString();

                string format = "{0}?token={1}&sender_id={2}&file_id={3}";

                string token    = userConfig.SessionToken;
                string senderId = UserManagement.SenderId.Get();

                url = string.Format(format, url, token, senderId, fileId);

                var response = HTTP.GET(url, true);
                if (response != null && response.Body != null && response.Headers != null)
                {
                    bool success = false;

                    string dummy = System.Text.Encoding.ASCII.GetString(response.Body);

                    // Takes forever to process an image
                    if (response.Body.Length < 500)
                    {
                        success = ApiError.ProcessResponse(response, "Download File");
                    }
                    else
                    {
                        success = true;
                    }

                    if (success)
                    {
                        try
                        {
                            using (var memStream = new MemoryStream())
                            {
                                memStream.Write(response.Body, 0, response.Body.Length);
                                result = Image.FromStream(memStream);
                                Logger.Log("Download File Successful", LogLineType.Notification);
                            }
                        }
                        catch (Exception ex) { Logger.Log("Response Not an Image : Exception : " + ex.Message); }
                    }

                    // Add Image to Cache
                    if (useCache && result != null)
                    {
                        AddImageToCache(new CachedImage(fileId, result));
                    }
                }
            }

            return(result);
        }
Exemplo n.º 17
0
        public static List <DeviceInfo> Get(UserConfiguration userConfig, List <string> uniqueIds, DateTime from, DateTime to, int timeout, string command)
        {
            Uri apiHost = ApiConfiguration.DataApiHost;

            string url = new Uri(apiHost, "data/get/index.php").ToString();

            string times = "&from=" + from.ToString("o") + "&to=" + to.ToString("o");

            string devices = "";

            // List Devices to Get data for
            // If no devices are listed then ALL devices are retrieved
            if (uniqueIds != null && uniqueIds.Count > 0)
            {
                var deviceItems = new List <DeviceListItem>();

                foreach (var uniqueId in uniqueIds)
                {
                    deviceItems.Add(new DeviceListItem(uniqueId));
                }

                string json = JSON.FromList <DeviceListItem>(deviceItems);
                if (!string.IsNullOrEmpty(json))
                {
                    devices = "&devices=" + json;
                }
            }

            string cmd = "";

            if (command != null)
            {
                cmd = "&command=" + command;
            }

            // Create GET request parameters
            if (userConfig != null)
            {
                string format = "{0}?token={1}&sender_id={2}{3}{4}{5}";

                string token    = userConfig.SessionToken;
                string senderId = UserManagement.SenderId.Get();

                url = string.Format(format, url, token, senderId, devices, cmd, times);
            }
            else
            {
                string format = "{0}?{1}{2}{3}";
                url = string.Format(format, url, devices, cmd, times);
            }

            // Setup HTTP Information for GET request
            var httpInfo = new HTTP.HTTPInfo();

            httpInfo.Url         = url;
            httpInfo.Timeout     = timeout;
            httpInfo.MaxAttempts = 1;


            string response = HTTP.GET(httpInfo);

            if (response != null)
            {
                bool success = ApiError.ProcessResponse(response, "Get Device Data");
                if (success)
                {
                    var deviceInfos = JSON.ToType <List <DeviceInfo> >(response);
                    if (deviceInfos != null)
                    {
                        return(deviceInfos);
                    }
                }
            }

            return(null);
        }