コード例 #1
0
ファイル: LinkedInClient.cs プロジェクト: keithshort/HOPE
 /// <summary>
 /// Gets LinkedIn updates asynchronously
 /// </summary>
 /// <param name="options">The object of type <see cref="LinkedInGetUpdatesOptions"/> representing retrieval options</param>
 /// <param name="action">Action to be invoked when the retrieval process ends</param>
 /// <returns>Status of asynchronous operation</returns>
 /// <exception cref="LinkedInNegativeParameterException">Thrown when UpdateCount or UpdateStart parameters of options are negative</exception>
 /// <exception cref="LinkedInMissingMemberIdException">Thrown when updates are requested by member's id, but no member Id is set</exception>
 /// <exception cref="LinkedInMissingProfileUrlException">Thrown when updates are requested by public member's URL, but no URL is set</exception>
 /// <exception cref="LinkedInInvalidProfileUrlException">Thrown when updates are requested by public member's URL, but URL is not well formed</exception>
 /// <exception cref="LinkedInCountIsZeroException">Thrown when UpdateCount parameter of options is equal to 0</exception>
 /// <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 retrieval options 
 /// var options = new LinkedInGetUpdatesOptions
 ///        {
 ///            UpdateScope = (LinkedInUpdateScope)cboScope.SelectedItem,
 ///            SortBy = (LinkedInUpdateSortField)cboSortBy.SelectedItem,
 ///            SortDirection = (LinkedInUpdateSortDirection)cboDirection.SelectedItem
 ///        };
 /// options.UpdateType = LinkedInUpdateType.AllAvailableUpdateTypes;
 /// options.Parameters.GetBy = LinkedInGetMemberBy.Self;
 /// _Client.GetUpdates(options, processUpdates);
 /// ...
 /// // application defined function
 /// private void processUpdates(LinkedInResponse&lt;IEnumerable&lt;LinkedInUpdate&gt;&gt; response)
 /// {
 ///     // always check response.Result and response.Status before processing 
 ///     if (response.Result != null &amp;&amp; response.Status == LinkedInResponseStatus.OK)
 ///     {
 ///         var linkedInUpdates = response.Result as LinkedInUpdate[] ?? response.Result.ToArray();
 ///         if (!linkedInUpdates.Any()) return;
 ///         foreach (var u in linkedInUpdates)
 ///         {
 ///             switch (u.UpdateType)
 ///             {
 ///                 case "SHAR":
 ///                     Console.WriteLine(((LinkedInShareUpdate)u).UpdateDate);
 ///                     break;
 ///                 case "PROF":
 ///                     // do something by casting: (LinkedInPositionUpdate)u
 ///                     break;
 ///                 case "CONN":
 ///                     // do something by casting: (LinkedInConnectionMemberUpdate)u
 ///                     break;
 ///                 case "NCON":
 ///                 case "CCEM":
 ///                     // do something by casting: (LinkedInConnectionUpdate)u
 ///                     break;
 ///                 case "STAT":
 ///                     // do something by casting: (LinkedInStatusUpdate)u
 ///                     break;
 ///                 case "VIRL":
 ///                     // do something by casting: (LinkedInViralUpdate)u
 ///                     break;
 ///                 case "JGRP":
 ///                     // do something by casting: (LinkedInGroupUpdate)u
 ///                     break;
 ///                    case "APPS":
 ///                 case "APPM":
 ///                     // do something by casting: (LinkedInApplicationUpdate)u
 ///                     break;
 ///                 case "PICU":
 ///                     // do something by casting: (LinkedInPictureUpdate)u
 ///                     break;
 ///                 case "PRFX":
 ///                     // do something by casting: (LinkedInExtendedProfileUpdate)u
 ///                     break;
 ///                 case "PREC":
 ///                 case "SVPR":
 ///                     // do something by casting: (LinkedInRecommendationUpdate)u
 ///                     break;
 ///                 case "JOBP":
 ///                     // do something by casting: (LinkedInJobPostingUpdate)u
 ///                     break;
 ///                 case "CMPY":
 ///                     var baseUpdate = (LinkedInCompanyBaseUpdate)u;
 ///                     switch (baseUpdate.CompanyUpdateType)
 ///                     {
 ///                         case LinkedInCompanyUpdateType.PersonUpdate:
 ///                             // do something by casting: (LinkedInCompanyPersonUpdate)u  
 ///                             break;
 ///                         case LinkedInCompanyUpdateType.JobUpdate:
 ///                             // do something by casting: (LinkedInCompanyJobUpdate)u
 ///                             break;
 ///                         case LinkedInCompanyUpdateType.ProfileUpdate:
 ///                             // do something by casting: (LinkedInCompanyProfileUpdate)u
 ///                             break;
 ///                         case LinkedInCompanyUpdateType.StatusUpdate:
 ///                             // do something by casting: (LinkedInCompanyStatusUpdate)u
 ///                             break;
 ///                     }
 ///                     break;
 ///                 case "MSFC":
 ///                     // do something by casting: (LinkedInStartFollowCompanyUpdate)u
 ///                     break;
 ///             }
 ///         }
 ///     }
 ///     else
 ///     {
 ///         // show possible error message LinkedIn response
 ///         MessageBox.Show(response.Message);
 ///     }
 /// }
 /// </code>
 /// </example>
 public IAsyncResult GetUpdates(LinkedInGetUpdatesOptions options, Action<LinkedInResponse<IEnumerable<LinkedInUpdate>>> action)
 {
     if (options.UpdateCount < 0)
         throw new LinkedInNegativeParameterException("Count of updates to retrieve cannot be negative", "UpdateCount");
     if (options.UpdateStart < 0)
         throw new LinkedInNegativeParameterException("Start point to retrieving updates cannot be negative", "UpdateStart");
     if (options.UpdateCount == 0)
         throw new LinkedInCountIsZeroException("Count parameter of query string cannot be 0");
     switch (options.Parameters.GetBy)
     {
         case LinkedInGetMemberBy.Id:
             if (string.IsNullOrEmpty(options.Parameters.RequestBy))
                 throw new LinkedInMissingMemberIdException("Member Id is not set", "RequestBy");
             break;
         case LinkedInGetMemberBy.Url:
             if (string.IsNullOrEmpty(options.Parameters.RequestBy))
                 throw new LinkedInMissingProfileUrlException("User profile url is not set",
                     "RequestBy");
             if (
                 !Uri.IsWellFormedUriString(options.Parameters.RequestBy,
                     UriKind.RelativeOrAbsolute))
                 throw new LinkedInInvalidProfileUrlException("Invalid user profile url",
                     "RequestBy");
             break;
     }
     GetUpdatesDelegate _delegate = GetUpdates;
     return _delegate.BeginInvoke(options, getUpdatesCallback, action);
 }
