[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: REST-JSON-YAJL



   Hi T Crook,
   Here you go. I use the feature in Scott's JSON to retrieve in-memory
   rather than to IFS, makes it a bit harder to debug but once its going,
   I like it better.
   This program validates address' using SmartStreets web service.�   Regards
   Chris

   On Mon, Apr 25, 2016 at 3:27 PM, T Crook <[1]tcrook57@xxxxxxxxx> wrote:

     Hi Chris,
     �I attempted to get the JSON part going last year for a credit card
     application, never got it going and used their own file layout.
     Would you mind sharing it with me so I can see where I went wrong?
     Thanks a ton in advance!
     T Crook
     -----Original Message----- From: Chris Edmondson
     Sent: Monday, April 25, 2016 6:09 PM
     To: HTTPAPI and FTPAPI Projects
     Subject: Re: REST-JSON-YAJL

   I don't have the REST part, but I can share the rest :) Mine connects
   to a
   web service which returns JSON, processed using YAJL. Happy to share it
   if
   useful to you.
   On Mon, Apr 25, 2016 at 12:48 PM, Tony Cesarios
   <[2]tony@xxxxxxxxxxxxxxxxxxx>
   wrote:

     � � [cid:image001.gif@01D19F7F.48A941D0]
     � �Hi,
     � �Just wondering if anyone has a sample program using HTTPAPI to
     execute
     � �a GET from an external URL using REST and JSON.
     � �Had a look at YAJL as a solution.
     � �We need to go and retrieve orders which contain about 15
     elements
     � �(parameters) and process them into our iSeries.
     � �Regards,
     � �Tony Cesarios
     --------------------------------------------------------------------
     ---
     This is the FTPAPI mailing list.� To unsubscribe, please go to:
     [3]http://www.scottklement.com/mailman/listinfo/ftpapi
     --------------------------------------------------------------------
     ---

   --
   Chris Edmondson
   [4]509.670.6817
   -----------------------------------------------------------------------
   This is the FTPAPI mailing list.� To unsubscribe, please go to:
   [5]http://www.scottklement.com/mailman/listinfo/ftpapi
   -----------------------------------------------------------------------
   -----------------------------------------------------------------------
   This is the FTPAPI mailing list.� To unsubscribe, please go to:
   [6]http://www.scottklement.com/mailman/listinfo/ftpapi
   -----------------------------------------------------------------------

   --
   Chris Edmondson
   509.670.6817

References

   1. mailto:tcrook57@xxxxxxxxx
   2. mailto:tony@xxxxxxxxxxxxxxxxxxx
   3. http://www.scottklement.com/mailman/listinfo/ftpapi
   4. tel:509.670.6817
   5. http://www.scottklement.com/mailman/listinfo/ftpapi
   6. http://www.scottklement.com/mailman/listinfo/ftpapi
SRCSEQ,SRCDAT,SRCDTA
1,150921,     H DFTACTGRP(*NO)
2,150921,     H ACTGRP('YAJL')
3,150921,     H BNDDIR('LIBHTTP/HTTPAPI': 'YAJL/YAJL': 'QC2LE')
4,90605,     H option(*nodebugio: *srcstmt)
5,41103,      *
6,150906,      * validate address at smartystreets
7,41103,      *
8,90605,      /define WEBFORMS
9,150920,      /define YAJL_C_PROTOTYPES
10,90605,      /copy qrpglesrc;httpapi_h
11,90605,      /copy qrpglesrc;ifsio_h
12,150920,      /copy qrpglesrc;errno_h
13,150920,      /copy qrpglesrc;yajl_h
14,11021,
15,90605,     d Form            s                   like(WEBFORM)
16,90605,     d url             s          32767a   varying
17,90605,     d urlLen          s             10i 0
18,90605,     d rc              s             10I 0
19,90605,     d
20,90605,     d adrs            s             30a
21,150905,     d adrs2           s             30a
22,90605,     d city            s             25a
23,90605,     d state           s              4a
24,90605,     d zip             s              5a
25,150920,     d
26,150920,     d vaad1           s             40a
27,150920,     d vaad2           s             40a
28,150920,     d vacty           s             28a
29,150920,     d vacnt           s             30a
30,150920,     d vaste           s              2a
31,150920,     d vazip           s             10a
32,150920,     d valat           s             11p 8
33,150920,     d valon           s             11p 8
34,150920,     d vaerr           s             80a
35,150920,     d
36,150920,     d dpvMatch        s              1a
37,150920,     d dpvCodes        s             32a
38,150920,     d
39,150920,     d i               s             10i 0
40,150920,     d j               s             10i 0
41,150920,     d k               s             10i 0
42,150910,     d
43,150920,     d docNode         s                   like(yajl_val)
44,150920,     d node1           s                   like(yajl_val)
45,150920,     d node2           s                   like(yajl_val)
46,150920,     d val             s                   like(yajl_val)
47,150920,     d key             s             50a   varying
48,150920,     d val2            s                   like(yajl_val)
49,150920,     d key2            s             50a   varying
50,150920,     d errMsg          s            500a   varying
51,150920,
52,150920,     d* save http raw value returned from webservice
53,150920,     d http_raw_save   PR            10I 0
54,150920,     d   peFD                        10I 0 value
55,150920,     d   peData                        *   value
56,150920,     d   peLen                       10I 0 value
57,150920,     d  rcvDta         s          32766a   varying
58,150920,     d
59,150920,     d fd              s             10I 0 inz(-1)
60,150920,     d msg             s             52A
61,150920,     d bytes           s             16P 0
62,150920,     d
63,150920,     d dbg             s              1n
64,150920,     d
65,150920,     d* add custom HTTP header for standardize-only
66,150920,     d Add_StndzOnly   PR
67,150920,     d   Header                    1024A   varying
68,150920,     d   UserData                      *   value
69,150920,     d
70,150920,     d pgmsts        esds
71,150920,     d
72,61030,
73,90605,     c     *entry        plist
74,90605,     c                   parm                    adrs
75,150905,     c                   parm                    adrs2
76,90605,     c                   parm                    city
77,90605,     c                   parm                    state
78,90605,     c                   parm                    zip
79,150920,     c                   parm                    vaad1
80,150920,     c                   parm                    vaad2
81,150920,     c                   parm                    vacty
82,150920,     c                   parm                    vacnt
83,150920,     c                   parm                    vaste
84,150920,     c                   parm                    vazip
85,150920,     c                   parm                    valat
86,150920,     c                   parm                    valon
87,150920,     c                   parm                    vaerr
88,150920,     c                   eval      *inlr    = *on
89,150920,     c                   eval      vaad1    = *blanks
90,150920,     c                   eval      vaad2    = *blanks
91,150920,     c                   eval      vacty    = *blanks
92,150920,     c                   eval      vacnt    = *blanks
93,150920,     c                   eval      vaste    = *blanks
94,150920,     c                   eval      vazip    = *blanks
95,150920,     c                   eval      valat    = 0
96,150920,     c                   eval      valon    = 0
97,150920,     c                   eval      vaerr    = *blanks
98,90605,     c* turn on debug
99,150920,     c                   callp     http_debug(*ON)
100,90605,     c
101,90605,     c* build url
102,150905,      /free
103,150905,       url  = 'https://api.smartystreets.com/street-address'
104,150905,          + '?auth-id=xxxxxxx'
105,150908,          + '&auth-token=xxxxxxx';
106,150908,          Form = WEBFORM_open();
107,150905,          WEBFORM_setVar(Form : 'street'      : %trim(adrs));
108,150907,          WEBFORM_setVar(Form : 'secondary'   : %trim(adrs2));
109,150905,          WEBFORM_setVar(Form : 'city'        : %trim(city));
110,150920,          WEBFORM_setVar(Form : 'state'       : %trim(state));
111,150905,          WEBFORM_setVar(Form : 'zipcode'     : %trim(zip));
112,90608,
113,90629,          url = url + WEBFORM_getData(Form);
114,90608,
115,90605,          WEBFORM_Close(Form);
116,150910,
117,150920,          // add standardize-only header
118,150910,          http_xproc( HTTP_POINT_ADDL_HEADER
119,150910,                            : %paddr(Add_StndzOnly) );
120,150920,          // validate address
121,150920,          rc = http_url_get_raw(url: fd: %paddr(HTTP_RAW_SAVE));
122,150920,          if rc <> 1;
123,150920,           vaerr = 'Y*Fatal http GET error: see debug log';
124,150920,           return;
125,150920,          endif;
126,150920,
127,150920,          // ascii to ebcdic
128,150920,          http_xlateP(%len(rcvDta): %addr(rcvDta) + 2: TO_EBCDIC);
129,150920,
130,150920,          // if smartysteets returns []; address cannot be validated
131,150923,          if %len(rcvDta) < 2;
132,150923,           vaerr     = 'Y*Address cannot be validated';
133,150923,           return;
134,150923,          endif;
135,150923,          if %subst(rcvDta: 1: 2) = '[]';
136,150920,           vaerr     = 'Y*Address cannot be validated';
137,150920,           return;
138,150920,          endif;
139,150920,
140,150920,          // process JSON response
141,150920,          docNode = yajl_buf_load_tree(%addr(rcvDta) + 2:
142,150920,                                       %len(rcvDta): errMsg);
143,150920,          if errMsg <> '';
144,150920,           vaerr     = 'Y*Address cannot be validated';
145,150920,           return;
146,150920,          endif;
147,150920,
148,150920,          // loop through outer array
149,150920,          i = 0;
150,150920,          dow YAJL_ARRAY_LOOP( docNode: i: node1 );
151,150920,           // loop through inner objects
152,150920,           j = 0;
153,150920,           dow YAJL_OBJECT_LOOP( node1: j: key: val);
154,150920,            select;
155,150920,            when key = 'delivery_line_1';
156,150920,             vaad1 = yajl_get_string(val);
157,150920,            when key = 'delivery_line_2';
158,150920,             vaad2 = yajl_get_string(val);
159,150920,            when key = 'components';
160,150920,             // loop through address component values
161,150920,             node2 = YAJL_object_elem(node1: j: key);
162,150920,             k = 0;
163,150920,             dow YAJL_OBJECT_LOOP( node2: k: key2: val2);
164,150920,              select;
165,150920,              when key2 = 'city_name';
166,150920,               vacty = yajl_get_string(val2);
167,150920,              when key2 = 'state_abbreviation';
168,150920,               vaste = yajl_get_string(val2);
169,150920,              when key2 = 'zipcode';
170,150920,               vazip = yajl_get_string(val2);
171,150920,              when key2 = 'plus4_code';
172,150920,               vazip = %trim(vazip) + '-' +
173,150920,                              yajl_get_string(val2);
174,150920,              endsl;
175,150920,             enddo;
176,150921,             // yajl_tree_free(node2);
177,150920,            when key = 'metadata';
178,150920,             // loop through address metadata values
179,150920,             node2 = YAJL_object_elem(node1: j: key);
180,150920,             k = 0;
181,150920,             dow YAJL_OBJECT_LOOP( node2: k: key2: val2);
182,150920,              select;
183,150920,              when key2 = 'latitude';
184,150920,               valat = yajl_get_number(val2);
185,150920,              when key2 = 'longitude';
186,150920,               valon = yajl_get_number(val2);
187,160118,              when key2 = 'county_name';
188,160118,               vacnt = yajl_get_string(val2);
189,150920,              endsl;
190,150920,             enddo;
191,150921,             // yajl_tree_free(node2);
192,150920,            when key = 'analysis';
193,150920,             // loop through address analysis
194,150920,             node2 = YAJL_object_elem(node1: j: key);
195,150920,             k = 0;
196,150920,             dow YAJL_OBJECT_LOOP( node2: k: key2: val2);
197,150920,              select;
198,150920,              when key2 = 'dpv_match_code';
199,150920,               dpvMatch = yajl_get_string(val2);
200,150920,              when key2 = 'dpv_footnotes';
201,150920,               dpvCodes = yajl_get_string(val2);
202,150920,              endsl;
203,150920,             enddo;
204,150921,             // yajl_tree_free(node2);
205,150920,            endsl;
206,150920,           enddo;
207,150921,           // yajl_tree_free(node1);
208,150920,          enddo;
209,150921,          yajl_tree_free(docNode);
210,150920,
211,150920,          vaerr  = 'N' + dpvMatch + dpvCodes;
212,150920,
213,90605,        return;
214,90605,
215,90605,      /end-free
216,90605,
217,150920,
218,150920,      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
219,150920,      *   This is called by the HTTP_url_get_raw() API each time
220,150920,      *   data is received from the HTTP server.
221,150920,      *
222,150920,      *   We use it to show the user how many bytes have been
223,150920,      *   received; and then we write the received data to disk.
224,150920,      *
225,150920,      *   Parameters:
226,150920,      *        peFD = number that we passed to HTTP_url_get_raw().
227,150920,      *               (We use the file descriptor that we got when
228,150920,      *                we opened the stream file that we're saving to)
229,150920,      *      peData = pointer to the data that was received from http
230,150920,      *                server.
231,150920,      *       peLen = length of data received
232,150920,      *
233,150920,      *   we return the length we received from HTTPAPI if successful
234,150920,      *     or -1 if we can't write the data to disk.
235,150920,      *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
236,150920,     P http_raw_save   B
237,150920,     D http_raw_save   PI            10I 0
238,150920,     D   peFD                        10I 0 value
239,150920,     D   peData                        *   value
240,150920,     D   peLen                       10I 0 value
241,150920,     d
242,150920,     d peDta           s          32000a   based(peData)
243,150920,
244,150920,     D QMHSNDPM        PR                  ExtPgm('QMHSNDPM')
245,150920,     D   MessageID                    7A   Const
246,150920,     D   QualMsgF                    20A   Const
247,150920,     D   MsgData                    256A   Const
248,150920,     D   MsgDtaLen                   10I 0 Const
249,150920,     D   MsgType                     10A   Const
250,150920,     D   CallStkEnt                  10A   Const
251,150920,     D   CallStkCnt                  10I 0 Const
252,150920,     D   MessageKey                   4A
253,150920,     D   ErrorCode                32766A   options(*varsize)
254,150920,
255,150920,     D dsEC            DS
256,150920,     D  dsECBytesP             1      4I 0 inz(0)
257,150920,     D  dsECBytesA             5      8I 0 inz(0)
258,150920,
259,150920,     D wwMsgKey        S              4A
260,150920,     D wwText          s            100A   varying
261,150920,
262,150920,     c                   eval      Bytes = Bytes + peLen
263,150920,     c                   eval      wwText = %trim(%editc(Bytes:'P')) +
264,150920,     c                                ' bytes received'
265,150920,
266,150920,     c                   if        peFd <> -1
267,150920,     c                   if        write(peFd: peData: peLen) < 1
268,150920,     c                   return    -1
269,150920,     c                   endif
270,150920,     c                   else
271,150920,     c                   eval      rcvDta = rcvDta +
272,150920,     c                                      %subst(peDta: 1: peLen)
273,150920,     c                   endif
274,150920,
275,150920,     c                   return    peLen
276,150920,     P                 E
277,150920,
278,150910,      * HTTPAPI will call this (because we set it with http_xproc)
279,150910,      * just before sending the HTTP headers to the remote server.
280,150910,      * This procedure lets us add any header we like to the
281,150910,      * HTTP request.
282,150910,      *
283,150910,      * In this example; I'll use it to supply the X-Standardize-Only
284,150910,      * header.
285,150910,      *
286,150910,     P Add_StndzOnly   B
287,150910,     D Add_StndzOnly   PI
288,150910,     D   Header                    1024A   varying
289,150910,     D   UserData                      *   value
290,150910,      /free
291,150910,               Header = 'X-standardize-only: ' + 'true ' + x'0d25';
292,150910,      /end-free
293,150910,     P                 E
294,150920,
295,150920,
296,150920,      /define ERRNO_LOAD_PROCEDURE
297,150920,      /copy qrpglesrc;errno_h
,,
-----------------------------------------------------------------------
This is the FTPAPI mailing list.  To unsubscribe, please go to:
http://www.scottklement.com/mailman/listinfo/ftpapi
-----------------------------------------------------------------------