Exemple #1
0
        /// <summary>
        /// Sends an HTTP Request to the backend and parse the response (expected as a list of BaseEntity objects)
        /// </summary>
        /// <param name="action">action to query (e.g. "Snippets/Get")</param>
        /// <param name="data">
        ///     if the request is a GET, the parameters to be put in the querystring (e.g. "snippetID=100"),
        ///     otherwise data to be put in the post (e.g. "content=...")
        /// </param>
        /// <param name="isPost">whether this request is a GET(false) or a POST(true)</param>
        /// <param name="requiresLogin">false if this request calls a public web service</param>
        /// <returns>null if any error occurred; the result of the invocation otherwise</returns>
        protected S2CResListBaseEntity <T> SendReqListBaseEntity <T>(string action, string data, bool isPost, bool requiresLogin = true)
            where T : BaseEntity, new()
        {
            string response = PrepareAndSendReq(action, data, isPost, requiresLogin);

            if (string.IsNullOrEmpty(response))
            {
                ErrorCodes errCode = ErrorCodes.COMMUNICATION_ERROR;
                if (WebConnector.Current.IsTimeout)
                {
                    errCode = ErrorCodes.TIMEOUT;
                }
                SetLastError(log, errCode, S2CRes <string> .GetErrorMsg(errCode));
                return(new S2CResListBaseEntity <T>(0.0, errCode, null, 0, null));
            }

            S2CResListBaseEntity <T> resp = S2CSerializer.DeserializeBaseEntityList <T>(response, m_serialFormat);

            if (!CheckResp <T>(resp))
            {
                PrintRespError <T>(resp);

                //if the problem is related to user not logged in, reset login status and retry another time:
                if (requiresLogin && (resp != null) && (resp.Status == ErrorCodes.NOT_LOGGED_IN))
                {
                    WebConnector.Current.ResetLoginStatus(); //reset login status

                    //retry the WS call:
                    response = PrepareAndSendReq(action, data, isPost, requiresLogin);
                    resp     = S2CSerializer.DeserializeBaseEntityList <T>(response, m_serialFormat);
                }
            }

            return(resp);
        }
Exemple #2
0
        public IList <int> GetChannelIDsOfUser(int userID)
        {
            if (userID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: userID={0}", userID));
                return(new List <int>());
            }

            //send the request and parse the response:
            string querystring = string.Format("ID={0}", userID);
            S2CResListBaseEntity <GroupComm> resp = SendReqListBaseEntity <GroupComm>(GET_CHANNELIDS_URL, querystring, false);

            //build the result:
            int          totNum = 0;
            List <Group> g      = resp.GetListFromResp <GroupComm, Group>(out totNum);

            if (g != null)
            {
                return(g.Select((group) => group.ID) as IList <int>);
            }
            else
            {
                return(new List <int>());
            }
        }
Exemple #3
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////


        #region Get Methods
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        public IList <Snippet> GetSnippetsForSearch(string searchText, out Dictionary <string, string[]> misSpellings, out int totNum,
                                                    out int totNumWithNonDefOp, out SortedDictionary <string, int> tagsOccurrences, int maxNum = int.MaxValue, int start = 0, bool onlyCreated = false, int onlyOfGroup = 0,
                                                    ItemSortField field = ItemSortField.Relevance, SortDirection direction = SortDirection.Descent)
        {
            //check for erroneous input:
            misSpellings       = null;
            totNum             = 0;
            totNumWithNonDefOp = 0;
            tagsOccurrences    = null;
            if ((maxNum <= 0) || (start < 0))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: userID={0}, searchText={1}, maxNum={2}, start={3}",
                                                                        CurrentUserID, searchText.PrintNull(), maxNum, start));
                return(new List <Snippet>());
            }

            //send the request and parse the response:
            string querystring = string.Format("query={0}&maxNum={1}&start={2}&onlyCreated={3}&onlyOfGroup={4}&field={5}&direction={6}",
                                               HttpUtility.UrlEncode(searchText), maxNum, start, onlyCreated, onlyOfGroup, field, direction);
            S2CResListBaseEntity <SnippetComm> resp = SendReqListBaseEntity <SnippetComm>(SEARCH_SNIPPET_URL, querystring, false, false);

            //build the result:
            IList <Snippet> snips = resp.GetListFromResp <SnippetComm, Snippet>(out totNum);

            misSpellings = resp.GetMiscFromResp <SnippetComm>(out totNumWithNonDefOp);
            return(snips);
        }