コード例 #2
0
ファイル: RequestRunner.cs プロジェクト: keithshort/HOPE
 internal static LinkedInResponse<IEnumerable<LinkedInUpdate>> GetUpdates(LinkedInGetUpdatesOptions options)
 {
     switch (options.Parameters.GetBy)
     {
         case LinkedInGetMemberBy.Self:
             return getUpdates(Utils.GET_UPDATES_URL, options);
         case LinkedInGetMemberBy.Id:
             return getUpdates(Utils.GET_UPDATES_BY_ID_URL.Replace("$USER_ID$", options.Parameters.RequestBy), options);
         default:    //LinkedInGetMemberBy.Url
             MethodInfo getSyntax = null;
             FieldInfo flagsField = null;
             try
             {
                 getSyntax = typeof(UriParser).GetMethod("GetSyntax", BindingFlags.Static | BindingFlags.NonPublic);
                 flagsField = typeof(UriParser).GetField("m_Flags", BindingFlags.Instance | BindingFlags.NonPublic);
                 setCompressionFlag(getSyntax, flagsField, false);
                 return getUpdates(
                         Utils.GET_UPDATES_BY_URL_URL.Replace("$USER_URL$",
                             Utils.NormalizeUrl(options.Parameters.RequestBy)), options);
             }
             finally
             {
                 setCompressionFlag(getSyntax, flagsField, true);
             }
     }
 }
コード例 #3
0
ファイル: RequestRunner.cs プロジェクト: keithshort/HOPE
        private static LinkedInResponse<IEnumerable<LinkedInUpdate>> getUpdates(string urlStart,
            LinkedInGetUpdatesOptions options)
        {
            try
            {
                var updates = new List<LinkedInUpdate>();

                var secondRequest = "";
                var sb = new StringBuilder(urlStart);

                sb.Append(RequestFields.PrepareGetUpdatesFields(options));

                //access token
                sb.Append("oauth2_access_token=");
                sb.Append(Singleton.Instance.AccessToken);

                if (options.UpdateScope == LinkedInUpdateScope.All && options.Parameters.GetBy == LinkedInGetMemberBy.Self)
                    secondRequest = sb.ToString().Replace("scope=self&", "");

                var responseXml = Utils.MakeRequest(sb.ToString(), "GET");
                var xdoc = XDocument.Parse(responseXml);

                if (xdoc.Root != null)
                {
                    var ac = xdoc.Root.Attribute("total");
                    if (ac != null && ac.Value != "0")
                    {
                        var ups = xdoc.Root.Elements("update");
                        updates.AddRange(ups.Select(Utils.BuildUpdate));
                    }
                }

                if (options.UpdateScope == LinkedInUpdateScope.All && options.Parameters.GetBy == LinkedInGetMemberBy.Self)
                {
                    responseXml = Utils.MakeRequest(secondRequest, "GET");
                    var secondXdoc = XDocument.Parse(responseXml);
                    if (secondXdoc.Root != null)
                    {
                        var ac = secondXdoc.Root.Attribute("total");
                        if (ac != null && ac.Value != "0")
                        {
                            var ups = secondXdoc.Root.Elements("update");
                            updates.AddRange(ups.Select(Utils.BuildUpdate));
                        }
                    }
                }
                updates.RemoveAll(u => u == null);
                IEnumerable<LinkedInUpdate> result;
                switch (options.SortBy)
                {
                    case LinkedInUpdateSortField.UpdateDate:
                        result = options.SortDirection == LinkedInUpdateSortDirection.Descending
                                   ? updates.OrderByDescending(u => u.UpdateDate)
                                   : updates.OrderBy(u => u.UpdateDate);
                        break;
                    case LinkedInUpdateSortField.UpdateKey:
                        result = options.SortDirection == LinkedInUpdateSortDirection.Descending
                                   ? updates.OrderByDescending(u => u.UpdateKey)
                                   : updates.OrderBy(u => u.UpdateKey);
                        break;
                    default:    //LinkedInUpdateSortField.UpdateType
                        result = options.SortDirection == LinkedInUpdateSortDirection.Descending
                                   ? updates.OrderByDescending(u => u.UpdateType)
                                   : updates.OrderBy(u => u.UpdateType);
                        break;
                }
                return new LinkedInResponse<IEnumerable<LinkedInUpdate>>(result, LinkedInResponseStatus.OK, null);
            }
            catch (WebException wex)
            {
                return Utils.GetResponse<IEnumerable<LinkedInUpdate>>(null, wex, null);
            }
            catch (Exception ex)
            {
                return new LinkedInResponse<IEnumerable<LinkedInUpdate>>(null, LinkedInResponseStatus.OtherException, null, ex);
            }
        }
