annotate Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs @ 302:11dddcc9a862 tip

Historico de Eventos, no muestra bien la Url del Patrocinador.
author juanjose.montesdeocaarbos
date Tue, 14 Aug 2012 21:54:30 -0300
parents 806fd94727ce
children
rev   line source
232
806fd94727ce Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents: 190
diff changeset
1 using System;
806fd94727ce Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents: 190
diff changeset
2 using System.IO;
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
3 using System.Net;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
4 using AltNetHispano.Agendas.Configurations;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
5
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
6 namespace AltNetHispano.Agendas.Google
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
7 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
8 public class GUrlShortenerAdapter
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
9 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
10 private readonly ShortenConfigurationElement _config;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
11
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
12 public GUrlShortenerAdapter()
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
13 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
14 _config = AgendasConfigurationManager.Publicadores.Google.Shorten;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
15 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
16
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
17 public string GetShortUrl(string url)
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
18 {
232
806fd94727ce Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents: 190
diff changeset
19 if (url == null)
806fd94727ce Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents: 190
diff changeset
20 throw new ArgumentNullException("url");
806fd94727ce Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents: 190
diff changeset
21 else if (string.IsNullOrWhiteSpace(url))
806fd94727ce Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents: 190
diff changeset
22 throw new ArgumentException();
806fd94727ce Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents: 190
diff changeset
23
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
24 WebRequest request = WebRequest.Create(_config.UrlAPI);
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
25 request.Method = WebRequestMethods.Http.Post;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
26 request.ContentType = "application/json";
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
27
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
28 using (var writer = new StreamWriter(request.GetRequestStream()))
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
29 {
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
30 var json = string.Format("{{\"longUrl\": \"{0}\"}}", url);
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
31 writer.Write(json);
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
32 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
33
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
34 var response = request.GetResponse() as HttpWebResponse;
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
35
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
36 if (response != null && response.StatusCode == HttpStatusCode.OK)
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
37 {
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
38 var stream = response.GetResponseStream();
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
39
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
40 if (stream != null)
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
41 {
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
42 using (var reader = new StreamReader(stream))
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
43 {
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
44 const string prefix = "\"id\": \"";
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
45
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
46 var json = reader.ReadToEnd();
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
47
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
48 var start = json.IndexOf(prefix) + prefix.Length;
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
49 if (start > prefix.Length)
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
50 {
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
51 var length = json.IndexOf("\",", start) - start;
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
52
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
53 if (length > 0)
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
54 return json.Substring(start, length);
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
55 }
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
56 }
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
57 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
58 }
190
e6abe8e1a794 Mejoras menores en shorten url
nelopauselli
parents: 189
diff changeset
59
189
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
60 return string.Empty;
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
61 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
62 }
fe47f11f5f20 Adapter para url shorten de google
nelopauselli
parents:
diff changeset
63 }