Ejemplo n.º 1
0
        /**************************************************************************************************************************/

        /// <summary>
        ///     <para>gets the search result</para>
        /// </summary>
        /// <param name="Parameters"></param>
        /// <returns></returns>
        public UserSearch GetUserSearchResult(GetUserSearchResultParameters Parameters)
        {
            UserSearch SearchResult = null;

            try
            {
                // SET UP REQUEST URI
                UriBuilder BaseUri = new UriBuilder();

                BaseUri.Scheme = Config.GetUriScheme();
                BaseUri.Host   = Config.GetApiUriString();
                BaseUri.Host  += "/users/search";

                // SET UP QUERY STRING
                NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty);
                QueryString.Add("access_token", AuthorisedUser.AccessToken);
                QueryString.Add("q", Parameters.Query);
                QueryString.Add("count", Parameters.Count.ToString());

                // SET THE QUERY STRING
                BaseUri.Query = QueryString.ToString();

                // SEND REQUEST
                WebClient Client       = new WebClient();
                byte[]    ResponseData = Client.DownloadData(BaseUri.Uri);
                String    Response     = Encoding.UTF8.GetString(ResponseData);

                // PARSE JSON
                dynamic ParsedJson = JsonConvert.DeserializeObject(Response);

                // CREATE SEARCH RESULT OBJECT
                SearchResult = new UserSearch();

                // CREATE META OBJECT
                MetaData Meta = new MetaData();
                Meta.Code         = ParsedJson.meta.code;
                SearchResult.Meta = Meta;

                // SET DATA FIELD IN SEARCHRESULT
                List <User> SearchedUsers = new List <User>();
                foreach (dynamic EachUser in ParsedJson.data)
                {
                    // CREATE NEW USER OBJECT
                    User EUser = new User();
                    EUser.UserName       = EachUser.username;
                    EUser.Bio            = EachUser.bio;
                    EUser.Website        = EachUser.website;
                    EUser.ProfilePicture = EachUser.profile_picture;
                    EUser.FullName       = EachUser.full_name;
                    EUser.Id             = EachUser.id;

                    // ADD EUSER TO THE LIST
                    SearchedUsers.Add(EUser);
                }
                SearchResult.Data = SearchedUsers;
            }
            catch (WebException WEx)
            {
                // FETCHES ANY ERROR THROWN BY INSTAGRAM API
                Stream ResponseStream = WEx.Response.GetResponseStream();
                if (ResponseStream != null)
                {
                    StreamReader ResponseReader = new StreamReader(ResponseStream);
                    if (ResponseReader != null)
                    {
                        // PARSE JSON
                        dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd());

                        // CREATE NEW META OBJECT AND FILL IN DATA
                        MetaData Meta = new MetaData();
                        Meta.Code         = ParsedJson.meta.code;
                        Meta.ErrorType    = ParsedJson.meta.error_type;
                        Meta.ErrorMessage = ParsedJson.meta.error_message;
                        SearchResult.Meta = Meta;
                    }
                }
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.StackTrace);
            }

            return(SearchResult);
        }
Ejemplo n.º 2
0
        /**************************************************************************************************************************/
        /// <summary>
        ///     <para>gets the search result</para>
        /// </summary>
        /// <param name="Parameters"></param>
        /// <returns></returns>
        public UserSearch GetUserSearchResult(GetUserSearchResultParameters Parameters)
        {
            UserSearch SearchResult = null;

            try
            {
                // SET UP REQUEST URI
                UriBuilder BaseUri = new UriBuilder();

                BaseUri.Scheme = Config.GetUriScheme();
                BaseUri.Host = Config.GetApiUriString();
                BaseUri.Host += "/users/search";

                // SET UP QUERY STRING
                NameValueCollection QueryString = System.Web.HttpUtility.ParseQueryString(String.Empty);
                QueryString.Add("access_token", AuthorisedUser.AccessToken);
                QueryString.Add("q", Parameters.Query);
                QueryString.Add("count", Parameters.Count.ToString());

                // SET THE QUERY STRING
                BaseUri.Query = QueryString.ToString();

                // SEND REQUEST
                WebClient Client = new WebClient();
                byte[] ResponseData = Client.DownloadData(BaseUri.Uri);
                String Response = Encoding.UTF8.GetString(ResponseData);

                // PARSE JSON
                dynamic ParsedJson = JsonConvert.DeserializeObject(Response);

                // CREATE SEARCH RESULT OBJECT
                SearchResult = new UserSearch();

                // CREATE META OBJECT
                MetaData Meta = new MetaData();
                Meta.Code = ParsedJson.meta.code;
                SearchResult.Meta = Meta;

                // SET DATA FIELD IN SEARCHRESULT
                List<User> SearchedUsers = new List<User>();
                foreach (dynamic EachUser in ParsedJson.data)
                {
                    // CREATE NEW USER OBJECT
                    User EUser = new User();
                    EUser.UserName = EachUser.username;
                    EUser.Bio = EachUser.bio;
                    EUser.Website = EachUser.website;
                    EUser.ProfilePicture = EachUser.profile_picture;
                    EUser.FullName = EachUser.full_name;
                    EUser.Id = EachUser.id;

                    // ADD EUSER TO THE LIST
                    SearchedUsers.Add(EUser);
                }
                SearchResult.Data = SearchedUsers;
            }
            catch (WebException WEx)
            {
                // FETCHES ANY ERROR THROWN BY INSTAGRAM API
                Stream ResponseStream = WEx.Response.GetResponseStream();
                if (ResponseStream != null)
                {
                    StreamReader ResponseReader = new StreamReader(ResponseStream);
                    if (ResponseReader != null)
                    {
                        // PARSE JSON
                        dynamic ParsedJson = JsonConvert.DeserializeObject(ResponseReader.ReadToEnd());

                        // CREATE NEW META OBJECT AND FILL IN DATA
                        MetaData Meta = new MetaData();
                        Meta.Code = ParsedJson.meta.code;
                        Meta.ErrorType = ParsedJson.meta.error_type;
                        Meta.ErrorMessage = ParsedJson.meta.error_message;
                        SearchResult.Meta = Meta;
                    }
                }
            }
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.StackTrace);
            }

            return SearchResult;
        }