Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetAsync #1

Merged
merged 1 commit into from Apr 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 35 additions & 12 deletions forecast.io/Entities/ForecastIORequest.cs
Expand Up @@ -3,6 +3,11 @@
using System.Globalization;
using System.Text;
using System.Web.Script.Serialization;
#if WITH_ASYNC
using System.Net;
using System.Threading;
using System.Threading.Tasks;
#endif

namespace ForecastIO
{
Expand All @@ -23,26 +28,44 @@ public class ForecastIORequest
private const string CurrentForecastUrl = "https://api.forecast.io/forecast/{0}/{1},{2}?units={3}&extend={4}&exclude={5}";
private const string PeriodForecastUrl = "https://api.forecast.io/forecast/{0}/{1},{2},{3}?units={4}&extend={5}&exclude={6}";

#if WITH_ASYNC
public async Task<ForecastIOResponse> GetAsync()
{
var url = (_time == null)
? String.Format(CurrentForecastUrl, _apiKey, _latitude, _longitude, _unit, _extend, _exclude)
: String.Format(PeriodForecastUrl, _apiKey, _latitude, _longitude, _time, _unit, _extend, _exclude);

using (var client = new CompressionEnabledWebClient { Encoding = Encoding.UTF8 })
{
var responseTask = client.DownloadStringTaskAsync(url);
return await responseTask.ContinueWith((t) => HandleResponse(client.ResponseHeaders, t.Result));
}
}
#endif

public ForecastIOResponse Get()
{
var url = (_time == null) ? String.Format(CurrentForecastUrl, _apiKey, _latitude, _longitude, _unit, _extend, _exclude) :
String.Format(PeriodForecastUrl, _apiKey, _latitude, _longitude, _time, _unit, _extend, _exclude);
var url = (_time == null)
? String.Format(CurrentForecastUrl, _apiKey, _latitude, _longitude, _unit, _extend, _exclude)
: String.Format(PeriodForecastUrl, _apiKey, _latitude, _longitude, _time, _unit, _extend, _exclude);

string result;
using (var client = new CompressionEnabledWebClient())
using (var client = new CompressionEnabledWebClient { Encoding = Encoding.UTF8})
{
client.Encoding = Encoding.UTF8;
result = RequestHelpers.FormatResponse(client.DownloadString(url));
// Set response values.
_apiResponseTime = client.ResponseHeaders["X-Response-Time"];
_apiCallsMade = client.ResponseHeaders["X-Forecast-API-Calls"];
string result = client.DownloadString(url);
return HandleResponse(client.ResponseHeaders, result);
}
}

var serializer = new JavaScriptSerializer();
var dataObject = serializer.Deserialize<ForecastIOResponse>(result);
private ForecastIOResponse HandleResponse(WebHeaderCollection headers, string response)
{
string result = RequestHelpers.FormatResponse(response);

return dataObject;
// Set response values.
_apiResponseTime = headers["X-Response-Time"];
_apiCallsMade = headers["X-Forecast-API-Calls"];

var serializer = new JavaScriptSerializer();
return serializer.Deserialize<ForecastIOResponse>(result);
}

public ForecastIORequest(string apiKey, float latF, float longF, Unit unit, Extend[] extend = null, Exclude[] exclude = null)
Expand Down
4 changes: 2 additions & 2 deletions forecast.io/Forecast.io.csproj
Expand Up @@ -18,7 +18,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG;WITH_ASYNC</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
Expand All @@ -27,7 +27,7 @@
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<DefineConstants>TRACE;WITH_ASYNC</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
Expand Down