Mercurial > altnet-hispano
changeset 193:523e3586b1c1
Combinar
author | juanjose.montesdeocaarbos |
---|---|
date | Wed, 10 Aug 2011 08:21:32 -0300 |
parents | 03cf6a377ee8 (current diff) 03c237cd5cc4 (diff) |
children | 21ad1c337c94 |
files | Agendas/trunk/src/Agendas.Google/GUrlShortener.cs |
diffstat | 8 files changed, 104 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj Wed Aug 10 08:21:32 2011 -0300 @@ -50,6 +50,7 @@ <Compile Include="PublicadoresConfigurationException.cs" /> <Compile Include="PublicadoresConfigurationSection.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="ShortenConfigurationElement.cs" /> <Compile Include="TwitterConfigurationElement.cs" /> <Compile Include="TwitterSendTwitterConfigurationElement.cs" /> <Compile Include="TwitterSignInConfigurationElement.cs" />
--- a/Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs Wed Aug 10 08:21:32 2011 -0300 @@ -10,6 +10,11 @@ get { return base["calendar"] as CalendarConfigurationElement; } } + [ConfigurationProperty("shorten")] + public ShortenConfigurationElement Shorten + { + get { return base["shorten"] as ShortenConfigurationElement; } + } [ConfigurationProperty("enabled")] public bool Enabled {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Configurations/ShortenConfigurationElement.cs Wed Aug 10 08:21:32 2011 -0300 @@ -0,0 +1,13 @@ +using System.Configuration; + +namespace AltNetHispano.Agendas.Configurations +{ + public class ShortenConfigurationElement : ConfigurationElement + { + [ConfigurationProperty("urlAPI")] + public string UrlAPI + { + get { return base["urlAPI"] as string; } + } + } +} \ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Google.Test/App.config Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google.Test/App.config Wed Aug 10 08:21:32 2011 -0300 @@ -8,34 +8,7 @@ <publicadores> <google enabled="true"> <calendar userName="altnethispano.desa@gmail.com" password="fA&Ec3wU3est" calendarId="altnethispano.desa@gmail.com" /> + <shorten urlAPI="https://www.googleapis.com/urlshortener/v1/url" /> </google> </publicadores> - - <system.serviceModel> - <client> - <endpoint address="https://www.googleapis.com/urlshortener/v1" - binding="webHttpBinding" - bindingConfiguration="webBinding" - behaviorConfiguration="urlShortener" - contract="GoogleAPI.UrlShortener.IUrlShortenerService" - name="UrlShortenerREST" /> - </client> - - <bindings> - <webHttpBinding> - <binding name="webBinding"> - <security mode="Transport"></security> - </binding> - </webHttpBinding> - </bindings> - - <behaviors> - <endpointBehaviors> - <behavior name="urlShortener"> - <webHttp /> - </behavior> - </endpointBehaviors> - </behaviors> - - </system.serviceModel> </configuration> \ No newline at end of file
--- a/Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs Wed Aug 10 08:21:32 2011 -0300 @@ -1,20 +1,42 @@ -using AltNetHispano.Agendas.Google; +using System; +using System.Net; +using AltNetHispano.Agendas.Google; using NUnit.Framework; namespace Agendas.Google.Test { [TestFixture] - [Explicit] public class GUrlShortenerTest { [Test] - public void Obtener_Url_corta() + public void Obtener_url_corta() { - var gUrlShortener = new GUrlShortener(); + var gUrlShortener = new GUrlShortenerAdapter(); var shortUrl = gUrlShortener.GetShortUrl("http://www.twitter.com"); + Console.WriteLine(shortUrl); + Assert.AreEqual("http://goo.gl/CbHS", shortUrl); } + + [Test] + public void Acceder_a_url_corta() + { + var gUrlShortener = new GUrlShortenerAdapter(); + + var shortUrl = gUrlShortener.GetShortUrl("http://altnethispano.org"); + + Console.WriteLine(shortUrl); + + var request = WebRequest.Create(shortUrl); + var response = request.GetResponse() as HttpWebResponse; + + Assert.IsNotNull(response); + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + + Assert.AreEqual(new Uri("http://altnethispano.org/Inicio.aspx"), response.ResponseUri); + + } } }
--- a/Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj Wed Aug 10 08:21:32 2011 -0300 @@ -58,7 +58,7 @@ <Compile Include="DetailsEvents\VanGEventDetail.cs" /> <Compile Include="GCalendarAdapter.cs" /> <Compile Include="GooglePublicador.cs" /> - <Compile Include="GUrlShortener.cs" /> + <Compile Include="GUrlShortenerAdapter.cs" /> <Compile Include="IGCalendarAdapter.cs" /> <Compile Include="IGEventDetail.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
--- a/Agendas/trunk/src/Agendas.Google/GUrlShortener.cs Wed Aug 10 08:20:28 2011 -0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -using System; -//using GoogleAPI.UrlShortener; - -namespace AltNetHispano.Agendas.Google -{ - public class GUrlShortener - { - public string GetShortUrl(string url) - { - //TODO: Esto es utilizando la GoogleAPI.UrlShortener. - //var client = new UrlResource(); - //var response = client.Insert(new ShortenRequest { LongUrl = url }); - - //return response.Id; - throw new NotImplementedException(); - } - } -} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs Wed Aug 10 08:21:32 2011 -0300 @@ -0,0 +1,57 @@ +using System.IO; +using System.Net; +using AltNetHispano.Agendas.Configurations; + +namespace AltNetHispano.Agendas.Google +{ + public class GUrlShortenerAdapter + { + private readonly ShortenConfigurationElement _config; + + public GUrlShortenerAdapter() + { + _config = AgendasConfigurationManager.Publicadores.Google.Shorten; + } + + public string GetShortUrl(string url) + { + WebRequest request = WebRequest.Create(_config.UrlAPI); + request.Method = WebRequestMethods.Http.Post; + request.ContentType = "application/json"; + + using (var writer = new StreamWriter(request.GetRequestStream())) + { + var json = string.Format("{{\"longUrl\": \"{0}\"}}", url); + writer.Write(json); + } + + var response = request.GetResponse() as HttpWebResponse; + + if (response != null && response.StatusCode == HttpStatusCode.OK) + { + var stream = response.GetResponseStream(); + + if (stream != null) + { + using (var reader = new StreamReader(stream)) + { + const string prefix = "\"id\": \""; + + var json = reader.ReadToEnd(); + + var start = json.IndexOf(prefix) + prefix.Length; + if (start > prefix.Length) + { + var length = json.IndexOf("\",", start) - start; + + if (length > 0) + return json.Substring(start, length); + } + } + } + } + + return string.Empty; + } + } +} \ No newline at end of file