Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Implemented connection operations
Browse files Browse the repository at this point in the history
  • Loading branch information
bbaia committed Jan 26, 2012
1 parent 1526d19 commit 72c52e2
Show file tree
Hide file tree
Showing 20 changed files with 676 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Collections.Generic;

using Spring.Json;
using Spring.Social.OAuth1;
Expand Down Expand Up @@ -51,6 +52,8 @@ static void Main(string[] args)
/*
LinkedInProfile profileById = linkedIn.ProfileOperations.GetUserProfileByIdAsync("xO3SEJSVZN").Result;
LinkedInProfile profileByPublicUrl = linkedIn.ProfileOperations.GetUserProfileByPublicUrlAsync("http://www.linkedin.com/in/bbaia").Result;
IList<LinkedInProfile> connections = linkedIn.ConnectionOperations.GetConnectionsAsync().Result;
NetworkStatistics statistics = linkedIn.ConnectionOperations.GetNetworkStatisticsAsync().Result;
*/
// Consume LinkedIn endpoints that are not covered by the API binding
/*
Expand Down Expand Up @@ -102,6 +105,8 @@ static void Main(string[] args)
/*
LinkedInProfile profileById = linkedIn.ProfileOperations.GetUserProfileById("xO3SEJSVZN");
LinkedInProfile profileByPublicUrl = linkedIn.ProfileOperations.GetUserProfileByPublicUrl("http://www.linkedin.com/in/bbaia");
IList<LinkedInProfile> connections = linkedIn.ConnectionOperations.GetConnections();
NetworkStatistics statistics = linkedIn.ConnectionOperations.GetNetworkStatistics();
*/
// Consume LinkedIn endpoints that are not covered by the API binding
/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#region License

/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#endregion

using System;
using System.IO;
using System.Collections.Generic;
#if NET_4_0 || SILVERLIGHT_5
using System.Threading.Tasks;
#else
using Spring.Rest.Client;
using Spring.Http;
#endif

