-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
HttpTransformer.cs
56 lines (47 loc) · 1.87 KB
/
HttpTransformer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using CKAN.NetKAN.Model;
using log4net;
namespace CKAN.NetKAN.Transformers
{
/// <summary>
/// An <see cref="ITransformer"/> that looks up data from an arbitrary HTTP endpoint.
/// </summary>
internal sealed class HttpTransformer : ITransformer
{
private static readonly ILog Log = LogManager.GetLogger(typeof(HttpTransformer));
public string Name { get { return "http"; } }
public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)
{
if (metadata.Kref != null && metadata.Kref.Source == "http")
{
var json = metadata.Json();
Log.InfoFormat("Executing HTTP transformation with {0}", metadata.Kref);
Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);
if (Uri.IsWellFormedUriString(metadata.Kref.Id, UriKind.Absolute))
{
var resolvedUri = Net.ResolveRedirect(new Uri(metadata.Kref.Id));
Log.InfoFormat("URL {0} resolved to {1}", metadata.Kref.Id, resolvedUri);
if (resolvedUri != null)
{
json["download"] = resolvedUri;
Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);
yield return new Metadata(json);
}
else
{
throw new Kraken("Could not resolve HTTP $kref URL, exceeded number of redirects.");
}
}
else
{
throw new Kraken("Invalid URL in HTTP $kref: " + metadata.Kref.Id);
}
}
else
{
yield return metadata;
}
}
}
}