-
Notifications
You must be signed in to change notification settings - Fork 849
/
Copy pathAdbsParUrlClient.sql
199 lines (174 loc) · 8.45 KB
/
AdbsParUrlClient.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
-- Copyright (c) 2024, Oracle and/or its affiliates.
-- The Universal Permissive License (UPL), Version 1.0
--
-- Subject to the condition set forth below, permission is hereby granted to any
-- person obtaining a copy of this software, associated documentation and/or data
-- (collectively the "Software"), free of charge and under any and all copyright
-- rights in the Software, and any and all patent rights owned or freely
-- licensable by each licensor hereunder covering either (i) the unmodified
-- Software as contributed to or provided by such licensor, or (ii) the Larger
-- Works (as defined below), to deal in both
--
-- (a) the Software, and
-- (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
-- one is included with the Software (each a "Larger Work" to which the Software
-- is contributed by such licensors),
--
-- without restriction, including without limitation the rights to copy, create
-- derivative works of, display, perform, and distribute the Software and make,
-- use, sell, offer for sale, import, export, have made, and have sold the
-- Software and the Larger Work(s), and to sublicense the foregoing rights on
-- either these or other terms.
--
-- This license is subject to the following condition:
-- The above copyright notice and either this complete permission notice or at
-- a minimum a reference to the UPL must be included in all copies or
-- substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
Rem
Rem AdbsParUrlClient.sql
Rem
Rem NAME
Rem AdbsParUrlPlClient.sql - PL/SQL script to fetch Autonomous Database pre-authenticated request url data.
Rem
Rem NOTES
Rem None.
SET SERVEROUTPUT ON
/
-----------------------------------------------
-- ADBS Pre auth Request Example package ------
-----------------------------------------------
CREATE OR REPLACE PACKAGE ADBS_PAR_URL_CLIENT
IS
NEXT_GET_PAR_URL VARCHAR2(1024);
COLLECTION_INDEX_POSITION NUMBER;
ITEMS JSON_ARRAY_T;
ITEMS_SIZE NUMBER;
TOTAL_RECORDS NUMBER;
PROCEDURE SET_PAR_URL(PAR_URL IN VARCHAR2);
FUNCTION GET_NEXT_ITEM RETURN CLOB;
END;
/
----------------------------------------------------
-- ADBS Pre auth Request Example package Body ------
----------------------------------------------------
CREATE OR REPLACE PACKAGE BODY ADBS_PAR_URL_CLIENT
IS
PROCEDURE SET_PAR_URL(PAR_URL IN VARCHAR2)
IS
BEGIN
ADBS_PAR_URL_CLIENT.NEXT_GET_PAR_URL := PAR_URL;
ADBS_PAR_URL_CLIENT.COLLECTION_INDEX_POSITION := 0;
ADBS_PAR_URL_CLIENT.ITEMS := JSON_ARRAY_T('[]');
ADBS_PAR_URL_CLIENT.ITEMS_SIZE := 0;
ADBS_PAR_URL_CLIENT.TOTAL_RECORDS := 0;
END;
FUNCTION GET_NEXT_ITEM
RETURN CLOB
IS
HTTP_REQUEST UTL_HTTP.REQ;
HTTP_RESPONSE UTL_HTTP.RESP;
RESPONSE_CODE NUMBER;
RESPONSE_CONTENT CLOB;
TEMP_BUFFER VARCHAR2(32767);
LINKS JSON_ARRAY_T;
HAS_MORE VARCHAR2(32767);
JSON_OBJ JSON_OBJECT_T;
BEGIN
IF ADBS_PAR_URL_CLIENT.COLLECTION_INDEX_POSITION < ADBS_PAR_URL_CLIENT.ITEMS_SIZE THEN
ADBS_PAR_URL_CLIENT.COLLECTION_INDEX_POSITION := ADBS_PAR_URL_CLIENT.COLLECTION_INDEX_POSITION + 1;
RETURN ADBS_PAR_URL_CLIENT.ITEMS.GET(ADBS_PAR_URL_CLIENT.COLLECTION_INDEX_POSITION - 1).to_Clob;
ELSE
IF ADBS_PAR_URL_CLIENT.NEXT_GET_PAR_URL IS NOT NULL THEN
-- Iterate and remove each item from the global array
FOR itemsIndx IN 0 .. ADBS_PAR_URL_CLIENT.ITEMS.get_size - 1
LOOP
ADBS_PAR_URL_CLIENT.ITEMS.REMOVE(itemsIndx);
END LOOP;
-- Initialize the CLOB.
DBMS_LOB.CREATETEMPORARY(RESPONSE_CONTENT, false);
HTTP_REQUEST := UTL_HTTP.BEGIN_REQUEST(ADBS_PAR_URL_CLIENT.NEXT_GET_PAR_URL, 'GET');
UTL_HTTP.SET_HEADER(HTTP_REQUEST, 'CONTENT-TYPE', 'application/json');
DBMS_OUTPUT.PUT_LINE('Invoking Get Request On Url: ' || ADBS_PAR_URL_CLIENT.NEXT_GET_PAR_URL);
-- Get response.
HTTP_RESPONSE := UTL_HTTP.GET_RESPONSE(HTTP_REQUEST);
-- Status code
RESPONSE_CODE := HTTP_RESPONSE.STATUS_CODE;
DBMS_OUTPUT.PUT_LINE('Response received. STATUS=' || RESPONSE_CODE);
DBMS_OUTPUT.PUT_LINE('Response received. REASON_PHRASE=' || HTTP_RESPONSE.reason_phrase);
DBMS_OUTPUT.PUT_LINE('Response received. HTTP_VERSION=' || HTTP_RESPONSE.http_version);
IF HTTP_RESPONSE.STATUS_CODE = 200 THEN
BEGIN
-- Copy the response into the intermediate CLOB.
LOOP
UTL_HTTP.READ_TEXT(HTTP_RESPONSE, TEMP_BUFFER, 32766);
RESPONSE_CONTENT := RESPONSE_CONTENT || TEMP_BUFFER;
END LOOP;
UTL_HTTP.END_RESPONSE(HTTP_RESPONSE);
EXCEPTION
WHEN UTL_HTTP.END_OF_BODY THEN
UTL_HTTP.END_RESPONSE(HTTP_RESPONSE);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
UTL_HTTP.END_RESPONSE(HTTP_RESPONSE);
END;
JSON_OBJ := JSON_OBJECT_T( RESPONSE_CONTENT );
ADBS_PAR_URL_CLIENT.ITEMS := JSON_OBJ.GET_ARRAY('items');
DBMS_OUTPUT.PUT_LINE('size:' || ADBS_PAR_URL_CLIENT.ITEMS.get_size);
ADBS_PAR_URL_CLIENT.TOTAL_RECORDS := ADBS_PAR_URL_CLIENT.TOTAL_RECORDS + ADBS_PAR_URL_CLIENT.ITEMS.get_size;
ADBS_PAR_URL_CLIENT.COLLECTION_INDEX_POSITION := 0;
ADBS_PAR_URL_CLIENT.ITEMS_SIZE := ADBS_PAR_URL_CLIENT.ITEMS.get_size;
-- Get the links field from the json response
LINKS := JSON_OBJ.GET_ARRAY('links');
-- Iterate and get the next page href if available
FOR linksIndx IN 0 .. LINKS.get_size - 1
LOOP
IF JSON_OBJECT_T( LINKS.get(linksIndx)).GET_STRING('rel') = 'next' THEN
ADBS_PAR_URL_CLIENT.NEXT_GET_PAR_URL := JSON_OBJECT_T(LINKS.get(linksIndx)).GET_STRING('href');
DBMS_OUTPUT.PUT_LINE('next href: ' || ADBS_PAR_URL_CLIENT.NEXT_GET_PAR_URL);
END IF;
END LOOP;
-- Get the hasMore field from the json response
HAS_MORE := JSON_OBJ.GET_STRING('hasMore');
DBMS_OUTPUT.PUT_LINE('hasMore: ' || HAS_MORE);
IF HAS_MORE = 'false' THEN
ADBS_PAR_URL_CLIENT.NEXT_GET_PAR_URL := NULL;
END IF;
-- Relase the resources associated with the temporary LOB.
DBMS_LOB.FREETEMPORARY(RESPONSE_CONTENT);
ADBS_PAR_URL_CLIENT.COLLECTION_INDEX_POSITION := COLLECTION_INDEX_POSITION + 1;
RETURN ADBS_PAR_URL_CLIENT.ITEMS.GET(ADBS_PAR_URL_CLIENT.COLLECTION_INDEX_POSITION - 1).to_Clob;
ELSE
ADBS_PAR_URL_CLIENT.NEXT_GET_PAR_URL := NULL;
DBMS_OUTPUT.PUT_LINE('ERROR');
UTL_HTTP.END_RESPONSE(HTTP_RESPONSE);
RETURN NULL;
END IF;
ELSE
RETURN NULL;
END IF;
END IF;
END;
END;
/
-------------------------------------------
---------- Get Pre Auth Url Data ----------
-------------------------------------------
DECLARE
ITEM CLOB;
BEGIN
ADBS_PAR_URL_CLIENT.SET_PAR_URL('https://dataaccess.adb.us-phoenix-1.oraclecloudapps.com/adb/p/NYGM5PicEMTe.../data');
LOOP
ITEM := ADBS_PAR_URL_CLIENT.GET_NEXT_ITEM();
DBMS_OUTPUT.PUT_LINE(ITEM);
EXIT WHEN ITEM IS NULL;
END LOOP;
END;