Пример #1
0
        /// <summary>
        /// Searches LinkedIn using specified options
        /// </summary>
        /// <param name="options">The object of type <see cref="LinkedInSearchOptions"/> representing search options</param>
        /// <returns>Value containing <see cref="LinkedInSearchResult"/> object and response status</returns>
        /// <exception cref="LinkedInNegativeParameterException">Thrown when Start or Count parameters of options are negative</exception>
        /// <exception cref="LinkedInCountIsZeroException">Thrown when Count parameter of options is equal to 0</exception>
        /// <remarks>For better understanding of LinkedIn People Search API, please visit <a href="https://developer.linkedin.com/documents/people-search-api">LinkedIn developers page</a></remarks>
        /// <example>
        /// This sample shows how to call this method:
        /// <code>
        /// using LinkedIn.NET;
        /// using LinkedIn.NET.Groups;
        /// using LinkedIn.NET.Members;
        /// using LinkedIn.NET.Options;
        /// using LinkedIn.NET.Search;
        /// using LinkedIn.NET.Updates;
        /// ... 
        /// // define search options
        /// var options = new LinkedInSearchOptions();
        /// // set various options parameters
        /// ...
        /// var response = _Client.Search(options);
        /// 
        /// // always check response.Result and response.Status before processing 
        /// if (response.Result != null &amp;&amp; response.Status == LinkedInResponseStatus.OK)
        /// {
        ///     // do something with response.Result.People and/or response.Result.Facets
        /// }
        /// else
        /// {
        ///     // show possible error message LinkedIn response
        ///     MessageBox.Show(response.Message);
        /// }
        /// </code>
        /// </example>
        /// <seealso cref="LinkedInSearchOptions"/>
        public LinkedInResponse<LinkedInSearchResult> Search(LinkedInSearchOptions options)
        {
            if (options.Start.HasValue && options.Start < 0)
                throw new LinkedInNegativeParameterException(
                    "Start parameter of query string cannot be less than zero", "Start");
            if (options.Count.HasValue && options.Count < 0)
                throw new LinkedInNegativeParameterException(
                    "Count parameter of query string cannot be less than zero", "Count");
            if (options.Count.HasValue && options.Count == 0)
                throw new LinkedInCountIsZeroException("Count parameter of query string cannot be 0");

            return RequestRunner.GetSearchResult(options);
        }
Пример #2
0
        /// <summary>
        /// Asynchronously searches LinkedIn using specified options
        /// </summary>
        /// <param name="options">The object of type <see cref="LinkedInSearchOptions"/> representing search options</param>
        /// <param name="action">Action to be invoked when the search process ends</param>
        /// <returns>Status of asynchronous operation</returns>
        /// <exception cref="LinkedInNegativeParameterException">Thrown when Start or Count parameters of options are negative</exception>
        /// <exception cref="LinkedInCountIsZeroException">Thrown when Count parameter of options is equal to 0</exception>
        /// <remarks>For better understanding of LinkedIn People Search API, please visit <a href="https://developer.linkedin.com/documents/people-search-api">LinkedIn developers page</a></remarks>
        /// <example>
        /// This sample shows how to call this method:
        /// <code>
        /// using LinkedIn.NET;
        /// using LinkedIn.NET.Groups;
        /// using LinkedIn.NET.Members;
        /// using LinkedIn.NET.Options;
        /// using LinkedIn.NET.Search;
        /// using LinkedIn.NET.Updates;
        /// ... 
        /// // define search options
        /// var options = new LinkedInSearchOptions();
        /// // set various options parameters
        /// ...
        /// _Client.Search(options, getSearchResult);
        /// ...
        /// // application defined function
        /// private void getSearchResult(LinkedInResponse&lt;LinkedInSearchResult&gt; response)
        /// {
        ///     // always check response.Result and response.Status before processing 
        ///     if (response.Result != null &amp;&amp; response.Status == LinkedInResponseStatus.OK)
        ///     {
        ///         // do something with response.Result.People and/or response.Result.Facets
        ///     }
        ///     else
        ///     {
        ///         // show possible error message LinkedIn response
        ///         MessageBox.Show(response.Message);
        ///     }
        /// }
        /// </code>
        /// </example>
        /// <seealso cref="LinkedInSearchOptions"/>
        public IAsyncResult Search(LinkedInSearchOptions options, Action<LinkedInResponse<LinkedInSearchResult>> action)
        {
            if (options.Start.HasValue && options.Start < 0)
                throw new LinkedInNegativeParameterException(
                    "Start parameter of query string cannot be less than zero", "Start");
            if (options.Count.HasValue && options.Count < 0)
                throw new LinkedInNegativeParameterException(
                    "Count parameter of query string cannot be less than zero", "Count");
            if (options.Count.HasValue && options.Count == 0)
                throw new LinkedInCountIsZeroException("Count parameter of query string cannot be 0");

            SearchDelegate _delegate = Search;
            return _delegate.BeginInvoke(options, searchCallback, action);
        }
