2020.08.20 - [IT Story/TIP] - MSSQL 웹(Get/Post)통신 : SQL_APIConsumer
2020.08.21 - [IT Story/TIP] - MSSQL 웹(Get/Post)통신 : SQL_APIConsumer 활용
출처 : https://github.com/geral2/SQL-APIConsumer
위 사이트의 API가 조금 변경 되었습니다.
오늘은 이 변경된 내용을 적용하는 방법에 대해서 포스팅 하고자 합니다.
기존 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'
);
SSRS 보고서 한 페이지로 출력 하는 방법 (0) | 2021.12.14 |
---|---|
파워빌더 PowerClient 사용방법 (0) | 2021.06.18 |
Powerbuilder Stored Procedure(SP) Output 파라미터 관련 (0) | 2021.06.14 |
Gmail 메일 발송시 스팸메일로 처리되어 발송이 안되는 경우 해결 방법 (0) | 2021.05.27 |
PowerBuilder + 키입력시 곱하기 1000 기능 구현 팁 (0) | 2021.04.12 |
Blog is powered by kakao / Designed by 미스터짱