Exemple #4
0
        public IList <Snippet> FindGroupItems(int start, int count, int[] groupIDs, out int totNum,
                                              ItemSortField field = ItemSortField.Relevance, SortDirection direction = SortDirection.Descent)
        {
            //check for erroneous input:
            totNum = 0;
            if ((count <= 0) || (start < 0) || (groupIDs == null) || (groupIDs.Length == 0))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: userID={0}, count={1}, start={2}, groupIDs={3}",
                                                                        CurrentUserID, count, start, groupIDs.Print()));
                return(new List <Snippet>());
            }

            //send the request and parse the response:
            string content = Utilities.MergeIntoCommaSeparatedString(groupIDs, true, ' ');

            if (string.IsNullOrEmpty(content))
            {
                return(new List <Snippet>());
            }
            string querystring = string.Format("content={0}&start={1}&count={2}&field={3}&direction={4}",
                                               HttpUtility.UrlEncode(content), start, count, field, direction);
            S2CResListBaseEntity <SnippetComm> resp = SendReqListBaseEntity <SnippetComm>(FINDGROUPS_SNIPPETS_URL, querystring, true, false);

            //build the result:
            IList <Snippet> snips = resp.GetListFromResp <SnippetComm, Snippet>(out totNum);

            return(snips);
        }
Exemple #5
0
        public ICollection <Tag> GetTags(long snippetID)
        {
            //check for erroneous input:
            if (snippetID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}", snippetID));
                return(new List <Tag>());
            }

            //send the request and parse the response:
            S2CResListBaseEntity <Tag> resp = SendReqListBaseEntity <Tag>(GET_TAGS_URL, "id=" + snippetID, false, false);

            //build the result:
            return(ParseListBaseEntityResponse <Tag>(resp));
        }
Exemple #6
0
        public List <Badge> GetBadges(int userID)
        {
            if (userID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: userID={0}", userID));
                return(new List <Badge>());
            }

            //send the request and parse the response:
            string querystring = string.Format("ID={0}", userID);
            S2CResListBaseEntity <Badge> resp = SendReqListBaseEntity <Badge>(GET_BADGES_URL, querystring, false);

            //build the result:
            int totNum = 0;

            return(resp.GetListFromResp <Badge, Badge>(out totNum));
        }
Exemple #7
0
        public IList <Group> GetAdministeredChannelsOfUser(int userID)
        {
            if (userID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: userID={0}", userID));
                return(new List <Group>());
            }

            //send the request and parse the response:
            string querystring = string.Format("ID={0}", userID);
            S2CResListBaseEntity <GroupComm> resp = SendReqListBaseEntity <GroupComm>(GET_ADMINISTEREDCHANNELS_URL, querystring, false);

            //build the result:
            int totNum = 0;

            return(resp.GetListFromResp <GroupComm, Group>(out totNum));
        }
Exemple #8
0
        public ICollection <Snippet> GetMostCorrelatedSnippets(long snippetID)
        {
            //check for erroneous input:
            if (snippetID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}", snippetID));
                return(new List <Snippet>());
            }

            //send the request and parse the response:
            S2CResListBaseEntity <SnippetComm> resp = SendReqListBaseEntity <SnippetComm>(GET_MOSTCORRELATED_URL, "id=" + snippetID, false, false);

            //build the result:
            int             totNum = 0;
            IList <Snippet> snips  = resp.GetListFromResp <SnippetComm, Snippet>(out totNum);

            return(snips);
        }
Exemple #9
0
        public List <Badge> GetBadges(long snippetID)
        {
            //check for erroneous input:
            if (snippetID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: snippetID={0}", snippetID));
                return(new List <Badge>());
            }

            //send the request and parse the response:
            S2CResListBaseEntity <Badge> resp = SendReqListBaseEntity <Badge>(GET_BADGES_URL, "id=" + snippetID, false, false);

            //build the result:
            int          totNum = 0;
            List <Badge> badges = resp.GetListFromResp <Badge, Badge>(out totNum);

            return(badges);
        }
