Permalink
Browse files

Implemented 'Connect' support

  • Loading branch information...
1 parent 0f074c5 commit 46c767b30376193fefa9c2522555ad07d67d5eee @bbaia bbaia committed Jan 24, 2012
@@ -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 Spring.Rest.Client;
+
+namespace Spring.Social.LinkedIn.Api
+{
+ /// <summary>
+ /// Interface specifying a basic set of operations for interacting with LinkedIn.
+ /// </summary>
+ /// <author>Craig Walls</author>
+ /// <author>Robert Drysdale</author>
+ /// <author>Bruno Baia (.NET)</author>
+ public interface ILinkedIn : IApiBinding
+ {
+ /// <summary>
+ /// Gets the underlying <see cref="IRestOperations"/> object allowing for consumption of LinkedIn endpoints
+ /// that may not be otherwise covered by the API binding.
+ /// </summary>
+ /// <remarks>
+ /// The <see cref="IRestOperations"/> object returned is configured to include an OAuth "Authorization" header on all requests.
+ /// </remarks>
+ IRestOperations RestOperations { get; }
+ }
+}
@@ -0,0 +1,116 @@
+#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;
+
+using Spring.Json;
+using Spring.Rest.Client;
+using Spring.Social.OAuth1;
+using Spring.Http.Converters;
+using Spring.Http.Converters.Json;
+
+namespace Spring.Social.LinkedIn.Api.Impl
+{
+ /// <summary>
+ /// This is the central class for interacting with LinkedIn.
+ /// </summary>
+ /// <author>Bruno Baia</author>
+ public class LinkedInTemplate : AbstractOAuth1ApiBinding, ILinkedIn
+ {
+ private static readonly Uri API_URI_BASE = new Uri("https://api.linkedin.com/v1/");
+
+ /// <summary>
+ /// Create a new instance of <see cref="LinkedInTemplate"/>.
+ /// </summary>
+ /// <param name="consumerKey">The application's API key.</param>
+ /// <param name="consumerSecret">The application's API secret.</param>
+ /// <param name="accessToken">An access token acquired through OAuth authentication with LinkedIn.</param>
+ /// <param name="accessTokenSecret">An access token secret acquired through OAuth authentication with LinkedIn.</param>
+ public LinkedInTemplate(string consumerKey, string consumerSecret, string accessToken, string accessTokenSecret)
+ : base(consumerKey, consumerSecret, accessToken, accessTokenSecret)
+ {
+ this.InitSubApis();
+ }
+
+ #region ILinkedIn Members
+
+ /// <summary>
+ /// Gets the underlying <see cref="IRestOperations"/> object allowing for consumption of LinkedIn endpoints
+ /// that may not be otherwise covered by the API binding.
+ /// </summary>
+ /// <remarks>
+ /// The <see cref="IRestOperations"/> object returned is configured to include an OAuth "Authorization" header on all requests.
+ /// </remarks>
+ public IRestOperations RestOperations
+ {
+ get { return this.RestTemplate; }
+ }
+
+ #endregion
+
+ /// <summary>
+ /// Enables customization of the <see cref="RestTemplate"/> used to consume provider API resources.
+ /// </summary>
+ /// <remarks>
+ /// An example use case might be to configure a custom error handler.
+ /// Note that this method is called after the RestTemplate has been configured with the message converters returned from GetMessageConverters().
+ /// </remarks>
+ /// <param name="restTemplate">The RestTemplate to configure.</param>
+ protected override void ConfigureRestTemplate(RestTemplate restTemplate)
+ {
+ restTemplate.BaseAddress = API_URI_BASE;
+ }
+
+ /// <summary>
+ /// Returns a list of <see cref="IHttpMessageConverter"/>s to be used by the internal <see cref="RestTemplate"/>.
+ /// </summary>
+ /// <remarks>
+ /// This implementation adds <see cref="SpringJsonHttpMessageConverter"/> and <see cref="ByteArrayHttpMessageConverter"/> to the default list.
+ /// </remarks>
+ /// <returns>
+ /// The list of <see cref="IHttpMessageConverter"/>s to be used by the internal <see cref="RestTemplate"/>.
+ /// </returns>
+ protected override IList<IHttpMessageConverter> GetMessageConverters()
+ {
+ IList<IHttpMessageConverter> converters = base.GetMessageConverters();
+ converters.Add(new ByteArrayHttpMessageConverter());
+ converters.Add(this.GetJsonMessageConverter());
+ return converters;
+ }
+
+ /// <summary>
+ /// Returns a <see cref="SpringJsonHttpMessageConverter"/> to be used by the internal <see cref="RestTemplate"/>.
+ /// <para/>
+ /// Override to customize the message converter (for example, to set a custom object mapper or supported media types).
+ /// </summary>
+ /// <returns>The configured <see cref="SpringJsonHttpMessageConverter"/>.</returns>
+ protected virtual SpringJsonHttpMessageConverter GetJsonMessageConverter()
+ {
+ JsonMapper jsonMapper = new JsonMapper();
+
+ return new SpringJsonHttpMessageConverter(jsonMapper);
+ }
+
+ private void InitSubApis()
+ {
+ }
+ }
+}
@@ -0,0 +1,61 @@
+#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 Spring.Social.OAuth1;
+using Spring.Social.LinkedIn.Api;
+using Spring.Social.LinkedIn.Api.Impl;
+
+namespace Spring.Social.LinkedIn.Connect
+{
+ /// <summary>
+ /// LinkedIn <see cref="IServiceProvider"/> implementation.
+ /// </summary>
+ /// <author>Keith Donald</author>
+ /// <author>Bruno Baia (.NET)</author>
+ public class LinkedInServiceProvider : AbstractOAuth1ServiceProvider<ILinkedIn>
+ {
+ /// <summary>
+ /// Creates a new instance of <see cref="LinkedInServiceProvider"/>.
+ /// </summary>
+ /// <param name="consumerKey">The application's API key.</param>
+ /// <param name="consumerSecret">The application's API secret.</param>
+ public LinkedInServiceProvider(string consumerKey, string consumerSecret)
+ : base(consumerKey, consumerSecret, new OAuth1Template(consumerKey, consumerSecret,
+ "https://api.linkedin.com/uas/oauth/requestToken",
+ "https://www.linkedin.com/uas/oauth/authorize",
+ "https://www.linkedin.com/uas/oauth/authenticate",
+ "https://api.linkedin.com/uas/oauth/accessToken"))
+ {
+ }
+
+ /// <summary>
+ /// Returns an API interface allowing the client application to access protected resources on behalf of a user.
+ /// </summary>
+ /// <param name="accessToken">The API access token.</param>
+ /// <param name="secret">The access token secret.</param>
+ /// <returns>A binding to the service provider's API.</returns>
+ public override ILinkedIn GetApi(string accessToken, string secret)
+ {
+ return new LinkedInTemplate(this.ConsumerKey, this.ConsumerSecret, accessToken, secret);
+ }
+ }
+}
@@ -49,6 +49,9 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Social\LinkedIn\Api\ILinkedIn.cs" />
+ <Compile Include="Social\LinkedIn\Api\Impl\LinkedTemplate.cs" />
+ <Compile Include="Social\LinkedIn\Connect\LinkedInServiceProvider.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -49,6 +49,9 @@
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Social\LinkedIn\Api\ILinkedIn.cs" />
+ <Compile Include="Social\LinkedIn\Api\Impl\LinkedTemplate.cs" />
+ <Compile Include="Social\LinkedIn\Connect\LinkedInServiceProvider.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -107,6 +107,9 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Social\LinkedIn\Api\Impl\LinkedTemplate.cs" />
+ <Compile Include="Social\LinkedIn\Api\ILinkedIn.cs" />
+ <Compile Include="Social\LinkedIn\Connect\LinkedInServiceProvider.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
@@ -58,6 +58,9 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Social\LinkedIn\Api\ILinkedIn.cs" />
+ <Compile Include="Social\LinkedIn\Api\Impl\LinkedTemplate.cs" />
+ <Compile Include="Social\LinkedIn\Connect\LinkedInServiceProvider.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.$(TargetFrameworkProfile).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.CSharp.targets" />
@@ -58,6 +58,9 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Social\LinkedIn\Api\ILinkedIn.cs" />
+ <Compile Include="Social\LinkedIn\Api\Impl\LinkedTemplate.cs" />
+ <Compile Include="Social\LinkedIn\Connect\LinkedInServiceProvider.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.$(TargetFrameworkProfile).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\Silverlight for Phone\$(TargetFrameworkVersion)\Microsoft.Silverlight.CSharp.targets" />
@@ -0,0 +1,39 @@
+#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 NUnit.Framework;
+
+namespace Spring.Social.LinkedIn.Api.Impl
+{
+ /// <summary>
+ /// Unit tests for the LinkedInTemplate class.
+ /// </summary>
+ /// <author>Bruno Baia</author>
+ [TestFixture]
+ public class LinkedInTemplateTests
+ {
+ [Test]
+ public void IsAuthorizedForUser()
+ {
+ LinkedInTemplate linkedIn = new LinkedInTemplate("API_KEY", "API_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
+ Assert.IsTrue(linkedIn.IsAuthorized);
+ }
+ }
+}
@@ -94,6 +94,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Social\LinkedIn\Api\Impl\LinkedInTemplateTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Spring.Social.LinkedIn.Tests.dll.config">
@@ -94,6 +94,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Social\LinkedIn\Api\Impl\LinkedInTemplateTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Spring.Social.LinkedIn.Tests.dll.config">
@@ -112,6 +112,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Social\LinkedIn\Api\Impl\LinkedInTemplateTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Spring.Social.LinkedIn.Tests.dll.config">

0 comments on commit 46c767b

Please sign in to comment.