forked from Azure/azure-functions-openapi-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PetHttpTrigger.cs
181 lines (157 loc) · 13.4 KB
/
PetHttpTrigger.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using AutoFixture;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Configurations;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.InProc.SecurityFlows;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.Models;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.FunctionApp.InProc
{
public class PetHttpTrigger
{
private readonly ILogger<PetHttpTrigger> _logger;
private readonly OpenApiSettings _openapi;
private readonly Fixture _fixture;
public PetHttpTrigger(ILogger<PetHttpTrigger> log, OpenApiSettings openapi, Fixture fixture)
{
this._logger = log.ThrowIfNullOrDefault();
this._openapi = openapi.ThrowIfNullOrDefault();
this._fixture = fixture.ThrowIfNullOrDefault();
}
[FunctionName(nameof(PetHttpTrigger.UpdatePet))]
[OpenApiOperation(operationId: "updatePet", tags: new[] { "pet" }, Summary = "Update an existing pet", Description = "This updates an existing pet.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("petstore_auth", SecuritySchemeType.OAuth2, Flows = typeof(PetStoreAuth))]
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(Pet), Required = true, Description = "Pet object that needs to be updated to the store")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(Pet), Summary = "Pet details updated", Description = "Pet details updated")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.BadRequest, Summary = "Invalid ID supplied", Description = "Invalid ID supplied")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.NotFound, Summary = "Pet not found", Description = "Pet not found")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.MethodNotAllowed, Summary = "Validation exception", Description = "Validation exception")]
public async Task<IActionResult> UpdatePet(
[HttpTrigger(AuthorizationLevel.Anonymous, "PUT", Route = "pet")] HttpRequest req)
{
this._logger.LogInformation($"document title: {this._openapi.DocTitle}");
return await Task.FromResult(new OkObjectResult(this._fixture.Create<Pet>())).ConfigureAwait(false);
}
[FunctionName(nameof(PetHttpTrigger.AddPet))]
[OpenApiOperation(operationId: "addPet", tags: new[] { "pet" }, Summary = "Add a new pet to the store", Description = "This add a new pet to the store.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("petstore_auth", SecuritySchemeType.OAuth2, Flows = typeof(PetStoreAuth))]
[OpenApiRequestBody(contentType: "application/json", bodyType: typeof(Pet), Required = true, Description = "Pet object that needs to be added to the store")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(Pet), Summary = "New pet details added", Description = "New pet details added")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.MethodNotAllowed, Summary = "Invalid input", Description = "Invalid input")]
public async Task<IActionResult> AddPet(
[HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "pet")] HttpRequest req)
{
this._logger.LogInformation($"document title: {this._openapi.DocTitle}");
return await Task.FromResult(new OkObjectResult(this._fixture.Create<Pet>())).ConfigureAwait(false);
}
[FunctionName(nameof(PetHttpTrigger.FindByStatus))]
[OpenApiOperation(operationId: "findPetsByStatus", tags: new[] { "pet" }, Summary = "Finds Pets by status", Description = "Multiple status values can be provided with comma separated strings.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("petstore_auth", SecuritySchemeType.OAuth2, Flows = typeof(PetStoreAuth))]
[OpenApiParameter(name: "status", In = ParameterLocation.Query, Required = true, Type = typeof(List<PetStatus>), Explode = true, Summary = "Pet status value", Description = "Status values that need to be considered for filter", Visibility = OpenApiVisibilityType.Important)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(List<Pet>), Summary = "successful operation", Description = "successful operation")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.BadRequest, Summary = "Invalid status value", Description = "Invalid status value")]
public async Task<IActionResult> FindByStatus(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "pet/findByStatus")] HttpRequest req)
{
this._logger.LogInformation($"document title: {this._openapi.DocTitle}");
var status = req.Query["status"]
.Select(p =>
{
var converted = Enum.TryParse<PetStatus>(p, ignoreCase: true, out var result) ? result : PetStatus.Available;
return converted;
})
.ToList();
var pets = this._fixture.Create<List<Pet>>().Where(p => status.Contains(p.Status));
return await Task.FromResult(new OkObjectResult(pets)).ConfigureAwait(false);
}
[FunctionName(nameof(PetHttpTrigger.FindByTags))]
[OpenApiOperation(operationId: "findPetsByTags", tags: new[] { "pet" }, Summary = "Finds Pets by tags", Description = "Muliple tags can be provided with comma separated strings.", Deprecated = true, Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("petstore_auth", SecuritySchemeType.OAuth2, Flows = typeof(PetStoreAuth))]
[OpenApiParameter(name: "tags", In = ParameterLocation.Query, Required = true, Type = typeof(List<string>), Explode = true, Summary = "Tags to filter by", Description = "Tags to filter by", Visibility = OpenApiVisibilityType.Important)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(List<Pet>), Summary = "successful operation", Description = "successful operation")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.BadRequest, Summary = "Invalid tag value", Description = "Invalid tag value")]
public async Task<IActionResult> FindByTags(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "pet/findByTags")] HttpRequest req)
{
this._logger.LogInformation($"document title: {this._openapi.DocTitle}");
var tags = req.Query["tags"]
.Select(p =>
{
var tag = this._fixture.Build<Tag>().With(q => q.Name, p).Create();
return tag;
})
.ToList();
var pets = this._fixture.Create<List<Pet>>()
.Select(p =>
{
p.Tags = tags;
return p;
});
return await Task.FromResult(new OkObjectResult(pets)).ConfigureAwait(false);
}
[FunctionName(nameof(PetHttpTrigger.GetPetById))]
[OpenApiOperation(operationId: "getPetById", tags: new[] { "pet" }, Summary = "Find pet by ID", Description = "Returns a single pet.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("api_key", SecuritySchemeType.ApiKey, Name = "api_key", In = OpenApiSecurityLocationType.Header)]
[OpenApiParameter(name: "petId", In = ParameterLocation.Path, Required = true, Type = typeof(long), Summary = "ID of pet to return", Description = "ID of pet to return", Visibility = OpenApiVisibilityType.Important)]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(Pet), Summary = "successful operation", Description = "successful operation")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.BadRequest, Summary = "Invalid ID supplied", Description = "Invalid ID supplied")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.NotFound, Summary = "Pet not found", Description = "Pet not found")]
public async Task<IActionResult> GetPetById(
[HttpTrigger(AuthorizationLevel.Anonymous, "GET", Route = "pet/{petId}")] HttpRequest req, long petId)
{
this._logger.LogInformation($"document title: {this._openapi.DocTitle}");
var pet = this._fixture.Build<Pet>().With(p => p.Id, petId).Create();
return await Task.FromResult(new OkObjectResult(pet)).ConfigureAwait(false);
}
[FunctionName(nameof(PetHttpTrigger.UpdatePetWithForm))]
[OpenApiOperation(operationId: "updatePetWithForm", tags: new[] { "pet" }, Summary = "Updates a pet in the store with form data", Description = "This updates a pet in the store with form data.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("petstore_auth", SecuritySchemeType.OAuth2, Flows = typeof(PetStoreAuth))]
[OpenApiParameter(name: "petId", In = ParameterLocation.Path, Required = true, Type = typeof(long), Summary = "ID of pet that needs to be updated", Description = "ID of pet that needs to be updated", Visibility = OpenApiVisibilityType.Important)]
[OpenApiRequestBody(contentType: "application/x-www-form-urlencoded", bodyType: typeof(PetUrlForm), Required = true, Description = "Pet object that needs to be added to the store")]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(Pet), Summary = "successful operation", Description = "successful operation")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.MethodNotAllowed, Summary = "Invalid input", Description = "Invalid input")]
public async Task<IActionResult> UpdatePetWithForm(
[HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "pet/{petId}")] HttpRequest req, long petId)
{
this._logger.LogInformation($"document title: {this._openapi.DocTitle}");
var pet = this._fixture.Build<Pet>().With(p => p.Id, petId).Create();
return await Task.FromResult(new OkObjectResult(pet)).ConfigureAwait(false);
}
[FunctionName(nameof(PetHttpTrigger.DeletePet))]
[OpenApiOperation(operationId: "deletePet", tags: new[] { "pet" }, Summary = "Deletes a pet", Description = "This deletes a pet.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("petstore_auth", SecuritySchemeType.OAuth2, Flows = typeof(PetStoreAuth))]
[OpenApiParameter(name: "api_key", In = ParameterLocation.Header, Type = typeof(string), Visibility = OpenApiVisibilityType.Important)]
[OpenApiParameter(name: "petId", In = ParameterLocation.Path, Required = true, Type = typeof(long), Summary = "Pet id to delete", Description = "Pet id to delete", Visibility = OpenApiVisibilityType.Important)]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.OK, Summary = "successful operation", Description = "successful operation")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.BadRequest, Summary = "Invalid ID supplied", Description = "Invalid ID supplied")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.NotFound, Summary = "Pet not found", Description = "Pet not found")]
public async Task<IActionResult> DeletePet(
[HttpTrigger(AuthorizationLevel.Anonymous, "DELETE", Route = "pet/{petId}")] HttpRequest req, long petId)
{
this._logger.LogInformation($"document title: {this._openapi.DocTitle}");
return await Task.FromResult(new OkResult()).ConfigureAwait(false);
}
[FunctionName(nameof(PetHttpTrigger.UploadFile))]
[OpenApiOperation(operationId: "uploadFile", tags: new[] { "pet" }, Summary = "Uploads an image", Description = "This uploads an image.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("petstore_auth", SecuritySchemeType.OAuth2, Flows = typeof(PetStoreAuth))]
[OpenApiParameter(name: "petId", In = ParameterLocation.Path, Required = true, Type = typeof(long), Summary = "ID of pet to update", Description = "ID of pet to update", Visibility = OpenApiVisibilityType.Important)]
[OpenApiRequestBody(contentType: "multipart/form-data", bodyType: typeof(PetFormData))]
[OpenApiResponseWithBody(statusCode: HttpStatusCode.OK, contentType: "application/json", bodyType: typeof(ApiResponse), Summary = "successful operation", Description = "successful operation")]
public async Task<IActionResult> UploadFile(
[HttpTrigger(AuthorizationLevel.Anonymous, "POST", Route = "pet/{petId}/uploadImage")] HttpRequest req, long petId)
{
this._logger.LogInformation($"document title: {this._openapi.DocTitle}");
return await Task.FromResult(new OkObjectResult(this._fixture.Create<ApiResponse>())).ConfigureAwait(false);
}
}
}