[BAEL-7524] API Code
This commit is contained in:
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.tutorials.openapi.quotes;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import com.baeldung.tutorials.openapi.quotes.service.BrokerService;
|
||||
|
||||
@SpringBootApplication
|
||||
public class QuotesApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(QuotesApplication.class, args);
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.tutorials.openapi.quotes.controller;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baeldung.tutorials.openapi.quotes.api.OrdersApiDelegate;
|
||||
|
||||
@Component
|
||||
public class OrdersApiImpl implements OrdersApiDelegate {
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.tutorials.openapi.quotes.controller;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baeldung.tutorials.openapi.quotes.api.QuotesApiDelegate;
|
||||
import com.baeldung.tutorials.openapi.quotes.api.model.QuoteResponse;
|
||||
import com.baeldung.tutorials.openapi.quotes.service.BrokerService;
|
||||
|
||||
@Component
|
||||
public class QuotesApiImpl implements QuotesApiDelegate {
|
||||
private final BrokerService broker;
|
||||
|
||||
public QuotesApiImpl(BrokerService broker) {
|
||||
this.broker = broker;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* GET /quotes/{symbol} : Get current quote for a security
|
||||
*
|
||||
* @param symbol Security's symbol (required)
|
||||
* @return OK (status code 200)
|
||||
* @see QuotesApi#getQuote
|
||||
*/
|
||||
@Override
|
||||
public ResponseEntity<QuoteResponse> getQuote(String symbol) {
|
||||
|
||||
var price = broker.getSecurityPrice(symbol);
|
||||
|
||||
if ( price.isPresent()) {
|
||||
var quote = new QuoteResponse();
|
||||
quote.setSymbol(symbol);
|
||||
quote.setPrice(price.get());
|
||||
return ResponseEntity.ok(quote);
|
||||
}
|
||||
else {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
||||
}
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.tutorials.openapi.quotes.service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class BrokerService {
|
||||
|
||||
private Map<String,BigDecimal> securities = new HashMap<>();
|
||||
|
||||
public BrokerService() {
|
||||
Random rnd = new Random();
|
||||
securities.put("GOOG", BigDecimal.valueOf(rnd.nextDouble() * 1000.00).setScale(4, RoundingMode.DOWN));
|
||||
securities.put("AA", BigDecimal.valueOf(rnd.nextDouble() * 1000.00).setScale(4, RoundingMode.DOWN));
|
||||
securities.put("BAEL", BigDecimal.valueOf(rnd.nextDouble() * 1000.00).setScale(4, RoundingMode.DOWN));
|
||||
}
|
||||
|
||||
|
||||
public Optional<BigDecimal> getSecurityPrice(@NonNull String symbol) {
|
||||
return Optional.ofNullable(securities.get(symbol));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
openapi: 3.0.0
|
||||
info:
|
||||
title: Quotes API
|
||||
version: 1.0.0
|
||||
servers:
|
||||
- description: Test server
|
||||
url: http://localhost:8080
|
||||
paths:
|
||||
/quotes/{symbol}:
|
||||
get:
|
||||
tags:
|
||||
- quotes
|
||||
summary: Get current quote for a security
|
||||
operationId: getQuote
|
||||
security:
|
||||
- ApiKey:
|
||||
- Quotes.Read
|
||||
parameters:
|
||||
- name: symbol
|
||||
in: path
|
||||
required: true
|
||||
description: Security's symbol
|
||||
schema:
|
||||
type: string
|
||||
pattern: '[A-Z0-9]+'
|
||||
responses:
|
||||
'200':
|
||||
description: OK
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/QuoteResponse'
|
||||
/orders/{symbol}:
|
||||
post:
|
||||
tags:
|
||||
- orders
|
||||
operationId: createOrder
|
||||
security:
|
||||
- ApiKey:
|
||||
- Orders.Write
|
||||
summary: Places a new order to buy a security
|
||||
parameters:
|
||||
- name: symbol
|
||||
in: path
|
||||
required: true
|
||||
description: Security's symbol
|
||||
schema:
|
||||
type: string
|
||||
pattern: '[A-Z0-9]'
|
||||
requestBody:
|
||||
required: true
|
||||
description: Order info
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: '#'#/components/schemas/OrderRequest'
|
||||
responses:
|
||||
'201':
|
||||
description: Order accepted
|
||||
content:
|
||||
'application/json':
|
||||
schema:
|
||||
$ref: '#/components/schemas/OrderResponse'
|
||||
|
||||
components:
|
||||
securitySchemes:
|
||||
ApiKey:
|
||||
type: apiKey
|
||||
in: header
|
||||
name: X-API-KEY
|
||||
|
||||
schemas:
|
||||
OrderType:
|
||||
type: string
|
||||
enum:
|
||||
- BUY
|
||||
- SELL
|
||||
OrderStatus:
|
||||
type: string
|
||||
enum:
|
||||
- PENDING
|
||||
- REJECTED
|
||||
- FULFILLED
|
||||
QuoteResponse:
|
||||
description: Quote response
|
||||
type: object
|
||||
properties:
|
||||
symbol:
|
||||
type: string
|
||||
description: security's symbol
|
||||
price:
|
||||
type: number
|
||||
description: Quote value
|
||||
OrderRequest:
|
||||
description: Buy/Sell order details
|
||||
type: object
|
||||
properties:
|
||||
clientRef:
|
||||
type: string
|
||||
orderType:
|
||||
$ref: '#/components/schemas/OrderType'
|
||||
symbol:
|
||||
type: string
|
||||
description: security's symbol
|
||||
quantity:
|
||||
type: number
|
||||
price:
|
||||
type: number
|
||||
OrderResponse:
|
||||
description: Buy/Sell order response
|
||||
properties:
|
||||
clientRef:
|
||||
type: string
|
||||
orderType:
|
||||
$ref: '#/components/schemas/OrderType'
|
||||
symbol:
|
||||
type: string
|
||||
description: security's symbol
|
||||
quantity:
|
||||
type: number
|
||||
price:
|
||||
type: number
|
||||
orderStatus:
|
||||
$ref: '#/components/schemas/OrderStatus'
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
logging:
|
||||
level:
|
||||
root: INFO
|
||||
org.springframework: DEBUG
|
||||
Reference in New Issue
Block a user