Skip to content

Commit f0165a8

Browse files
author
Andrew Omondi
committed
Merge pull request #110 from microsoftgraph/supportMacHttpHandlers
Support mac http handlers. It is important to note that Xamarin.Mac and Xamarin.iOS NSURLSessionHander are in different namespaces.
1 parent 52189cb commit f0165a8

File tree

5 files changed

+68
-12
lines changed

5 files changed

+68
-12
lines changed

src/Microsoft.Graph.Core/Microsoft.Graph.Core.csproj

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
55
<AssemblyTitle>Microsoft Graph Core Client Library</AssemblyTitle>
66
<Authors>Microsoft</Authors>
7-
<TargetFrameworks>netstandard2.0;net461;MonoAndroid80;Xamarin.iOS10</TargetFrameworks>
7+
<TargetFrameworks>netstandard2.0;net461;MonoAndroid80;Xamarin.iOS10;Xamarin.Mac20;</TargetFrameworks>
88
<PreserveCompilationContext>false</PreserveCompilationContext>
99
<AssemblyName>Microsoft.Graph.Core</AssemblyName>
1010
<PackageId>Microsoft.Graph.Core</PackageId>
@@ -37,6 +37,13 @@
3737
<DebugType>full</DebugType>
3838
<LanguageTargets>$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets</LanguageTargets>
3939
</PropertyGroup>
40+
<PropertyGroup Condition="'$(TargetFramework)' == 'Xamarin.Mac20'">
41+
<TargetFrameworkIdentifier>Xamarin.Mac</TargetFrameworkIdentifier>
42+
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
43+
<DefineConstants>$(DefineConstants);macOS</DefineConstants>
44+
<DebugType>full</DebugType>
45+
<LanguageTargets>$(MSBuildExtensionsPath)\Xamarin\Mac\Xamarin.Mac.CSharp.targets</LanguageTargets>
46+
</PropertyGroup>
4047
<PropertyGroup Condition="'$(TargetFramework)' == 'MonoAndroid80'">
4148
<TargetFrameworkIdentifier>MonoAndroid</TargetFrameworkIdentifier>
4249
<TargetFrameworkVersion>v8.0</TargetFrameworkVersion>
@@ -72,6 +79,15 @@
7279
<Reference Include="System.Xml" />
7380
<Reference Include="Microsoft.CSharp" />
7481
</ItemGroup>
82+
<ItemGroup Condition=" '$(TargetFramework)' == 'Xamarin.Mac20' ">
83+
<Reference Include="Xamarin.Mac" />
84+
<Reference Include="System" />
85+
<Reference Include="System.Core" />
86+
<Reference Include="System.Xml" />
87+
<Reference Include="Microsoft.CSharp" />
88+
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
89+
<PackageReference Include="System.Net.Http" Version="4.3.3" />
90+
</ItemGroup>
7591
<ItemGroup Condition=" '$(TargetFramework)' == 'MonoAndroid80' ">
7692
<Reference Include="Mono.Android" />
7793
<Reference Include="System" />

src/Microsoft.Graph.Core/Requests/GraphClientFactory.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,20 @@ internal static (HttpMessageHandler Pipeline, FeatureFlag FeatureFlags) CreatePi
179179
throw new ArgumentNullException(nameof(handlers), "DelegatingHandler array contains null item.");
180180
}
181181

182+
#if iOS || macOS
182183
#if iOS
183-
// Skip CompressionHandler since NSUrlSessionHandler automatically handles decompression on iOS and it can't be turned off.
184+
// Skip CompressionHandler since NSUrlSessionHandler automatically handles decompression on iOS and macOS and it can't be turned off.
184185
// See issue https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/481 for more details.
185186
if (finalHandler.GetType().Equals(typeof(NSUrlSessionHandler)) && handler.GetType().Equals(typeof(CompressionHandler)))
187+
#elif macOS
188+
if (finalHandler.GetType().Equals(typeof(Foundation.NSUrlSessionHandler)) && handler.GetType().Equals(typeof(CompressionHandler)))
189+
#endif
186190
{
187191
// Skip chaining of CompressionHandler.
188192
continue;
189193
}
190194
#endif
195+
191196
// Check for duplicate handler by type.
192197
if (!existingHandlerTypes.Add(handler.GetType()))
193198
{
@@ -206,18 +211,20 @@ internal static (HttpMessageHandler Pipeline, FeatureFlag FeatureFlags) CreatePi
206211
}
207212