namespace Spring.Social.LinkedIn.Api
{
/// <summary>
/// Interface defining the operations for retrieving authenticated user's connections.
/// </summary>
/// <author>Robert Drysdale</author>
/// <author>Bruno Baia (.NET)</author>
public interface IConnectionOperations
{
#if NET_4_0 || SILVERLIGHT_5
/// <summary>
/// Asynchronously retrieves the 1st-degree connections from the authenticated user's network.
/// </summary>
/// <returns>
/// A <code>Task</code> that represents the asynchronous operation that can return
/// a <see cref="LinkedInProfile"/> object representing the user's connections.
/// </returns>
/// <exception cref="LinkedInApiException">If there is an error while communicating with LinkedIn.</exception>
Task<IList<LinkedInProfile>> GetConnectionsAsync();

/// <summary>
/// Asynchronously retrieves network statistics for the authenticated user.
/// </summary>
/// <returns>
/// A <code>Task</code> that represents the asynchronous operation that can return
/// a <see cref="NetworkStatistics"/> that contains count of 1st-degree and second degree connections.
/// </returns>
/// <exception cref="LinkedInApiException">If there is an error while communicating with LinkedIn.</exception>
Task<NetworkStatistics> GetNetworkStatisticsAsync();
#else
#if !SILVERLIGHT
/// <summary>
/// Retrieves the 1st-degree connections from the authenticated user's network.
/// </summary>
/// <returns>
/// A <see cref="LinkedInProfile"/> object representing the user's connections.
/// </returns>
/// <exception cref="LinkedInApiException">If there is an error while communicating with LinkedIn.</exception>
IList<LinkedInProfile> GetConnections();

/// <summary>
/// Retrieves network statistics for the authenticated user.
/// </summary>
/// <returns>
/// A <see cref="NetworkStatistics"/> that contains count of 1st-degree and second degree connections.
/// </returns>
/// <exception cref="LinkedInApiException">If there is an error while communicating with LinkedIn.</exception>
NetworkStatistics GetNetworkStatistics();
#endif

/// <summary>
/// Asynchronously retrieves the 1st-degree connections from the authenticated user's network.
/// </summary>
/// <param name="operationCompleted">
/// The <code>Action&lt;&gt;</code> to perform when the asynchronous request completes.
/// Provides a <see cref="LinkedInProfile"/> object representing the user's connections.
/// </param>
/// <returns>
/// A <see cref="RestOperationCanceler"/> instance that allows to cancel the asynchronous operation.
/// </returns>
/// <exception cref="LinkedInApiException">If there is an error while communicating with LinkedIn.</exception>
RestOperationCanceler GetConnectionsAsync(Action<RestOperationCompletedEventArgs<IList<LinkedInProfile>>> operationCompleted);

/// <summary>
/// Asynchronously retrieves network statistics for the authenticated user.
/// </summary>
/// <param name="operationCompleted">
/// The <code>Action&lt;&gt;</code> to perform when the asynchronous request completes.
/// Provides a <see cref="NetworkStatistics"/> that contains count of 1st-degree and second degree connections.
/// </param>
/// <returns>
/// A <see cref="RestOperationCanceler"/> instance that allows to cancel the asynchronous operation.
/// </returns>
/// <exception cref="LinkedInApiException">If there is an error while communicating with LinkedIn.</exception>
RestOperationCanceler GetNetworkStatisticsAsync(Action<RestOperationCompletedEventArgs<NetworkStatistics>> operationCompleted);
#endif
}
}
5 changes: 5 additions & 0 deletions src/Spring.Social.LinkedIn/Social/LinkedIn/Api/ILinkedIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ namespace Spring.Social.LinkedIn.Api
/// <author>Bruno Baia (.NET)</author>
public interface ILinkedIn : IApiBinding
{
/// <summary>
/// Gets the portion of the LinkedIn API retrieving connections.
/// </summary>
IConnectionOperations ConnectionOperations { get; }

/// <summary>
/// Gets the portion of the LinkedIn API retrieving and performing operations on profiles.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#region License

/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#endregion

using System;
using System.Collections.Generic;

#if NET_4_0 || SILVERLIGHT_5
using System.Threading.Tasks;
#endif

using Spring.Http;
using Spring.Rest.Client;

namespace Spring.Social.LinkedIn.Api.Impl
{
/// <summary>
/// Implementation of <see cref="IConnectionOperations"/>, providing a binding to LinkedIn's connections-oriented REST resources.
/// </summary>
/// <author>Robert Drysdale</author>
/// <author>Bruno Baia (.NET)</author>
class ConnectionTemplate : IConnectionOperations
{
private RestTemplate restTemplate;

public ConnectionTemplate(RestTemplate restTemplate)
{
this.restTemplate = restTemplate;
}

#region IConnectionOperations Members

#if NET_4_0 || SILVERLIGHT_5
public Task<IList<LinkedInProfile>> GetConnectionsAsync()
{
return this.restTemplate.GetForObjectAsync<IList<LinkedInProfile>>("people/~/connections:(id,first-name,last-name,headline,industry,site-standard-profile-request,public-profile-url,picture-url,summary)?format=json");
}

public Task<NetworkStatistics> GetNetworkStatisticsAsync()
{
return this.restTemplate.GetForObjectAsync<NetworkStatistics>("people/~/network/network-stats?format=json");
}
#else
#if !SILVERLIGHT
public IList<LinkedInProfile> GetConnections()
{
return this.restTemplate.GetForObject<IList<LinkedInProfile>>("people/~/connections:(id,first-name,last-name,headline,industry,site-standard-profile-request,public-profile-url,picture-url,summary)?format=json");
}

public NetworkStatistics GetNetworkStatistics()
{
return this.restTemplate.GetForObject<NetworkStatistics>("people/~/network/network-stats?format=json");
}
#endif

public RestOperationCanceler GetConnectionsAsync(Action<RestOperationCompletedEventArgs<IList<LinkedInProfile>>> operationCompleted)
{
return this.restTemplate.GetForObjectAsync<IList<LinkedInProfile>>("people/~/connections:(id,first-name,last-name,headline,industry,site-standard-profile-request,public-profile-url,picture-url,summary)?format=json", operationCompleted);
}

public RestOperationCanceler GetNetworkStatisticsAsync(Action<RestOperationCompletedEventArgs<NetworkStatistics>> operationCompleted)
{
return this.restTemplate.GetForObjectAsync<NetworkStatistics>("people/~/network/network-stats?format=json", operationCompleted);
}
#endif

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public object Deserialize(JsonValue json, JsonMapper mapper)
ID = json.GetValue<string>("id"),
FirstName = json.GetValue<string>("firstName"),
LastName = json.GetValue<string>("lastName"),
Headline = json.GetValue<string>("headline"),
Industry = json.GetValue<string>("industry"),
PictureUrl = json.GetValue<string>("pictureUrl"),
Summary = json.GetValue<string>("summary"),
PublicProfileUrl = json.GetValue<string>("publicProfileUrl")
Headline = json.ContainsName("headline") ? json.GetValue<string>("headline") : "",
Industry = json.ContainsName("industry") ? json.GetValue<string>("industry") : "",
PictureUrl = json.ContainsName("pictureUrl") ? json.GetValue<string>("pictureUrl") : null,
Summary = json.ContainsName("summary") ? json.GetValue<string>("summary") : "",
PublicProfileUrl = json.ContainsName("publicProfileUrl") ? json.GetValue<string>("publicProfileUrl") : null
// SiteStandardProfileRequest
// ApiStandardProfileRequest
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#region License

/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#endregion

using System.Collections.Generic;

using Spring.Json;

namespace Spring.Social.LinkedIn.Api.Impl.Json
{
/// <summary>
/// JSON deserializer for list of LinkedIn user's profiles.
/// </summary>
/// <author>Bruno Baia</author>
class LinkedInProfileListDeserializer : IJsonDeserializer
{
public object Deserialize(JsonValue value, JsonMapper mapper)
{
IList<LinkedInProfile> linkedInProfiles = new List<LinkedInProfile>();
JsonValue usersValue = value.IsObject ? value.GetValue("values") : value;
foreach (JsonValue itemValue in usersValue.GetValues())
{
linkedInProfiles.Add(mapper.Deserialize<LinkedInProfile>(itemValue));
}
return linkedInProfiles;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#region License

/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#endregion

using System;
using System.Globalization;

using Spring.Json;

namespace Spring.Social.LinkedIn.Api.Impl.Json
{
/// <summary>
/// JSON deserializer for network statistics.
/// </summary>
/// <author>Bruno Baia</author>
class NetworkStatisticsDeserializer : IJsonDeserializer
{
public object Deserialize(JsonValue json, JsonMapper mapper)
{
JsonValue values = json.GetValue("values");
return new NetworkStatistics()
{
FirstDegreeCount = values.GetValue<int>(0),
SecondDegreeCount = values.GetValue<int>(1),
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class LinkedInTemplate : AbstractOAuth1ApiBinding, ILinkedIn
{
private static readonly Uri API_URI_BASE = new Uri("https://api.linkedin.com/v1/");

private IConnectionOperations connectionOperations;
private IProfileOperations profileOperations;

/// <summary>
Expand All @@ -56,6 +57,14 @@ public LinkedInTemplate(string consumerKey, string consumerSecret, string access

#region ILinkedIn Members

/// <summary>
/// Gets the portion of the LinkedIn API retrieving connections.
/// </summary>
public IConnectionOperations ConnectionOperations
{
get { return this.connectionOperations; }
}

/// <summary>
/// Gets the portion of the LinkedIn API retrieving and performing operations on profiles.
/// </summary>
Expand Down Expand Up @@ -121,12 +130,15 @@ protected virtual SpringJsonHttpMessageConverter GetJsonMessageConverter()
{
JsonMapper jsonMapper = new JsonMapper();
jsonMapper.RegisterDeserializer(typeof(LinkedInProfile), new LinkedInProfileDeserializer());
jsonMapper.RegisterDeserializer(typeof(IList<LinkedInProfile>), new LinkedInProfileListDeserializer());
jsonMapper.RegisterDeserializer(typeof(NetworkStatistics), new NetworkStatisticsDeserializer());

return new SpringJsonHttpMessageConverter(jsonMapper);
}

private void InitSubApis()
{
this.connectionOperations = new ConnectionTemplate(this.RestTemplate);
this.profileOperations = new ProfileTemplate(this.RestTemplate);
}
}
Expand Down
Loading

0 comments on commit 72c52e2

Please sign in to comment.