Skip to content

Commit

Permalink
Sample using local api (#785)
Browse files Browse the repository at this point in the history
* Add Sample

* change the reference of the Projects

---------

Co-authored-by: Benjamin Howarth <322383+benjaminhowarth1@users.noreply.github.com>
  • Loading branch information
NakWarsi and jamiehowarth0 committed Apr 12, 2023
1 parent 5fe5ce5 commit 00dde43
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 0 deletions.
37 changes: 37 additions & 0 deletions samples/SampleWithLocalAPIs.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.852
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibraryWithSDKandRefitService", "sampleUsngLocalApi\LibraryWithSDKandRefitService\LibraryWithSDKandRefitService.csproj", "{97A0AF3F-830B-4067-BFC3-0BA7AC6851E7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RestApiForTest", "sampleUsngLocalApi\RestApiforTest\RestApiForTest.csproj", "{1A2E3E56-878E-4D2D-89D3-626DB0825120}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleSampleUsingLocalApi", "sampleUsngLocalApi\ConsoleApplication\ConsoleSampleUsingLocalApi.csproj", "{D7F3EC64-7473-4FD3-AB05-D7BE55009BE3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{97A0AF3F-830B-4067-BFC3-0BA7AC6851E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{97A0AF3F-830B-4067-BFC3-0BA7AC6851E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{97A0AF3F-830B-4067-BFC3-0BA7AC6851E7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{97A0AF3F-830B-4067-BFC3-0BA7AC6851E7}.Release|Any CPU.Build.0 = Release|Any CPU
{1A2E3E56-878E-4D2D-89D3-626DB0825120}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1A2E3E56-878E-4D2D-89D3-626DB0825120}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1A2E3E56-878E-4D2D-89D3-626DB0825120}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1A2E3E56-878E-4D2D-89D3-626DB0825120}.Release|Any CPU.Build.0 = Release|Any CPU
{D7F3EC64-7473-4FD3-AB05-D7BE55009BE3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7F3EC64-7473-4FD3-AB05-D7BE55009BE3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7F3EC64-7473-4FD3-AB05-D7BE55009BE3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7F3EC64-7473-4FD3-AB05-D7BE55009BE3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {ACEEE495-B3D1-4788-9EE2-C33449109B85}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\LibraryWithSDKandRefitService\LibraryWithSDKandRefitService.csproj" />
</ItemGroup>

</Project>
53 changes: 53 additions & 0 deletions samples/sampleUsngLocalApi/ConsoleApplication/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using LibraryWithSDKandRefitService;
using Refit;

namespace ConsoleSampleUsingLocalApi
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
HttpClient _client = new HttpClient
{
BaseAddress = new Uri("http://localhost:61868")
};
IRestService _restApiService = RestService.For<IRestService>(_client);
Console.WriteLine("Enter from the following numbers to access the APIs,\n1 for get ,\n2 for get with argument, \n3 for post,\n4 for put, \n5 for Delete \n");
while (true)
{
int choice = Int32.Parse(Console.ReadLine() ?? "6");
switch (choice)
{
case 1:
var result1 = _restApiService.GetWithNoParameter().Result;
Console.WriteLine(result1);
break;
case 2:
var result2 = _restApiService.GetWithParameter(4).Result;
Console.WriteLine(result2);
break;
case 3:
var result3 = _restApiService.PostWithTestObject(new ModelForTest()).Result;
Console.WriteLine(result3);
break;
case 4:
var result4 = _restApiService.PutWithParameters(4, new ModelForTest()).Result;
Console.WriteLine(result4);
break;
case 5:
var result5 = _restApiService.DeleteWithParameters(5).Result;
Console.WriteLine(result5);
break;
default:
Console.WriteLine("Bhai Please Enter valid if you are really serious");
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Refit;

namespace LibraryWithSDKandRefitService
{
public interface IRestService
{
[Get("/api/values")]
Task<string> GetWithNoParameter();

[Get("/api/values/{id}")]
Task<string> GetWithParameter([AliasAs("id")] int id);

[Post("/api/values")]
Task<string> PostWithTestObject([Body] ModelForTest modelObject);

[Put("/api/values/{id}")]
Task<string> PutWithParameters([AliasAs("id")] int id, [Body] ModelForTest modelObject);

[Delete("/api/values/{id}")]
Task<string> DeleteWithParameters([AliasAs("id")] int id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Refit" Version="4.8.14" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace LibraryWithSDKandRefitService
{
public class ModelForTest
{
public string TestVariable { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using LibraryWithSDKandRefitService;
using Microsoft.AspNetCore.Mvc;

namespace RestApiforTest.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<string> Get()
{
return "Get Api with no argument was Called";
}

// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "Get Api was called";
}

// POST api/values
[HttpPost]
public ActionResult<string> Post([FromBody] ModelForTest testObject)
{
return "Post Api was Called";
}

// PUT api/values/5
[HttpPut("{id}")]
public ActionResult<string> Put(int id, [FromBody] ModelForTest testObject)
{
return "Put Api was called";
}

// DELETE api/values/5
[HttpDelete("{id}")]
public ActionResult<string> Delete(int id)
{
return "Delete Api was Called";
}
}
}
24 changes: 24 additions & 0 deletions samples/sampleUsngLocalApi/RestApiforTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace RestApiforTest
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61868",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"RestApiforTest": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
17 changes: 17 additions & 0 deletions samples/sampleUsngLocalApi/RestApiforTest/RestApiForTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LibraryWithSDKandRefitService\LibraryWithSDKandRefitService.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions samples/sampleUsngLocalApi/RestApiforTest/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace RestApiforTest
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
8 changes: 8 additions & 0 deletions samples/sampleUsngLocalApi/RestApiforTest/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

0 comments on commit 00dde43

Please sign in to comment.