208213
/// <summary>
209-
/// Gets a platform's native http handler i.e. NSUrlSessionHandler for Xamarin.iOS, AndroidClientHandler for Xamarin.Android and HttpClientHandler for others.
214+
/// Gets a platform's native http handler i.e. NSUrlSessionHandler for Xamarin.iOS and Xamarin.Mac, AndroidClientHandler for Xamarin.Android and HttpClientHandler for others.
210215
/// </summary>
211216
/// <param name="proxy">The proxy to be used with created client.</param>
212217
/// <returns>
213-
/// 1. NSUrlSessionHandler for Xamarin.iOS
218+
/// 1. NSUrlSessionHandler for Xamarin.iOS and Xamarin.Mac
214219
/// 2. AndroidClientHandler for Xamarin.Android.
215220
/// 3. HttpClientHandler for other platforms.
216221
/// </returns>
217222
internal static HttpMessageHandler GetNativePlatformHttpHandler(IWebProxy proxy = null)
218223
{
219224
#if iOS
220225
return new NSUrlSessionHandler { AllowAutoRedirect = false };
226+
#elif macOS
227+
return new Foundation.NSUrlSessionHandler { AllowAutoRedirect = false };
221228
#elif ANDROID
222229
return new Xamarin.Android.Net.AndroidClientHandler { Proxy = proxy, AllowAutoRedirect = false, AutomaticDecompression = DecompressionMethods.None };
223230
#else

tests/Microsoft.Graph.DotnetCore.Core.Test/Microsoft.Graph.DotnetCore.Core.Test.csproj

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@
2525
<DebugType>full</DebugType>
2626
<LanguageTargets>$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets</LanguageTargets>
2727
</PropertyGroup>
28-
28+
29+
<PropertyGroup Condition="'$(TargetFramework)' == 'Xamarin.Mac20'">
30+
<TargetFrameworkIdentifier>Xamarin.Mac</TargetFrameworkIdentifier>
31+
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
32+
<DefineConstants>$(DefineConstants);macOS</DefineConstants>
33+
<DebugType>full</DebugType>
34+
<LanguageTargets>$(MSBuildExtensionsPath)\Xamarin\Mac\Xamarin.Mac.CSharp.targets</LanguageTargets>
35+
</PropertyGroup>
36+
2937
<PropertyGroup Condition="'$(TargetFramework)' == 'MonoAndroid80'">
3038
<TargetFrameworkIdentifier>MonoAndroid</TargetFrameworkIdentifier>
3139
<TargetFrameworkVersion>v8.0</TargetFrameworkVersion>
@@ -60,7 +68,17 @@
6068
<Reference Include="System.Xml" />
6169
<Reference Include="System.Runtime.Serialization" />
6270
</ItemGroup>
63-
71+
72+
<ItemGroup Condition=" '$(TargetFramework)' == 'Xamarin.Mac20' ">
73+
<Reference Include="Microsoft.CSharp" />
74+
<Reference Include="Xamarin.Mac" />
75+
<Reference Include="System" />
76+
<Reference Include="System.Core" />
77+
<Reference Include="System.Xml" />
78+
<Reference Include="System.Runtime.Serialization" />
79+
<PackageReference Include="System.Net.Http" Version="4.3.3" />
80+
</ItemGroup>
81+
6482
<ItemGroup Condition=" '$(TargetFramework)' == 'MonoAndroid80' ">
6583
<Reference Include="Microsoft.CSharp" />
6684
<Reference Include="Mono.Android" />

tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/GraphClientFactoryTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,18 @@ public void Dispose()
4141
// and 'GraphClientFactory.DefaultHttpHandler' can easily be modified
4242
// by other tests since it's a static delegate.
4343

44-
#if iOS
44+
#if iOS || macOS
4545
[Fact]
4646
public void Should_CreatePipeline_Without_CompressionHandler()
4747
{
4848
using (AuthenticationHandler authenticationHandler = (AuthenticationHandler)GraphClientFactory.CreatePipeline(handlers))
4949
using (RetryHandler retryHandler = (RetryHandler)authenticationHandler.InnerHandler)
5050
using (RedirectHandler redirectHandler = (RedirectHandler)retryHandler.InnerHandler)
51+
#if iOS
5152
using (NSUrlSessionHandler innerMost = (NSUrlSessionHandler)redirectHandler.InnerHandler)
53+
#elif macOS
54+
using (Foundation.NSUrlSessionHandler innerMost = (Foundation.NSUrlSessionHandler)redirectHandler.InnerHandler)
55+
#endif
5256
{
5357
Assert.NotNull(authenticationHandler);
5458
Assert.NotNull(retryHandler);
@@ -57,7 +61,11 @@ public void Should_CreatePipeline_Without_CompressionHandler()
5761
Assert.IsType<AuthenticationHandler>(authenticationHandler);
5862
Assert.IsType<RetryHandler>(retryHandler);
5963
Assert.IsType<RedirectHandler>(redirectHandler);
64+
#if iOS
6065
Assert.IsType<NSUrlSessionHandler>(innerMost);
66+
#elif macOS
67+
Assert.IsType<Foundation.NSUrlSessionHandler>(innerMost);
68+
#endif
6169
}
6270
}
6371
#else

tests/Microsoft.Graph.DotnetCore.Core.Test/Requests/HttpProviderTests.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ public void HttpProvider_DefaultConstructor()
115115
#elif iOS
116116
Assert.IsType<NSUrlSessionHandler>(defaultHttpProvider.httpMessageHandler);
117117
Assert.False((defaultHttpProvider.httpMessageHandler as NSUrlSessionHandler).AllowAutoRedirect);
118+
#elif macOS
119+
Assert.IsType<Foundation.NSUrlSessionHandler>(defaultHttpProvider.httpMessageHandler);
120+
Assert.False((defaultHttpProvider.httpMessageHandler as Foundation.NSUrlSessionHandler).AllowAutoRedirect);
118121
#else
119122
Assert.IsType<HttpClientHandler>(defaultHttpProvider.httpMessageHandler);
120123
Assert.False((defaultHttpProvider.httpMessageHandler as HttpClientHandler).AllowAutoRedirect);
@@ -553,12 +556,16 @@ public void HttpProvider_CustomAndroidClientHandler()
553556
#endif
554557
#endregion
555558

556-
#region iOS
557-
#if iOS
559+
#region iOS_macOS
560+
#if iOS || macOS
558561
[Fact]
559562
public void HttpProvider_CustomNSUrlSessionHandler()
560563
{
564+
#if iOS
561565
using (var httpClientHandler = new NSUrlSessionHandler())
566+
#elif macOS
567+
using (var httpClientHandler = new Foundation.NSUrlSessionHandler())
568+
#endif
562569
using (var httpProvider = new HttpProvider(httpClientHandler, false, null))
563570
{
564571
Assert.Equal(httpClientHandler, httpProvider.httpMessageHandler);
@@ -567,6 +574,6 @@ public void HttpProvider_CustomNSUrlSessionHandler()
567574
}
568575
}
569576
#endif
570-
#endregion
571-
}
572-
}
577+
#endregion
578+
}
579+
}

0 commit comments

Comments
 (0)