티스토리 뷰

IT Story/TIP

MSSQL 웹(Get/Post)통신 : SQL_APIConsumer Upgrade

행복한소식까치 2021. 6. 15. 15:21
반응형

 관련 강좌

 

2020.08.20 - [IT Story/TIP] - MSSQL 웹(Get/Post)통신 : SQL_APIConsumer

 

MSSQL 웹(Get/Post)통신 : SQL_APIConsumer

공공Data 포탈이나 다른 시스템과 연동시 XML 또는 Json으로 Data를 Web을 통해 받아 오는 경우가 종종 있습니다. 웹방식의 프로그램을 작성 할 경우 바로 받아서 사용하면 되지만 , 웹방식으로 받은 D

kindmaster.tistory.com

2020.08.21 - [IT Story/TIP] - MSSQL 웹(Get/Post)통신 : SQL_APIConsumer 활용

 

MSSQL 웹(Get/Post)통신 : SQL_APIConsumer 활용

2020/08/20 - [IT Story/TIP] - MSSQL 웹(Get/Post)통신 : SQL_APIConsumer MSSQL 웹(Get/Post)통신 : SQL_APIConsumer 공공Data 포탈이나 다른 시스템과 연동시 XML 또는 Json으로 Data를 Web을 통해 받아 오는..

kindmaster.tistory.com

 

 

 

 배경 설명

출처 : https://github.com/geral2/SQL-APIConsumer  

위 사이트의 API가 조금 변경 되었습니다.

오늘은 이 변경된 내용을 적용하는 방법에 대해서 포스팅 하고자 합니다.

 

geral2/SQL-APIConsumer

Database Project with generic procedures to consume API through GET/POST methods. - geral2/SQL-APIConsumer

github.com

 

 

선행 작업

기존 SQL-APIConsumer 설치 이력이 없는 사람은 아래 내용은 생략해도 됩니다.

기존 저정 프로시즈 , 펑션, 어셈블리 삭제

drop procedure [APICaller_GET]
go
drop function Create_HMACSHA256
go
drop function GetTimestamp
go
drop function fn_GetBytes
go
drop procedure APICaller_POSTAuth
go
drop procedure APICaller_POST
go
drop procedure APICaller_GETAuth
go
drop procedure APICaller_GET_Headers
go
drop procedure APICaller_GET_Headers_BODY
go
drop procedure APICaller_POST_Headers
go
drop assembly [API_Consumer]
go
drop assembly [Newtonsoft.Json]
go
drop assembly [System.Runtime.Serialization]
go

 

환경 설정

여기서 부터는 위 출처 사이트의  설명과 동일하게 진행했습니다.

-- 1단계

sp_configure 'clr enabled',1
RECONFIGURE

-- 2단계
ALTER DATABASE DB명 SET TRUSTWORTHY ON

-- 3단계
CREATE ASSEMBLY [System.Runtime.Serialization]
AUTHORIZATION	dbo
FROM  N'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Runtime.Serialization.dll'
WITH PERMISSION_SET = UNSAFE--external_access


-- 4단계
CREATE ASSEMBLY [Newtonsoft.Json]
AUTHORIZATION dbo
FROM  N'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Newtonsoft.Json.dll'
WITH PERMISSION_SET = UNSAFE

-- 5단계

 https://github.com/geral2/SQL-APIConsumer
   위사이트에서 파일 다운 받은 후 
   ...\API_Consumer\bin\Debug 폴더 아래의 
API_Consumer.dll
Newtonsoft.Json.dll
System.Net.Http.dll
위 3개 파일을 C:\CLR 폴더를 만들어 복사 붙여넣기 한다.

 

 

설치 방법

API_Consumer 어셈플리 등록

-- 1단계

CREATE ASSEMBLY [API_Consumer]
AUTHORIZATION dbo
FROM  N'C:\CLR\API_Consumer.dll'
WITH PERMISSION_SET = UNSAFE
-- 2단계
GO
	PRINT N'Creating [dbo].[APICaller_WebMethod]...';
GO

CREATE PROCEDURE [dbo].[APICaller_WebMethod]
@httpMethod NVARCHAR (MAX) NULL, @URL NVARCHAR (MAX) NULL, @JsonBody NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_WebMethod]


GO
	PRINT N'Creating [dbo].[APICaller_Web_Extended]...';
GO

CREATE PROCEDURE [dbo].[APICaller_Web_Extended]
@httpMethod NVARCHAR (MAX) NULL, @URL NVARCHAR (MAX) NULL, @Headers NVARCHAR (MAX) NULL, @JsonBody NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_Web_Extended]

GO

PRINT N'Creating [dbo].[Create_HMACSHA256]...';

GO
CREATE FUNCTION [dbo].[Create_HMACSHA256]
(@message NVARCHAR (MAX) NULL, @SecretKey NVARCHAR (MAX) NULL)
RETURNS NVARCHAR (MAX)
AS
 EXTERNAL NAME [API_Consumer].[UserDefinedFunctions].[Create_HMACSHA256]

 GO
PRINT N'Creating [dbo].[GetTimestamp]...';

