Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
Use System.Net.Http in .NET 4.5 project
Browse files Browse the repository at this point in the history
* Change ConnectionOptions.Proxy
* TODO: Report detail progress
  • Loading branch information
azyobuzin committed Jul 22, 2016
1 parent c8c44c4 commit bf1d1db
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 440 deletions.
63 changes: 23 additions & 40 deletions CoreTweet.Shared/ConnectionOptions.cs
Expand Up @@ -24,10 +24,6 @@
using System;
using System.Net;

#if WIN_RT
using Windows.Web.Http;
#endif

namespace CoreTweet
{
/// <summary>
Expand All @@ -38,65 +34,43 @@ public class ConnectionOptions
: ICloneable
#endif
{
/// <summary>
/// Initializes a new instance of the <see cref="ConnectionOptions"/> class.
/// </summary>
public ConnectionOptions()
{
this.ApiUrl = "https://api.twitter.com";
this.UploadUrl = "https://upload.twitter.com";
this.UserStreamUrl = "https://userstream.twitter.com";
this.SiteStreamUrl = "https://sitestream.twitter.com";
this.StreamUrl = "https://stream.twitter.com";
this.ApiVersion = "1.1";
this.Timeout = 100000;
#if SYNC
this.ReadWriteTimeout = 300000;
#endif
this.Proxy = WebRequest.DefaultWebProxy;
this.UserAgent = "CoreTweet";
this.UseCompression = true;
this.UseCompressionOnStreaming = false;
this.DisableKeepAlive = true;
}

/// <summary>
/// Gets or sets the URL of REST API.
/// <para>Default: <c>"https://api.twitter.com"</c></para>
/// </summary>
public string ApiUrl { get; set; }
public string ApiUrl { get; set; } = "https://api.twitter.com";

/// <summary>
/// Gets or sets the URL of upload API.
/// <para>Default: <c>"https://upload.twitter.com"</c></para>
/// </summary>
public string UploadUrl { get; set; }
public string UploadUrl { get; set; } = "https://upload.twitter.com";

/// <summary>
/// Gets or sets the URL of User Streams API.
/// <para>Default: <c>"https://userstream.twitter.com"</c></para>
/// </summary>
public string UserStreamUrl { get; set; }
public string UserStreamUrl { get; set; } = "https://userstream.twitter.com";

/// <summary>
/// Gets or sets the URL of Site Streams API.
/// <para>Default: <c>"https://sitestream.twitter.com"</c></para>
/// </summary>
public string SiteStreamUrl { get; set; }
public string SiteStreamUrl { get; set; } = "https://sitestream.twitter.com";

/// <summary>
/// Gets or sets the URL of Public Streams API.
/// <para>Default: <c>"https://stream.twitter.com"</c></para>
/// </summary>
public string StreamUrl { get; set; }
public string StreamUrl { get; set; } = "https://stream.twitter.com";

/// <summary>
/// Gets or sets the version of the Twitter API.
/// <para>Default: <c>"1.1"</c></para>
/// </summary>
public string ApiVersion { get; set; }
public string ApiVersion { get; set; } = "1.1";

private int timeout;
private int timeout = 100000;
/// <summary>
/// Gets or sets the time-out value in milliseconds.
/// </summary>
Expand All @@ -115,10 +89,9 @@ public int Timeout
}

#if SYNC
private int readWriteTimeout;
private int readWriteTimeout = 300000;
/// <summary>
/// Gets or sets a time-out in milliseconds when writing to or reading from a stream.
/// This value will be applied to only sync API methods.
/// </summary>
public int ReadWriteTimeout
{
Expand All @@ -135,30 +108,37 @@ public int ReadWriteTimeout
}
#endif

/// <summary>
/// Gets or sets a value that indicates whether the handler uses a proxy for requests.
/// </summary>
public bool UseProxy { get; set; } = true;

#if WEBPROXY
/// <summary>
/// Gets or sets the proxy information for the request.
/// </summary>
public IWebProxy Proxy { get; set; }
public IWebProxy Proxy { get; set; } = null;
#endif

/// <summary>
/// Gets or sets the value of the User-agent HTTP header.
/// </summary>
public string UserAgent { get; set; }
public string UserAgent { get; set; } = "CoreTweet";

/// <summary>
/// Gets or sets whether the compression is used on non-streaming requests.
/// </summary>
public bool UseCompression { get; set; }
public bool UseCompression { get; set; } = true;

/// <summary>
/// Gets or sets whether the compression is used on streaming requests.
/// </summary>
public bool UseCompressionOnStreaming { get; set; }
public bool UseCompressionOnStreaming { get; set; } = false;

/// <summary>
/// Gets or sets whether Keep-Alive requests are disabled.
/// </summary>
public bool DisableKeepAlive { get; set; }
public bool DisableKeepAlive { get; set; } = true;

/// <summary>
/// Creates a new object that is a copy of the current instance.
Expand All @@ -178,7 +158,10 @@ public object Clone()
#if SYNC
ReadWriteTimeout = this.ReadWriteTimeout,
#endif
UseProxy = this.UseProxy,
#if WEBPROXY
Proxy = this.Proxy,
#endif
UserAgent = this.UserAgent,
UseCompression = this.UseCompression,
UseCompressionOnStreaming = this.UseCompressionOnStreaming,
Expand Down
6 changes: 4 additions & 2 deletions CoreTweet.Shared/Internal/Extensions.cs
Expand Up @@ -147,11 +147,12 @@ internal static void Rethrow(this Exception ex)
#if WIN_RT || PCL
internal static class TypeInfoExtensions
{
internal static IEnumerable<TypeInfo> GetInterfaces(this TypeInfo source)
internal static IEnumerable<Type> GetInterfaces(this TypeInfo source)
{
return source.ImplementedInterfaces.Select(IntrospectionExtensions.GetTypeInfo);
return source.ImplementedInterfaces;
}

#if !NETCOREAPP1_0
internal static PropertyInfo GetProperty(this TypeInfo source, string name)
{
return source.GetDeclaredProperty(name);
Expand All @@ -161,6 +162,7 @@ internal static MethodInfo GetGetMethod(this PropertyInfo source)
{
return source.GetMethod;
}
#endif
}
#endif

Expand Down
3 changes: 3 additions & 0 deletions CoreTweet.Shared/Internal/InternalUtils.cs
Expand Up @@ -98,6 +98,9 @@ internal static class InternalUtils
var elements = ienumerable.Cast<object>();
var ieElementTypes =
type.GetInterfaces()
#if WIN_RT || PCL
.Select(IntrospectionExtensions.GetTypeInfo)
#endif
.Where(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEnumerable<>))
#if WIN_RT || PCL
.Select(x => x.GenericTypeArguments[0].GetTypeInfo())
Expand Down
52 changes: 18 additions & 34 deletions CoreTweet.Shared/Internal/MultipartItem.cs
Expand Up @@ -21,7 +21,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#if !(WIN_RT || PCL)
#if SYNC
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -39,27 +39,27 @@ protected MultipartItem(string key)

public abstract void WriteTo(Stream stream);

public static MultipartItem Create(string key, object value, Action<int> report)
public static MultipartItem Create(string key, object value)
{
var valueStream = value as Stream;
if (valueStream != null)
return new StreamMultipartItem(key, valueStream, report);
return new StreamMultipartItem(key, valueStream);

if (value is ArraySegment<byte>)
return new ArraySegmentMultipartItem(key, (ArraySegment<byte>)value, report);
return new ArraySegmentMultipartItem(key, (ArraySegment<byte>)value);

var valueByteArray = value as byte[];
if (valueByteArray != null)
return new ByteArrayMultipartItem(key, valueByteArray, report);
return new ByteArrayMultipartItem(key, valueByteArray);

var valueBytes = value as IEnumerable<byte>;
if (valueBytes != null)
return new ByteEnumerableMultipartItem(key, valueBytes, report);
return new ByteEnumerableMultipartItem(key, valueBytes);

#if FILEINFO
var valueFile = value as FileInfo;
if (valueFile != null)
return new FileInfoMultipartItem(key, valueFile, report);
return new FileInfoMultipartItem(key, valueFile);
#endif

return new StringMultipartItem(key, value.ToString());
Expand All @@ -85,13 +85,7 @@ public override void WriteTo(Stream stream)

internal abstract class FileMultipartItem : MultipartItem
{
private readonly Action<int> report;

protected FileMultipartItem(string key, Action<int> report)
: base(key)
{
this.report = report;
}
protected FileMultipartItem(string key) : base(key) { }

public abstract long? Length { get; }
public virtual string FileName => null;
Expand All @@ -109,19 +103,14 @@ public override void WriteTo(Stream stream)
));
this.WriteContent(stream);
}