Пример #3
0
        internal static LinkedInResponse<LinkedInSearchResult> GetSearchResult(LinkedInSearchOptions options)
        {
            try
            {
                var result = new LinkedInSearchResult();

                //if (options.MemberFieldOptions.HasValues &&
                //    (options.SearchSets & LinkedInSearchSets.People) != LinkedInSearchSets.People)
                //    options.SearchSets |= LinkedInSearchSets.People;
                if ((options.FacetFields.HasValues | options.BucketFields.HasValues) &&
                    (options.SearchSets & LinkedInSearchSets.Facets) != LinkedInSearchSets.Facets)
                    options.SearchSets |= LinkedInSearchSets.Facets;
                if (options.FacetLocationValues.Count > 0 && (options.FacetTypes & LinkedInFacetTypes.Location) != LinkedInFacetTypes.Location)
                    options.FacetTypes |= LinkedInFacetTypes.Location;
                if (options.FacetIndustryValues.Count > 0 && (options.FacetTypes & LinkedInFacetTypes.Industry) != LinkedInFacetTypes.Industry)
                    options.FacetTypes |= LinkedInFacetTypes.Industry;
                if (options.FacetLanguageValues.Count > 0 && (options.FacetTypes & LinkedInFacetTypes.Language) != LinkedInFacetTypes.Language)
                    options.FacetTypes |= LinkedInFacetTypes.Language;
                if (options.FacetCurrentCompanyValues.Count > 0 && (options.FacetTypes & LinkedInFacetTypes.CurrentCompany) != LinkedInFacetTypes.CurrentCompany)
                    options.FacetTypes |= LinkedInFacetTypes.CurrentCompany;
                if (options.FacetPastCompanyValues.Count > 0 && (options.FacetTypes & LinkedInFacetTypes.PastCompany) != LinkedInFacetTypes.PastCompany)
                    options.FacetTypes |= LinkedInFacetTypes.PastCompany;
                if (options.FacetSchoolValues.Count > 0 && (options.FacetTypes & LinkedInFacetTypes.School) != LinkedInFacetTypes.School)
                    options.FacetTypes |= LinkedInFacetTypes.School;
                if (options.FacetNetworkValues != LinkedInFacetNetwork.None && (options.FacetTypes & LinkedInFacetTypes.Network) != LinkedInFacetTypes.Network)
                    options.FacetTypes |= LinkedInFacetTypes.Network;

                var fieldsParams = RequestFields.PrepareMemberFields(options.MemberFieldOptions);
                var searchParams = RequestFields.PrepareSearchParams(options);
                var bucketsParams = RequestFields.PrepareBucketsParams(options);
                var facetsParams = RequestFields.PrepareFacetsParams(options);

                var sb = new StringBuilder(Utils.PEOPLE_SEARCH_URL);

                if (options.SearchSets != LinkedInSearchSets.None)
                    sb.Append(":(");

                if ((options.SearchSets & LinkedInSearchSets.People) == LinkedInSearchSets.People)
                {
                    sb.Append("people");
                    if (!string.IsNullOrEmpty(fieldsParams))
                        sb.Append(fieldsParams);
                    sb.Append(",");
                }

                if ((options.SearchSets & LinkedInSearchSets.Facets) == LinkedInSearchSets.Facets)
                {
                    sb.Append("facets");
                    if (!string.IsNullOrEmpty(bucketsParams))
                        sb.Append(bucketsParams);
                    sb.Append(",");
                }

                if (sb.Length > Utils.PEOPLE_SEARCH_URL.Length)
                    sb.Length -= 1;

                if (options.SearchSets != LinkedInSearchSets.None)
                    sb.Append(")");

                sb.Append("?");

                if (!string.IsNullOrEmpty(searchParams))
                    sb.Append(searchParams);

                if (!string.IsNullOrEmpty(facetsParams))
                    sb.Append(facetsParams);

                switch (options.Sort)
                {
                    case LinkedinSearchResultsOrder.Distance:
                        sb.Append("sort=distance&");
                        break;
                    case LinkedinSearchResultsOrder.Recommenders:
                        sb.Append("sort=recommenders&");
                        break;
                    case LinkedinSearchResultsOrder.Relevance:
                        sb.Append("sort=relevance&");
                        break;
                }

                if (options.Start.HasValue)
                {
                    sb.Append("start=");
                    sb.Append(options.Start.Value);
                    sb.Append("&");
                }
                if (options.Count.HasValue)
                {
                    sb.Append("count=");
                    sb.Append(options.Count.Value);
                    sb.Append("&");
                }
                sb.Append("oauth2_access_token=");
                sb.Append(Singleton.Instance.AccessToken);

                var responseString = Utils.MakeRequest(sb.ToString(), "GET");
                var xdoc = XDocument.Parse(responseString);
                var xroot = xdoc.Root;
                if (xroot == null)
                    return new LinkedInResponse<LinkedInSearchResult>(result, LinkedInResponseStatus.OK, null);

                if ((options.SearchSets & LinkedInSearchSets.Facets) == LinkedInSearchSets.Facets)
                {
                    var xp = xroot.Element("facets");
                    if (xp != null)
                        result.AddFacets(xp.Elements("facet").Select(Utils.BuildFacet));
                }
                if ((options.SearchSets & LinkedInSearchSets.People) != LinkedInSearchSets.People)
                    return new LinkedInResponse<LinkedInSearchResult>(result, LinkedInResponseStatus.OK, null);
                var xm = xroot.Element("people");
                if (xm == null)
                    return new LinkedInResponse<LinkedInSearchResult>(result, LinkedInResponseStatus.OK, null);
                var xpeople = xm.Elements("person").ToArray();
                result.AddPeople(xpeople.Select(Utils.BuildMember));
                return new LinkedInResponse<LinkedInSearchResult>(result, LinkedInResponseStatus.OK, null);
            }
            catch (WebException wex)
            {
                return Utils.GetResponse<LinkedInSearchResult>(null, wex, null);
            }
            catch (Exception ex)
            {
                return new LinkedInResponse<LinkedInSearchResult>(null, LinkedInResponseStatus.OtherException, null, ex);
            }
        }
