-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidateAddress.sql
More file actions
104 lines (88 loc) · 3.85 KB
/
Copy pathValidateAddress.sql
File metadata and controls
104 lines (88 loc) · 3.85 KB
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
/*
Purpose:
- Validate an address through the Google Address Validation API and return parsed location data.
Security:
- Inject the API key at runtime or load it from a secure configuration source.
- Do not commit secrets into this procedure definition.
*/
CREATE PROCEDURE spAddressvalidation
@Address varchar(80) = NULL OUTPUT,
@City varchar(40) = NULL OUTPUT,
@State varchar(40) = NULL OUTPUT,
@Country varchar(40) = NULL OUTPUT,
@PostalCode varchar(20) = NULL OUTPUT,
@County varchar(40) = NULL OUTPUT,
@GPSLatitude numeric(9,6) = NULL OUTPUT,
@GPSLongitude numeric(9,6) = NULL OUTPUT,
@MapURL varchar(1024) = NULL OUTPUT,
@AddressText varchar(200) = NULL,
@ApiKey varchar(200) = NULL
AS
BEGIN
SET NOCOUNT ON
IF NULLIF(@ApiKey, '') IS NULL
BEGIN
RAISERROR('Supply the API key at execution time; do not store it in this procedure.', 16, 1);
RETURN;
END;
DECLARE @URL varchar(MAX)
SET @URL = 'https://addressvalidation.googleapis.com/v1:validateAddress?key=' + @ApiKey
-- Create JSON request body
DECLARE @RequestBody NVARCHAR(MAX);
SET @RequestBody = N'{
"address": {
"regionCode": "' + ISNULL(@Country, '') + N'",
"addressLines": ["' + ISNULL(@AddressText, '') + N'"]
},
"previousResponseId": "",
"enableUspsCass": false
}';
PRINT @RequestBody -- Debugging: Print the request body
DECLARE @Response varchar(8000)
DECLARE @Obj int
DECLARE @Result int
DECLARE @HTTPStatus int
DECLARE @ErrorMsg varchar(MAX)
EXEC @Result = sp_OACreate 'MSXML2.ServerXMLHttp', @Obj OUT
BEGIN TRY
EXEC @Result = sp_OAMethod @Obj, 'open', NULL, 'POST', @URL, false
EXEC @Result = sp_OAMethod @Obj, 'setRequestHeader', NULL, 'Content-Type', 'application/json'
EXEC @Result = sp_OAMethod @Obj, 'send', NULL, @RequestBody
EXEC @Result = sp_OAGetProperty @Obj, 'status', @HTTPStatus OUT
EXEC @Result = sp_OAGetProperty @Obj, 'responseText', @Response OUT
END TRY
BEGIN CATCH
SET @ErrorMsg = ERROR_MESSAGE()
END CATCH
EXEC @Result = sp_OADestroy @Obj
IF (@ErrorMsg IS NOT NULL) OR (@HTTPStatus <> 200) BEGIN
--Improved error handling with response details
DECLARE @HTTPStatusMessage VARCHAR(255);
SELECT @HTTPStatusMessage = CASE
WHEN @HTTPStatus = 400 THEN 'Bad Request'
WHEN @HTTPStatus = 403 THEN 'Forbidden. Check API Key and Address Validation API enablement.'
ELSE 'Unknown Error'
END;
SET @ErrorMsg = 'Error in spGeocode <>: HTTP result is ' + CAST(@HTTPStatus AS VARCHAR(3)) + ' (' + @HTTPStatusMessage + '). ' + ISNULL(@ErrorMsg, '') + ' Response: ' + ISNULL(@Response, '');
RAISERROR(@ErrorMsg, 16, 1)
RETURN
END
-- JSON parsing using JSON_VALUE
SET @GPSLatitude = JSON_VALUE(@Response, '$.result.geocode.location.latitude');
SET @GPSLongitude = JSON_VALUE(@Response, '$.result.geocode.location.longitude');
SET @Address = JSON_VALUE(@Response, '$.result.address.formattedAddress');
SET @City = JSON_VALUE(@Response, '$.result.address.postalAddress.locality');
SET @State = JSON_VALUE(@Response, '$.result.address.postalAddress.administrativeArea');
SET @PostalCode = JSON_VALUE(@Response, '$.result.address.postalAddress.postalCode');
SET @Country = JSON_VALUE(@Response, '$.result.address.postalAddress.regionCode');
SET @MapURL = 'https://www.google.com/maps/search/?api=1&query=' + ISNULL(CAST(@GPSLatitude AS VARCHAR(20)), '') + ',' + ISNULL(CAST(@GPSLongitude AS VARCHAR(20)), '');
-- Return the parsed data
SELECT
@GPSLatitude AS GPSLatitude,
@GPSLongitude AS GPSLongitude,
@City AS City,
@State AS [State],
@PostalCode AS PostalCode,
@Address AS [Address],
@MapURL AS MapURL
END