GO
CREATE FUNCTION [dbo].[GetTimestamp]
( )
RETURNS NVARCHAR (MAX)
AS
 EXTERNAL NAME [API_Consumer].[UserDefinedFunctions].[GetTimestamp]

GO
	PRINT N'Creating [dbo].[fn_GetBytes]...';
GO
CREATE FUNCTION [dbo].fn_GetBytes
(@value NVARCHAR (MAX) NULL )
RETURNS NVARCHAR (MAX)
AS
 EXTERNAL NAME [API_Consumer].[UserDefinedFunctions].fn_GetBytes
GO

PRINT N'Creating [dbo].[APICaller_GET]...';
GO
CREATE PROCEDURE [dbo].[APICaller_GET]
@URL NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_GET]

PRINT N'Creating [dbo].[APICaller_POST]...';
GO
CREATE PROCEDURE [dbo].[APICaller_POST]
@URL NVARCHAR (MAX) NULL
,@JsonBody	NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_POST]


PRINT N'Creating [dbo].[APICaller_POSTAuth]...';
GO
CREATE PROCEDURE [dbo].[APICaller_POSTAuth]
@URL NVARCHAR (MAX) NULL
,@Token NVARCHAR (MAX) NULL
,@JsonBody	NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_POST_Auth]

PRINT N'Creating [dbo].[APICaller_GETAuth]...';
GO
CREATE PROCEDURE [dbo].[APICaller_GETAuth]
@URL NVARCHAR (MAX) NULL
,@Token NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_GET_Auth]

GO

PRINT N'Creating [dbo].[APICaller_GET_Headers]...';

GO
CREATE PROCEDURE [dbo].[APICaller_GET_Headers]
@URL NVARCHAR (MAX) NULL, @Headers NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_GET_Headers]

GO

PRINT N'Creating [dbo].[APICaller_GET_Headers_BODY]...';

GO

CREATE PROCEDURE [dbo].[APICaller_GET_Headers_BODY]
@URL NVARCHAR (MAX) NULL, @JsonBody NVARCHAR (MAX) NULL, @Headers NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].APICaller_GET_JsonBody_Header
GO

PRINT N'Creating [dbo].[APICaller_POST_Headers]...';
GO
CREATE PROCEDURE [dbo].[APICaller_POST_Headers]
@URL NVARCHAR (MAX) NULL, @Headers NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].APICaller_POST_Headers

GO
PRINT N'Creating [dbo].[APICaller_POST_JsonBody_Header]...';
GO
CREATE PROCEDURE [dbo].[APICaller_POST_JsonBody_Header]
	@URL NVARCHAR (MAX)  
  , @Headers NVARCHAR (MAX)  
  , @jSON NVARCHAR (MAX)  
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].APICaller_POST_JsonBody_Headers

GO
PRINT N'Creating [dbo].[APICaller_GET_Extended]...';
GO
CREATE PROCEDURE [dbo].[APICaller_GET_Extended]
@URL NVARCHAR (MAX) NULL, @JsonBody NVARCHAR (MAX) NULL, @Headers NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_GET_Extended]

GO
PRINT N'Creating [dbo].[APICaller_POST_Extended]...';
GO
CREATE PROCEDURE [dbo].[APICaller_POST_Extended]
@URL NVARCHAR (MAX) NULL, @Headers NVARCHAR (MAX) NULL, @JsonBody NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].[APICaller_POST_Extended]

GO
PRINT N'Creating [dbo].[APICaller_POST_Encoded]...';
GO

CREATE PROCEDURE [dbo].APICaller_POST_Encoded
  @URL		NVARCHAR (MAX) NULL
, @Headers	NVARCHAR (MAX) NULL
, @JsonBody NVARCHAR (MAX) NULL
AS EXTERNAL NAME [API_Consumer].[StoredProcedures].APICaller_POST_Encoded

 

 

사용 방법 예제

-- 더 많은 예제는 출처 참조하세요

DECLARE @RoutingNumber AS VARCHAR(50) = '122242597'

--Public API: routingnumbers.info
DECLARE @Url  VARCHAR(200) = CONCAT('https://www.routingnumbers.info/api/name.json?','rn=',@RoutingNumber) 

DECLARE @Results AS TABLE
(
	Context varchar(max)
)

DECLARE @Result AS VARCHAR(MAX)

INSERT INTO @Results
EXEC  [dbo].[APICaller_GET] @Url

--Result: Row per value 

 SELECT  B.*
  FROM (
			SELECT Context 
			  from @Results
		)tb
	OUTER APPLY OPENJSON  (context) B

--Result: column per value.
SELECT 
		[name]	
		,[rn]		
		,[message]	
		,[code]	
 FROM (
			SELECT Context 
			  from @Results
		)tb
	OUTER APPLY OPENJSON  (context)  
  WITH
    ( [name]		VARCHAR(20) '$.name'
	, [rn]			VARCHAR(20) '$.rn'
	, [message]		VARCHAR(20) '$.message'
	, [code]		INT			'$.code'
    );
반응형
댓글