Exemple #10
0
        public ICollection <Group> GetGroupsOfUser(int userID)
        {
            if (userID <= 0)
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT, string.Format("Input error: userID={0}", userID));
                return(new List <Group>());
            }

            //send the request and parse the response:
            string querystring = string.Format("ID={0}", userID);
            S2CResListBaseEntity <GroupComm> resp = SendReqListBaseEntity <GroupComm>(GET_GROUPS_URL, querystring, false);

            //build the result:
            int          totNum = 0;
            List <Group> g      = resp.GetListFromResp <GroupComm, Group>(out totNum);

            return(g);
        }
Exemple #11
0
        /// <summary>
        /// Checks the given response and returns the expected collection of BaseEntity objects
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="resp"></param>
        /// <returns>an empty list of Base Entity objects if any error occurred, the expected collection otherwise</returns>
        protected ICollection <T> ParseListBaseEntityResponse <T>(S2CResListBaseEntity <T> resp)
            where T : BaseEntity
        {
            ICollection <T> result = new List <T>();

            if (!CheckResp(resp) || (resp.Data == null))
            {
                PrintRespError(resp);
                return(result);
            }

            if (resp.Data != null)
            {
                foreach (BaseEntity be in resp.Data)
                {
                    result.Add((T)be.ToBaseEntity());
                }
            }
            return(result);
        }
Exemple #12
0
        public IList <Snippet> FindGroupItems(int start, int count, int groupID, out int totNum)
        {
            //check for erroneous input:
            totNum = 0;
            if ((count <= 0) || (start < 0))
            {
                SetLastError(log, ErrorCodes.WRONG_INPUT,
                             string.Format("Input error: userID={0}, count={1}, start={2}", CurrentUserID, count, start));
                return(new List <Snippet>());
            }

            //send the request and parse the response:
            string querystring = string.Format("groupID={0}&start={1}&count={2}", groupID, start, count);
            S2CResListBaseEntity <SnippetComm> resp = SendReqListBaseEntity <SnippetComm>(FINDGROUP_SNIPPETS_URL, querystring, false, false);

            //build the result:
            IList <Snippet> snips = resp.GetListFromResp <SnippetComm, Snippet>(out totNum);

            return(snips);
        }
Exemple #13
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////


        #region Get Methods
        /////////////////////////////////////////////////////////////////////////////////////////////////////

        public ICollection <DefaultProperty> GetDefaultProperties()
        {
            if (s_defProperties != null)
            {
                return(s_defProperties);
            }

            //send the request and parse the response:
            string querystring = string.Empty;
            S2CResListBaseEntity <DefaultProperty> resp = SendReqListBaseEntity <DefaultProperty>(GET_DEFAULTS_URL, querystring, false);

            log.DebugFormat("GetDefaultProperties: resp in {0} ms", resp.ExecTime);

            //build the result:
            if (!CheckResp(resp))
            {
                PrintRespError(resp);
                return(new List <DefaultProperty>());
            }

            s_defProperties = resp.Data; // resp.GetListFromResp<DefaultProperty>();
            return(s_defProperties);
        }
Exemple #14
0
        public IList <Snippet> FindItems(long[] snippetIDs, bool keepOrdering = false)
        {
            //check for erroneous input:
            if ((snippetIDs == null) || (snippetIDs.Length == 0))
            {
                return(new List <Snippet>());
            }

            //send the request and parse the response:
            string content = Utilities.MergeIntoCommaSeparatedString(snippetIDs, true, ' ');

            if (string.IsNullOrEmpty(content))
            {
                return(new List <Snippet>());
            }
            string querystring = string.Format("content={0}", HttpUtility.UrlEncode(content));
            S2CResListBaseEntity <SnippetComm> resp = SendReqListBaseEntity <SnippetComm>(FIND_SNIPPETS_URL, querystring, true, false);

            //build the result:
            int totNum = 0;

            return(resp.GetListFromResp <SnippetComm, Snippet>(out totNum));
        }