SwitchyLinkGenerator - Unofficial Nuget for switchy.io

SwitchyLinkGenerator

What’s SwitchyLinkGenerator?

switchy.io - Unofficial API for links Generator

In the last days, I wrote this component for myself and today -after some test- I’m releasing it on Nuget.

If you use switchy.io, you can generate short links from classic links. This component help you in this process.

How To Use

1
PM> Install-Package SwitchyLinkGenerator

In this example, I’m using this component in an Azure Function with NETCore 3.1.

startup.cs

Add SwitchyLinkGeneratorService as Services.AddSingleton in the startup.cs file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public override void Configure(IFunctionsHostBuilder builder)
{
    var config = new ConfigurationBuilder()
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();		

    // More service ...

    builder.Services.AddSingleton<SwitchyLinkGeneratorService>(s =>
    {
        var key = config["SwitchyApiKey"];
        var res = new SwitchyLinkGeneratorService(key);
        return res;
    }
    );
}

Tips: You get the value of SwitchyApiKey inside the switchy’s portal in the API Section.

Function

Please create a HTTP Trigger Azure Functions and use Dependency Injection for SwitchyLinkGeneratorService

 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
public class ShortURLGenerator
{
    private readonly SwitchyLinkGeneratorService _service;

    public ShortURLGenerator(SwitchyLinkGeneratorService service)
    {
        _service = service;
    }

    [FunctionName("ShortURLGenerator")]
    public async Task<string> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        var content = await GetContentAsyc(req);

        var items = await _service.GenerateLink(content);

        string result = JsonConvert.SerializeObject(items);

        return result;
    }

    private async Task<ContentDTO> GetContentAsyc(HttpRequest request)
    {
        using (StreamReader reader = new StreamReader(request.Body, Encoding.UTF8))
        {
            var json = await reader.ReadToEndAsync();
            return JsonConvert.DeserializeObject<ContentDTO>(json);
        }
    }
}

Call API - JSON

If you want to call the previous function, you need to use this JSON as body

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
{
    "Title" : "My Content Title",
    "Description" : "My Content Description",
    "UrlLink" : "https:// ... /",
    "utm_medium" : "utm-medium-post",
    "utm_campaign" : "utm-campaign-post",
    "utm_content" : "utm-content-post",
    "favicon": "https:// ... /favicon-96x96.png",
    "BaseShortLinkUrl" : "url. ... .xyz",
    "Sources":
    [
        "my-source-one",
        "my-source-two",
        "my-source-three"
    ]
}	

C# Create ContentDTO (example)

Otherwise, If you want to use the service only via C# this is an example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
var links = await _service.GenerateLink
(
    new SwitchyLinkGenerator.DTO.ContentDTO()
    {
        BaseShortLinkUrl = "url. ... .xyz",
        Description = "My Content Description",
        favicon = "https:// ... /favicon-96x96.png",
        Sources = new string[] { "my-source-one","my-source-two","my-source-three" },
        Title = "My Content Title",
        UrlLink = "https:// ... /",
        utm_medium =  "utm-medium-post",
        utm_content =  "utm-content-post",
        utm_campaign = "utm-campaign-post",
    }
);

Output

In all case, the GenerateLink return a JSON with the follow definition:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[{
        "DestinationURL": "[...]",
        "ShortURL": "[...]"
    }, {
        "DestinationURL": "[...]",
        "ShortURL": "[...]"
    },{
        "DestinationURL": "[...]",
        "ShortURL": "[...]"
    }
]

Now you can open your switchy account and see the new links.

Notes:

  • This is not a sponsor post. I wrote this component because I need it.
  • I don’t have any relationship with switchy
  • Last update 2021-03-09 21:40 CTE