Add quote api example (#20)

In preparation for a v2 release that will support the Lambda Runtime v2, I'm adding an end-to-end example project
This commit is contained in:
Sébastien Stormacq
2025-08-30 22:31:12 +02:00
committed by GitHub
parent 78dcf0c864
commit 6a54484782
13 changed files with 578 additions and 0 deletions
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift OpenAPI Lambda open source project
//
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates
// and the Swift OpenAPI Lambda project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import OpenAPIRuntime
import OpenAPILambda
@main
struct QuoteServiceImpl: APIProtocol, OpenAPILambdaHttpApi {
init(transport: OpenAPILambdaTransport) throws {
try self.registerHandlers(on: transport)
}
func getQuote(_ input: Operations.getQuote.Input) async throws -> Operations.getQuote.Output {
let symbol = input.path.symbol
var date: Date = Date()
if let dateString = input.query.date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
date = dateFormatter.date(from: dateString) ?? Date()
}
let price = Components.Schemas.quote(
symbol: symbol,
price: Double.random(in: 100..<150).rounded(),
change: Double.random(in: -5..<5).rounded(),
changePercent: Double.random(in: -0.05..<0.05),
volume: Double.random(in: 10000..<100000).rounded(),
timestamp: date
)
return .ok(.init(body: .json(price)))
}
}
@@ -0,0 +1,4 @@
generate:
- types
- server
+54
View File
@@ -0,0 +1,54 @@
openapi: 3.1.0
info:
title: StockQuoteService
version: 1.0.0
components:
schemas:
quote:
type: object
properties:
symbol:
type: string
price:
type: number
change:
type: number
changePercent:
type: number
volume:
type: number
timestamp:
type: string
format: date-time
paths:
/stocks/{symbol}:
get:
summary: Get the latest quote for a stock
operationId: getQuote
parameters:
- name: symbol
in: path
required: true
schema:
type: string
- name: date
in: query
required: false
schema:
type: string
format: date
tags:
- stocks
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/quote'
400:
description: Bad Request
404:
description: Not Found