Пример #4
0
 internal static string PrepareSearchParams(LinkedInSearchOptions options)
 {
     var sb = new StringBuilder();
     if (options.Keywords.Count > 0)
     {
         var keywords = string.Join(SPACE, options.Keywords);
         sb.Append("keywords=");
         sb.Append(keywords.Replace("'", "%5C").Replace(" ",SPACE));
         sb.Append("&");
     }
     if (!string.IsNullOrEmpty(options.FirstName))
     {
         sb.Append("first-name=");
         sb.Append(options.FirstName.Replace("'", "%5C").Replace(" ",SPACE));
         sb.Append("&");
     }
     if (!string.IsNullOrEmpty(options.LastName))
     {
         sb.Append("last-name=");
         sb.Append(options.LastName.Replace("'", "%5C").Replace(" ",SPACE));
         sb.Append("&");
     }
     if (!string.IsNullOrEmpty(options.CompanyName))
     {
         sb.Append("company-name=");
         sb.Append(options.CompanyName.Replace("'", "%5C").Replace(" ",SPACE));
         sb.Append("&");
         if (options.CurrentCompany.HasValue)
         {
             sb.Append(options.CurrentCompany.Value ? "current-company=true" : "current-company=false");
             sb.Append("&");
         }
     }
     if (!string.IsNullOrEmpty(options.Title))
     {
         sb.Append("title=");
         sb.Append(options.Title.Replace("'", "%5C").Replace(" ",SPACE));
         sb.Append("&");
         if (options.CurrentTitle.HasValue)
         {
             sb.Append(options.CurrentTitle.Value ? "current-title=true" : "current-title=false");
             sb.Append("&");
         }
     }
     if (!string.IsNullOrEmpty(options.SchoolName))
     {
         sb.Append("school-name=");
         sb.Append(options.SchoolName.Replace("'", "%5C").Replace(" ",SPACE));
         sb.Append("&");
         if (options.CurrentSchool.HasValue)
         {
             sb.Append(options.CurrentSchool.Value ? "current-school=true" : "current-school=false");
             sb.Append("&");
         }
     }
     if (options.Country != LinkedInCountries.None)
     {
         if (Utils.CountryCodes.ContainsKey(options.Country))
         {
             sb.Append("country-code=");
             sb.Append(Utils.CountryCodes[options.Country]);
             sb.Append("&");
             if (!string.IsNullOrEmpty(options.PostalCode))
             {
                 sb.Append("postal-code=");
                 sb.Append(options.PostalCode.Replace("'", "%5C").Replace(" ",SPACE));
                 sb.Append("&");
             }
             if (options.Distance.HasValue)
             {
                 sb.Append("distance=");
                 sb.Append(options.Distance.Value);
                 sb.Append("&");
             }
         }
     }
     return sb.ToString();
 }
