Creating :query-params For clj-http get

Most of the U.S. mail our town sends out is barcoded. Before we can barcode an address, it must be verified by software recognized by the U.S. Post Office. This software, AccuMail (SmartSoftUsa) runs on Windows workstation or server. We have an ActiveX toolkit to accompany the base software. This means a Microsoft IIS web server running an ASP page can talk to the AccuMail address verification software using ActiveX. Such an ASP page has been working for years, and processes our addresses serially.

Given the tags expected by the ASP page, these are a def in the Clojure program:

(def accumail-url-keys ["CA", "STREET", "STREET2", "CITY", "STATE", "ZIP"] )

These tags merged with part of the input data forms a :query-params map used in a clj-http get call like this:

(client/get "http://site.com/search" {:query-params {"q" "foo, bar"}})

I needed a way to create a :query-params map for my data. Here is what I have so far. The :query-params just print out for now, but eventually they will be used in a clj-http get call to the AccuMail server.

Here is the input data from our receivables database:

1,266 BROADWAY APT 1,,NULLINGHAM,ZA,02474,11,900034,e
1,66 PLEASANT ST #2,,NULLINGHAM,ZA,02476,11,900035,e
1,254 FLORENCE AVE,,NULLINGHAM,ZA,02476,11,900036,e
1,75 ZARATHON ST,,NULLINGHAM,ZA,02474,11,900037,e
1,5 VIKING COURT,47,NULLINGHAM,ZA,02474,11,31879,e
1,94 FAIRMONT ST, ,NULLINGHAM,ZA,02474,11,31880,e
1,12 OLDHAM RD, ,NULLINGHAM,ZA,02474,11,31881,e
1,11 NEWPORT STREET,APT 1,NULLINGHAM,ZA,02476,11,31882,e
1,14A GROVE ST, ,NULLINGHAM,ZA,02476,11,31883,e
1,1 KENILWORTH RD, ,NULLINGHAM,ZA,02476,11,31884,e

Here’s the program:

(ns test-csv
  (:gen-class)
  (:use clojure.contrib.command-line)
  (:use clojure-csv.core))

(def accumail-url-keys ["CA", "STREET", "STREET2", "CITY", "STATE", "ZIP"] )

(defn gen-parsed-csv-file
    "Returns a sequence generated by parse-csv"
    [file-name]
    (let [data (slurp file-name)
     all-csv-rows (parse-csv data)]
      (lazy-seq (drop-last 1 all-csv-rows))))

(defn gen-qparams
    "Generates all q-parameters from accumail-url-keys and a lazy sequence returned from parse-csv"
    [all-csv-rows]
    (doseq [one-full-csv-row all-csv-rows]
        (let [accumail-csv-row (first (split-at 6 one-full-csv-row))
              q-param (zipmap accumail-url-keys accumail-csv-row)]
        (println q-param))))

(defn -main [& args]
  (with-command-line args
    "Get csv file name"
    [[file-name ".csv file name" "resultset.csv"]]
    [[file-name ".csv file name" 1]]
    (println "file-name:", file-name)
    (gen-qparams (gen-parsed-csv-file file-name))))

And, here is the output. To reiterate, eventually, this :query-params data will be passed to a client-get call.

file-name: resultset.csv
{ZIP 02474, STATE ZA, CITY NULLINGHAM, STREET2 , STREET 266 BROADWAY APT 1, CA 1}
{ZIP 02476, STATE ZA, CITY NULLINGHAM, STREET2 , STREET 66 PLEASANT ST #2, CA 1}
{ZIP 02476, STATE ZA, CITY NULLINGHAM, STREET2 , STREET 254 FLORENCE AVE, CA 1}
{ZIP 02474, STATE ZA, CITY NULLINGHAM, STREET2 , STREET 75 ZARATHON ST, CA 1}
{ZIP 02474, STATE ZA, CITY NULLINGHAM, STREET2 47, STREET 5 VIKING COURT, CA 1}
{ZIP 02474, STATE ZA, CITY NULLINGHAM, STREET2  , STREET 94 FAIRMONT ST, CA 1}
{ZIP 02474, STATE ZA, CITY NULLINGHAM, STREET2  , STREET 12 OLDHAM RD, CA 1}
{ZIP 02476, STATE ZA, CITY NULLINGHAM, STREET2 APT 1, STREET 11 NEWPORT STREET, CA 1}
{ZIP 02476, STATE ZA, CITY NULLINGHAM, STREET2  , STREET 14A GROVE ST, CA 1}
{ZIP 02476, STATE ZA, CITY NULLINGHAM, STREET2  , STREET 1 KENILWORTH RD, CA 1}

Edit: 7/21/2011 16:03
———————-

Here is a cleaned up version of gen-qparams

(defn gen-parsed-csv-file
"Returns a sequence generated by parse-csv"
[file-name]
(let [  data (slurp file-name)
all-csv-rows        (parse-csv data)
clean-csv-rows      (if (= 0 (count (first (last all-csv-rows))))
(drop-last 1 all-csv-rows)
all-csv-rows)]

clean-csv-rows))