mirror of
https://github.com/swift-server/swift-openapi-lambda.git
synced 2026-06-02 07:27:32 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user