Пример #5
0
 internal static string PrepareFacetsParams(LinkedInSearchOptions options)
 {
     var sb = new StringBuilder();
     if (options.FacetTypes != LinkedInFacetTypes.None)
     {
         sb.Append("facets=");
         if ((options.FacetTypes & LinkedInFacetTypes.Location) == LinkedInFacetTypes.Location)
             sb.Append("location,");
         if ((options.FacetTypes & LinkedInFacetTypes.Network) == LinkedInFacetTypes.Network)
             sb.Append("network,");
         if ((options.FacetTypes & LinkedInFacetTypes.Industry) == LinkedInFacetTypes.Industry)
             sb.Append("industry,");
         if ((options.FacetTypes & LinkedInFacetTypes.CurrentCompany) == LinkedInFacetTypes.CurrentCompany)
             sb.Append("current-company,");
         if ((options.FacetTypes & LinkedInFacetTypes.PastCompany) == LinkedInFacetTypes.PastCompany)
             sb.Append("past-company,");
         if ((options.FacetTypes & LinkedInFacetTypes.Language) == LinkedInFacetTypes.Language)
             sb.Append("language,");
         if ((options.FacetTypes & LinkedInFacetTypes.School) == LinkedInFacetTypes.School)
             sb.Append("school,");
         sb.Length -= 1;
         if (options.FacetLocationValues.Count > 0)
         {
             sb.Append("&facet=location,");
             sb.Append(string.Join(",", options.FacetLocationValues));
         }
         if (options.FacetNetworkValues != LinkedInFacetNetwork.None)
         {
             sb.Append("&facet=network,");
             if ((options.FacetNetworkValues & LinkedInFacetNetwork.FirstDegree) ==
                 LinkedInFacetNetwork.FirstDegree)
             {
                 sb.Append("F,");
             }
             if ((options.FacetNetworkValues & LinkedInFacetNetwork.SecondDegree) ==
                 LinkedInFacetNetwork.SecondDegree)
             {
                 sb.Append("S,");
             }
             if ((options.FacetNetworkValues & LinkedInFacetNetwork.InsideGroup) ==
                 LinkedInFacetNetwork.InsideGroup)
             {
                 sb.Append("A,");
             }
             if ((options.FacetNetworkValues & LinkedInFacetNetwork.OutOfNetwork) ==
                 LinkedInFacetNetwork.OutOfNetwork)
             {
                 sb.Append("O,");
             }
             sb.Length -= 1;
         }
         if (options.FacetIndustryValues.Count > 0)
         {
             sb.Append("&facet=industry,");
             sb.Append(string.Join(",", options.FacetIndustryValues));
         }
         if (options.FacetCurrentCompanyValues.Count > 0)
         {
             sb.Append("&facet=current-company,");
             sb.Append(string.Join(",", options.FacetCurrentCompanyValues));
         }
         if (options.FacetPastCompanyValues.Count > 0)
         {
             sb.Append("&facet=past-company,");
             sb.Append(string.Join(",", options.FacetPastCompanyValues));
         }
         if (options.FacetSchoolValues.Count > 0)
         {
             sb.Append("&facet=school,");
             sb.Append(string.Join(",", options.FacetSchoolValues));
         }
         if (options.FacetLanguageValues.Count > 0)
         {
             sb.Append("&facet=language,");
             foreach (var f in options.FacetLanguageValues)
             {
                 switch (f)
                 {
                     case LinkedInFacetLanguage.English:
                         sb.Append("en,");
                         break;
                     case LinkedInFacetLanguage.Russian:
                         sb.Append("ru,");
                         break;
                     case LinkedInFacetLanguage.French:
                         sb.Append("fr,");
                         break;
                     case LinkedInFacetLanguage.German:
                         sb.Append("de,");
                         break;
                     case LinkedInFacetLanguage.Italian:
                         sb.Append("it,");
                         break;
                     case LinkedInFacetLanguage.Portuguese:
                         sb.Append("pt,");
                         break;
                     case LinkedInFacetLanguage.Spanish:
                         sb.Append("es,");
                         break;
                     case LinkedInFacetLanguage.Others:
                         sb.Append("_o,");
                         break;
                 }
             }
             sb.Length -= 1;
         }
         sb.Append("&");
     }
     var result = sb.ToString().Replace(",", "%2C").Replace(":", "%3A");
     return result;
 }
Пример #6
0
 internal static string PrepareBucketsParams(LinkedInSearchOptions options)
 {
     var sb = new StringBuilder();
     if (options.FacetFields.HasValues)
     {
         sb.Append(":(");
         if (options.FacetFields[LinkedInFacetFields.Code])
             sb.Append("code,");
         if (options.FacetFields[LinkedInFacetFields.Name])
             sb.Append("name,");
         if (options.BucketFields.HasValues)
         {
             sb.Append("buckets:(");
             if (options.BucketFields[LinkedInBucketFields.Code])
                 sb.Append("code,");
             if (options.BucketFields[LinkedInBucketFields.Name])
                 sb.Append("name,");
             if (options.BucketFields[LinkedInBucketFields.Count])
                 sb.Append("count,");
             if (options.BucketFields[LinkedInBucketFields.Selected])
                 sb.Append("selected,");
             sb.Length -= 1;
             sb.Append(")");
         }
         sb.Append(")");
     }
     return sb.ToString();
 }