Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 3.03 주식 차트 정보 기능 테스트 #9 #42

Merged
merged 4 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 36 additions & 28 deletions BE/src/stock/index/stock.index.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { StockIndexListChartElementDto } from './dto/stock.index.list.chart.element.dto';
import { StockIndexListElementDto } from './dto/stock.index.list.element.dto';
import { StockIndexValueElementDto } from './dto/stock.index.value.element.dto';
Expand All @@ -10,22 +11,26 @@ import {
@Injectable()
export class StockIndexService {
async getDomesticStockIndexListByCode(code: string, accessToken: string) {
const url = `${process.env.KOREA_INVESTMENT_BASE_URL}/uapi/domestic-stock/v1/quotations/inquire-index-timeprice`;
const queryParams = `?FID_INPUT_HOUR_1=300&FID_COND_MRKT_DIV_CODE=U&FID_INPUT_ISCD=${code}`;

const response = await fetch(url + queryParams, {
method: 'GET',
headers: {
'content-type': 'application/json; charset=utf-8',
authorization: `Bearer ${accessToken}`,
appkey: process.env.KOREA_INVESTMENT_APP_KEY,
appsecret: process.env.KOREA_INVESTMENT_APP_SECRET,
tr_id: 'FHPUP02110200',
custtype: 'P',
const response = await axios.get<StockIndexChartInterface>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 axios 적용 좋습니다! 고생하셨습니다

`${process.env.KOREA_INVESTMENT_BASE_URL}/uapi/domestic-stock/v1/quotations/inquire-index-timeprice`,
{
headers: {
'content-type': 'application/json; charset=utf-8',
authorization: `Bearer ${accessToken}`,
appkey: process.env.KOREA_INVESTMENT_APP_KEY,
appsecret: process.env.KOREA_INVESTMENT_APP_SECRET,
tr_id: 'FHPUP02110200',
custtype: 'P',
},
params: {
fid_input_hour_1: 300,
fid_cond_mrkt_div_code: 'U',
fid_input_iscd: code,
},
},
});
);

const result: StockIndexChartInterface = await response.json();
const result = response.data;
if (result.rt_cd !== '0') throw new Error('유효하지 않은 토큰');

return new StockIndexListElementDto(
Expand All @@ -40,22 +45,25 @@ export class StockIndexService {
}

async getDomesticStockIndexValueByCode(code: string, accessToken: string) {
const url = `${process.env.KOREA_INVESTMENT_BASE_URL}/uapi/domestic-stock/v1/quotations/inquire-index-price`;
const queryParams = `?FID_COND_MRKT_DIV_CODE=U&FID_INPUT_ISCD=${code}`;

const response = await fetch(url + queryParams, {
method: 'GET',
headers: {
'content-type': 'application/json; charset=utf-8',
authorization: `Bearer ${accessToken}`,
appkey: process.env.KOREA_INVESTMENT_APP_KEY,
appsecret: process.env.KOREA_INVESTMENT_APP_SECRET,
tr_id: 'FHPUP02100000',
custtype: 'P',
const response = await axios.get<StockIndexValueInterface>(
`${process.env.KOREA_INVESTMENT_BASE_URL}/uapi/domestic-stock/v1/quotations/inquire-index-price`,
{
headers: {
'content-type': 'application/json; charset=utf-8',
authorization: `Bearer ${accessToken}`,
appkey: process.env.KOREA_INVESTMENT_APP_KEY,
appsecret: process.env.KOREA_INVESTMENT_APP_SECRET,
tr_id: 'FHPUP02100000',
custtype: 'P',
},
params: {
fid_cond_mrkt_div_code: 'U',
fid_input_iscd: code,
},
},
});
);

const result: StockIndexValueInterface = await response.json();
const result = response.data;
return new StockIndexValueElementDto(
code,
result.output.bstp_nmix_prpr,
Expand Down
3 changes: 3 additions & 0 deletions BE/src/websocket/interface/socket.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface SocketConnectTokenInterface {
approval_key: string;
}
26 changes: 9 additions & 17 deletions BE/src/websocket/socket.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Injectable, OnModuleInit } from '@nestjs/common';
import { WebSocket } from 'ws';
import axios from 'axios';
import { SocketGateway } from './socket.gateway';
import { StockIndexValueElementDto } from '../stock/index/dto/stock.index.value.element.dto';
import { SocketConnectTokenInterface } from './interface/socket.interface';

@Injectable()
export class SocketService implements OnModuleInit {
Expand Down Expand Up @@ -50,20 +52,16 @@ export class SocketService implements OnModuleInit {
}

private async getSocketConnectionKey() {
const url = `${process.env.KOREA_INVESTMENT_BASE_URL}/oauth2/Approval`;

const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
body: JSON.stringify({
const response = await axios.post<SocketConnectTokenInterface>(
`${process.env.KOREA_INVESTMENT_BASE_URL}/oauth2/Approval`,
{
grant_type: 'client_credentials',
appkey: process.env.KOREA_INVESTMENT_APP_KEY,
secretkey: process.env.KOREA_INVESTMENT_APP_SECRET,
}),
});
const result: SocketConnectTokenInterface = await response.json();
},
);

const result = response.data;
return result.approval_key;
}

Expand All @@ -86,9 +84,3 @@ export class SocketService implements OnModuleInit {
);
}
}

// interfaces

interface SocketConnectTokenInterface {
approval_key: string;
}