-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaddressToZipCodeR.R
More file actions
60 lines (54 loc) · 2.04 KB
/
addressToZipCodeR.R
File metadata and controls
60 lines (54 loc) · 2.04 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
rm(list=ls())
gc()
directory = "~/Documents/Masters/data712/"
setwd(directory)
library(httr)
library(readr)
library(xml2)
#' @title zipcode_from_address()
#' @description Get zipcode from an address, using USPS Api
#' @section see link below for more details
#' https://www.usps.com/business/web-tools-apis/address-information-api.htm
#' @author Seth
#'
#' @param address_1 Delivery Address in the destination address. May contain
#' secondary unit designator, such as APT or SUITE, for Accountable mail).
#' Optional
#' @param address_2 Delivery Address in the destination address. Required.
#' @param city City name of the destination address. Optional
#' @param state Two-character state code of the destination address. Optional.
#' @param secret_key usps_key. get your own.
#' @returns a string
#'
#' @example Queens college # usps_key is the name of the file where my secret
#' key is stored. Get your own. `secret_key = read_file("usps_key")`
#' `zipcode_from_address(address_2="65-30 Kissena Blvd", city="Queens",
#' state="NY", secret_key=secrety_key)
#' @export
zipcode_from_address <- function(address_1="", address_2, city="", state="", secret_key){
queryString <- list('xml'= paste0(
"<ZipCodeLookupRequest USERID=\""
,secret_key
,"\"><Address><Address2>"
,address_2
,"</Address2><City>"
,city
,"</City><State>"
,state
,"NY</State></Address></ZipCodeLookupRequest>"
)
,'API' = "ZipCodeLookup"
)
# The url endpoint. Same for everyone.
url <- "http://production.shippingapis.com/ShippingAPI.dll"
# What is the encoding scheme
encode <- "raw"
response <- VERB("GET", url, query = queryString, content_type("application/xml"), encode = encode)
# Get zipcode in one line. convert binary to xml, get the xml node with zip5,
# get the output/value of the node
xml_text(xml_find_all(read_xml(response), ".//Zip5"))
}
# Example in action
secret_key = read_file("usps_key")
response = zipcode_from_address(address_2="65-30 Kissena Blvd", city="Queens", state="NY", secret_key = secret_key)
print(response)