-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNallpac_API.R
More file actions
102 lines (87 loc) · 3.13 KB
/
Nallpac_API.R
File metadata and controls
102 lines (87 loc) · 3.13 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
# Loading packages
library(httr)
library(jsonlite)
library(openxlsx)
library(dplyr)
#Retrieve product Information from NALPAC website.
#User authentication
user<-'abc@xyzemail.com'
pw<-'************************'
#Product Requests
#Get product by sku
#https://api2.nalpac.com/Help/Api/GET-api-product-sku
#product = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/product/10006"));
retrieve_SKU_info<-function(sku=1006, user,pw){
final_url<-paste0('https://api2.nalpac.com/api/product/',sku)
product_info<-httr::GET(url=final_url,accept_json(),authenticate(user, pw))
return(product_info)
}
#Get product by upc
#https://api2.nalpac.com/Help/Api/GET-api-product_upc
#product2 = json_decode(CallAPI("GET", "https://api2.nalpac.com/api/product?upc=782631003079"));
retrieve_UPC_info<-function(upc=782631003079, user,pw){
final_url<-paste0('https://api2.nalpac.com/api/product?upc=',upc)
product_info<-httr::GET(url=final_url,accept_json(),authenticate(user, pw))
return(product_info)
}
retrieve_product_list<-function(ids,user, pw,search_type = 'upc'){
final_df<-data.frame()
missing_df<-data.frame(id=NA)
response<-list()
counter<-0
for(id in ids){
counter = counter+1
print(counter)
if(search_type == 'upc') response[[counter]]<-retrieve_UPC_info(as.integer(id),user,pw)
if(search_type == 'sku') response[[counter]]<-retrieve_SKU_info(as.integer(id),user,pw)
if(counter %% 100 == 0) saveRDS(response, 'response.RDS')
}
print('parsing...')
saveRDS(response, 'response.RDS')
for(l in 1:length(response)){
response2<-response[[l]]
print(l)
if(response2$status_code == 200){
raised <- content(response2, as="text")
parsed<- fromJSON(raised)
df<-data.frame(0)
for(i in 1:length(parsed)){
#print(i)
if(class(parsed[[i]]) == 'data.frame'){
tempdf<-parsed[[i]]
tempdf2<-data.frame()
for(j in 1:ncol(tempdf)){
tempdf2[1,j]<-paste(tempdf[,j], sep='; ',collapse = '; ')
}
names(tempdf2)<-paste0(names(parsed)[[i]],'_',names(tempdf))
df<-cbind(df,tempdf2)
} else {
tempdf<-data.frame(attribute = 'value')
if(is.null(parsed[[i]])) parsed[[i]]<-''
tempdf[1,1]<-parsed[[i]]
names(tempdf)<-names(parsed)[[i]]
df<-cbind(df,tempdf)
}
}
final_df<-bind_rows(final_df,df[2:ncol(df)])
}
else{
missing_df[nrow(missing_df)+1,'id']<-id
}
}
return(list(final_df,missing_df))
}
#input
product_list<-openxlsx::read.xlsx('products.xlsx')
skus<-na.omit(product_list$SKUs)
upcs<-as.numeric(na.omit(product_list$UPCs))
#output
catalogue_upc<-retrieve_product_list(upcs,user,pw,'upc')
catalogue_sku<-retrieve_product_list(skus,user,pw,'sku')
product_information<-bind_rows(catalogue_sku[[1]],catalogue_upc[[1]])
missing_products<-bind_rows(catalogue_sku[[2]],catalogue_upc[[2]])
catalogue<-list('Product Information' = product_information,
'Products Not Retrieved' = missing_products)
#Store results
saveRDS(catalogue,'catalogue.RDS')
openxlsx::write.xlsx(catalogue,paste0(Sys.time(),'_APIProductSearchResults.xlsx'))