コード例 #4
0
ファイル: RequestFields.cs プロジェクト: keithshort/HOPE
        internal static string PrepareGetUpdatesFields(LinkedInGetUpdatesOptions options)
        {
            var unixDate = new DateTime(1970, 1, 1);
            var sb = new StringBuilder();

            //updates scope
            if (options.Parameters.GetBy == LinkedInGetMemberBy.Self)
            {
                if (options.UpdateScope == LinkedInUpdateScope.Self || options.UpdateScope == LinkedInUpdateScope.All)
                    sb.Append("scope=self&");
            }
            else
            {
                //always add scope=self when getting updates by memeber id or member URL
                sb.Append("scope=self&");
            }
            //updates type
            if ((options.UpdateType & LinkedInUpdateType.ApplicationUpdate) == LinkedInUpdateType.ApplicationUpdate)
                sb.Append("type=APPS&");
            if ((options.UpdateType & LinkedInUpdateType.CompanyFollowUpdate) == LinkedInUpdateType.CompanyFollowUpdate)
                sb.Append("type=CMPY&");
            if ((options.UpdateType & LinkedInUpdateType.ConnectionUpdate) == LinkedInUpdateType.ConnectionUpdate)
                sb.Append("type=CONN&");
            if ((options.UpdateType & LinkedInUpdateType.PostedJobUpdate) == LinkedInUpdateType.PostedJobUpdate)
                sb.Append("type=JOBS&");
            if ((options.UpdateType & LinkedInUpdateType.JoinedGroupUpdate) == LinkedInUpdateType.JoinedGroupUpdate)
                sb.Append("type=JGRP&");
            if ((options.UpdateType & LinkedInUpdateType.ChangedPictureUpdate) == LinkedInUpdateType.ChangedPictureUpdate)
                sb.Append("type=PICT&");
            if ((options.UpdateType & LinkedInUpdateType.ExtendedProfileUpdate) == LinkedInUpdateType.ExtendedProfileUpdate)
                sb.Append("type=PRFX&");
            if ((options.UpdateType & LinkedInUpdateType.RecommendationUpdate) == LinkedInUpdateType.RecommendationUpdate)
                sb.Append("type=RECU&");
            if ((options.UpdateType & LinkedInUpdateType.ChangedProfileUpdate) == LinkedInUpdateType.ChangedProfileUpdate)
                sb.Append("type=PRFU&");
            if ((options.UpdateType & LinkedInUpdateType.SharedItemUpdate) == LinkedInUpdateType.SharedItemUpdate)
                sb.Append("type=SHAR&");
            if ((options.UpdateType & LinkedInUpdateType.StatusUpdate) == LinkedInUpdateType.StatusUpdate)
                sb.Append("type=STAT&");
            if ((options.UpdateType & LinkedInUpdateType.ViralUpdate) == LinkedInUpdateType.ViralUpdate)
                sb.Append("type=VIRL&");
            //updates start
            if (options.UpdateStart.HasValue)
            {
                sb.Append("start=");
                sb.Append(options.UpdateStart.Value);
                sb.Append("&");
            }
            //updates count
            if (options.UpdateCount.HasValue)
            {
                sb.Append("count=");
                sb.Append(options.UpdateCount.Value);
                sb.Append("&");
            }
            //updates after
            if (options.After.HasValue)
            {
                var mseconds = (options.After.Value - unixDate).Milliseconds;
                sb.Append("after=");
                sb.Append(mseconds);
                sb.Append("&");
            }
            //updates before
            if (options.Before.HasValue)
            {
                var mseconds = (options.Before.Value - unixDate).Milliseconds;
                sb.Append("before=");
                sb.Append(mseconds);
                sb.Append("&");
            }
            //updates show hidden members
            if (options.ShowHiddenMembers.HasValue)
            {
                sb.Append("show-hidden-members=true&");
            }

            return sb.ToString();
        }