Skip to content

Commit

Permalink
use of RefitHttpFactory to post to medium
Browse files Browse the repository at this point in the history
  • Loading branch information
FrqSalah committed Aug 6, 2024
1 parent 25b4d1c commit 5be7c28
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ArticlePublisher/ArticlePublisher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.17.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.22.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.2.0" />
<PackageReference Include="Refit" Version="7.1.2" />
<PackageReference Include="Refit.HttpClientFactory" Version="7.1.2" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="YamlDotNet" Version="16.0.0" />
</ItemGroup>
Expand Down
13 changes: 12 additions & 1 deletion ArticlePublisher/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
using ArticlePublisher;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Refit;

const string IMediumHost = "https://api.medium.com";


var host = new HostBuilder()
.ConfigureFunctionsWebApplication()
.ConfigureServices(services =>
{
services.AddApplicationInsightsTelemetryWorkerService();
services.ConfigureFunctionsApplicationInsights();
services.ConfigureFunctionsApplicationInsights();
services.AddHttpClient("HttpMedium", (provider, client) =>
{
client.BaseAddress = new System.Uri(IMediumHost);
client.DefaultRequestHeaders.Add("Accept", "application/json");
})
.AddTypedClient(c => RestService.For<IMedium>(c));
})
.Build();

Expand Down
30 changes: 24 additions & 6 deletions ArticlePublisher/PublisherFunction.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
using Markdig;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using System.Text.Json.Serialization;
using FromBodyAttribute = Microsoft.Azure.Functions.Worker.Http.FromBodyAttribute;

using static System.Net.Mime.MediaTypeNames;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Configuration;

namespace ArticlePublisher
{
public class PublisherFunction
{
private readonly ILogger<PublisherFunction> _logger;
private readonly IMedium _mediumClient;

public PublisherFunction(ILogger<PublisherFunction> logger)
public PublisherFunction(ILogger<PublisherFunction> logger, IMedium mediumClient)
{
_logger = logger;
_mediumClient = mediumClient;
}

[Function("Publisher")]
Expand All @@ -37,7 +35,27 @@ public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous,
.ToHtml(content!, pipeline);

_logger.LogInformation("C# HTTP trigger function processed a request.");

await PublishToMediumAsync(frontMatter, html);

return new OkObjectResult(html);
}

public async Task PublishToMediumAsync(BlogFrontMatter blogFrontMatter, string html)
{
MediumPost mediumPost = new MediumPost
{
Title = blogFrontMatter.Title,
ContentFormat = "html",
Content = html,
PublishStatus = "draft"
};

var mediumToken = Environment.GetEnvironmentVariable("MEDIUM_TOKEN");
var mediumUserId = Environment.GetEnvironmentVariable("MEDIUM_USERID");

await _mediumClient.CreatePost(mediumUserId, mediumPost, mediumToken);

}
}
}

0 comments on commit 5be7c28

Please sign in to comment.