Mercurial > altnet-hispano
changeset 189:fe47f11f5f20
Adapter para url shorten de google
author | nelopauselli |
---|---|
date | Tue, 09 Aug 2011 09:38:28 -0300 |
parents | 49125b681709 |
children | e6abe8e1a794 |
files | Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs Agendas/trunk/src/Agendas.Configurations/ShortenConfigurationElement.cs Agendas/trunk/src/Agendas.Google.Test/App.config Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj Agendas/trunk/src/Agendas.Google/GUrlShortener.cs Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs |
diffstat | 8 files changed, 74 insertions(+), 49 deletions(-) [+] |
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj Tue Aug 09 09:38:28 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 Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs Tue Aug 09 09:38:28 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 Tue Aug 09 09:38:28 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 Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google.Test/App.config Tue Aug 09 09:38:28 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 Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs Tue Aug 09 09:38:28 2011 -0300 @@ -1,4 +1,5 @@ -using AltNetHispano.Agendas.Google; +using System; +using AltNetHispano.Agendas.Google; using NUnit.Framework; namespace Agendas.Google.Test @@ -10,10 +11,12 @@ [Test] 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); } }
--- a/Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj Tue Aug 09 09:38:28 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 Tue Aug 09 08:55:08 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 Tue Aug 09 09:38:28 2011 -0300 @@ -0,0 +1,48 @@ +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(); + + using(var reader = new StreamReader(response.GetResponseStream())) + { + 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