Skip to content

Commit

Permalink
Added advanced example
Browse files Browse the repository at this point in the history
  • Loading branch information
ellizio committed Jul 18, 2024
1 parent 9c88748 commit 454ac99
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 2 deletions.
41 changes: 39 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,47 @@
```
7. Pack `Class library` project
8. Enjoy using API client
```csharp
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Weather.Client;

## Examples
using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://your_service_url");

See more examples and extended usages [here](examples)
var provider = new AnonymousAuthenticationProvider();
using var adapter = new HttpClientRequestAdapter(provider, httpClient: httpClient);
var client = new WeatherClient(adapter);

var forecasts = await client.Weatherforecast.GetAsync();
```

See [full example](examples/basic_swagger)

## Advanced Usage

You can provide your own implementations of Microsoft.Kiota.Abstractions from the `Class library` project if you need\
Usage:
```csharp
using Microsoft.Kiota.Abstractions.Authentication;
using Microsoft.Kiota.Http.HttpClientLibrary;
using Weather.Client;

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://your_service_url");

var provider = new AnonymousAuthenticationProvider();
using var adapter = new HttpClientRequestAdapter(provider, new WeatherParseNodeFactory(), new WeatherSerializationWriterFactory(), httpClient: httpClient);
var client = new WeatherClient(adapter);

var forecasts = await client.Weatherforecast.GetAsync();
```

See [full example](examples/advanced)

## More Examples

See more examples [here](examples)

## Additional References

Expand Down
14 changes: 14 additions & 0 deletions examples/advanced/ExampleService.Api/ExampleService.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>

</Project>
44 changes: 44 additions & 0 deletions examples/advanced/ExampleService.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:60353",
"sslPort": 44347
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5167",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7125;http://localhost:5167",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions examples/advanced/ExampleService.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>true</IsPackable>
<PackageId>ExampleService.Client</PackageId>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ExampleService.Api\ExampleService.Api.csproj" ExcludeAssets="All" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Kiota.Autogen.Swagger" Version="1.15.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.9.9" />
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.9.9" />
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.9.9" />
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.9.9" />
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.9.9" />
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.9.9" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions examples/advanced/ExampleService.Client/WeatherParseNodeFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Kiota.Abstractions.Serialization;

namespace Weather.Client;

public class WeatherParseNodeFactory : IParseNodeFactory
{
public IParseNode GetRootParseNode(string contentType, Stream content)
{
throw new NotImplementedException();
}

public string ValidContentType { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Kiota.Abstractions.Serialization;

namespace Weather.Client;

public class WeatherSerializationWriterFactory : ISerializationWriterFactory
{
public ISerializationWriter GetSerializationWriter(string contentType)
{
throw new NotImplementedException();
}

public string ValidContentType { get; }
}
7 changes: 7 additions & 0 deletions examples/advanced/ExampleService.Client/gensettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"name": "WeatherClient",
"namespace": "Weather.Client",
"version": "v1"
}
]
22 changes: 22 additions & 0 deletions examples/advanced/ExampleService.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleService.Api", "ExampleService.Api\ExampleService.Api.csproj", "{F5231CDA-3DDE-461D-A48C-B631DB986876}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleService.Client", "ExampleService.Client\ExampleService.Client.csproj", "{838C542F-EAA3-41F3-B0E9-70950DE32DF3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{F5231CDA-3DDE-461D-A48C-B631DB986876}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5231CDA-3DDE-461D-A48C-B631DB986876}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5231CDA-3DDE-461D-A48C-B631DB986876}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5231CDA-3DDE-461D-A48C-B631DB986876}.Release|Any CPU.Build.0 = Release|Any CPU
{838C542F-EAA3-41F3-B0E9-70950DE32DF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{838C542F-EAA3-41F3-B0E9-70950DE32DF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{838C542F-EAA3-41F3-B0E9-70950DE32DF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{838C542F-EAA3-41F3-B0E9-70950DE32DF3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 454ac99

Please sign in to comment.