protected void Report(int writtenBytes)
{
this.report?.Invoke(writtenBytes);
}
}

internal class StreamMultipartItem : FileMultipartItem
{
public Stream Content { get; }

public StreamMultipartItem(string key, Stream content, Action<int> report)
: base(key, report)
public StreamMultipartItem(string key, Stream content)
: base(key)
{
this.Content = content;
}
Expand All @@ -136,7 +125,6 @@ protected override void WriteContent(Stream stream)
while ((count = this.Content.Read(buffer, 0, bufferSize)) > 0)
{
stream.Write(buffer, 0, count);
this.Report(count);
}
}
}
Expand All @@ -145,8 +133,8 @@ internal class ArraySegmentMultipartItem : FileMultipartItem
{
public ArraySegment<byte> Content { get; }

public ArraySegmentMultipartItem(string key, ArraySegment<byte> content, Action<int> report)
: base(key, report)
public ArraySegmentMultipartItem(string key, ArraySegment<byte> content)
: base(key)
{
this.Content = content;
}
Expand All @@ -156,16 +144,15 @@ public ArraySegmentMultipartItem(string key, ArraySegment<byte> content, Action<
protected override void WriteContent(Stream stream)
{
stream.Write(this.Content.Array, this.Content.Offset, this.Content.Count);
this.Report(this.Content.Count);
}
}

internal class ByteArrayMultipartItem : FileMultipartItem
{
public byte[] Content { get; }

public ByteArrayMultipartItem(string key, byte[] content, Action<int> report)
: base(key, report)
public ByteArrayMultipartItem(string key, byte[] content)
: base(key)
{
this.Content = content;
}
Expand All @@ -175,16 +162,15 @@ public ByteArrayMultipartItem(string key, byte[] content, Action<int> report)
protected override void WriteContent(Stream stream)
{
stream.Write(this.Content, 0, this.Content.Length);
this.Report(this.Content.Length);
}
}

internal class ByteEnumerableMultipartItem : FileMultipartItem
{
public IEnumerable<byte> Content { get; }

public ByteEnumerableMultipartItem(string key, IEnumerable<byte> content, Action<int> report)
: base(key, report)
public ByteEnumerableMultipartItem(string key, IEnumerable<byte> content)
: base(key)
{
this.Content = content;
}
Expand All @@ -202,14 +188,12 @@ protected override void WriteContent(Stream stream)
if (i == bufferSize)
{
stream.Write(buffer, 0, bufferSize);
this.Report(bufferSize);
i = 0;
}
}
if (i > 0)
{
stream.Write(buffer, 0, i);
this.Report(i);
}
}
}
Expand All @@ -219,8 +203,8 @@ internal class FileInfoMultipartItem : StreamMultipartItem
{
private readonly long _length;

public FileInfoMultipartItem(string key, FileInfo content, Action<int> report)
: base(key, content.OpenRead(), report)
public FileInfoMultipartItem(string key, FileInfo content)
: base(key, content.OpenRead())
{
this._length = content.Length;
}
Expand Down

0 comments on commit bf1d1db

Please sign in to comment.