Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd9a90e0

Browse files
committed
add error checking and some tests
1 parent52b6e0a commitd9a90e0

File tree

2 files changed

+92
-45
lines changed

2 files changed

+92
-45
lines changed

‎R/api.R‎

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ Petfinder <- function(key, secret) {
6262
for (typeintypes) {
6363
url<- paste0(private$host,'types/',type,sep='')
6464

65-
r<-httr::GET(url,
66-
httr::add_headers(Authorization= paste0('Bearer',
67-
private$auth,sep='')))
65+
r<-private$get_request(url=url)
6866

6967
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'))$type
7068
req_json$`_links`<-NULL
@@ -106,11 +104,10 @@ Petfinder <- function(key, secret) {
106104
for (typeinanimal_types) {
107105
url<- paste0(private$host,'types/',type,'/breeds',sep='')
108106

109-
r<-httr::GET(url,
110-
httr::add_headers(Authorization= paste0('Bearer',
111-
private$auth,sep='')))
107+
r<-private$get_request(url=url)
112108

113109
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'))$breeds$name
110+
114111
breeds_collection[[type]]<-req_json
115112

116113
}
@@ -163,9 +160,7 @@ Petfinder <- function(key, secret) {
163160
for (ani_idinanimal_id) {
164161
url<- paste0(private$host,'animals/',ani_id,sep='')
165162

166-
r<-httr::GET(url,
167-
httr::add_headers(Authorization= paste0('Bearer',
168-
private$auth,sep='')))
163+
r<-private$get_request(url=url)
169164

170165
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'),
171166
flatten=TRUE)$animal
@@ -199,10 +194,8 @@ Petfinder <- function(key, secret) {
199194
params['limit']=100
200195
params['page']=1
201196

202-
r<-httr::GET(url,
203-
query=params,
204-
httr::add_headers(Authorization= paste0('Bearer',
205-
private$auth,sep='')))
197+
r<-private$get_request(url=url,
198+
params=params)
206199

207200
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'),
208201
flatten=TRUE)
@@ -213,10 +206,8 @@ Petfinder <- function(key, secret) {
213206
for (pagein2:max_pages) {
214207
params['page']<-page
215208

216-
r<-httr::GET(url,
217-
query=params,
218-
httr::add_headers(Authorization= paste0('Bearer',
219-
private$auth,sep='')))
209+
r<-private$get_request(url=url,
210+
params=params)
220211

221212
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'),
222213
flatten=TRUE)
@@ -230,10 +221,8 @@ Petfinder <- function(key, secret) {
230221
else {
231222
params['page']=1
232223

233-
r<-httr::GET(url,
234-
query=params,
235-
httr::add_headers(Authorization= paste0('Bearer',
236-
private$auth,sep='')))
224+
r<-private$get_request(url=url,
225+
params=params)
237226

238227
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'))
239228

@@ -253,10 +242,8 @@ Petfinder <- function(key, secret) {
253242
for (pagein2:pages) {
254243
params['page']=page
255244

256-
r<-httr::GET(url,
257-
query=params,
258-
httr::add_headers(Authorization= paste0('Bearer',
259-
private$auth,sep='')))
245+
r<-private$get_request(url=url,
246+
params=params)
260247

261248
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'),
262249
flatten=TRUE)
@@ -296,9 +283,7 @@ Petfinder <- function(key, secret) {
296283
for (org_idinorganization_id) {
297284
url<- paste0(private$host,'organizations/',org_id,sep='')
298285

299-
r<-httr::GET(url,
300-
httr::add_headers(Authorization= paste0('Bearer',
301-
private$auth,sep='')))
286+
r<-private$get_request(url=url)
302287

303288
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'),
304289
flatten=TRUE)$organization
@@ -324,10 +309,8 @@ Petfinder <- function(key, secret) {
324309
params['limit']=100
325310
params['page']=1
326311

327-
r<-httr::GET(url,
328-
query=params,
329-
httr::add_headers(Authorization= paste0('Bearer',
330-
private$auth,sep='')))
312+
r<-private$get_request(url=url,
313+
params=params)
331314

332315
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'),
333316
flatten=TRUE)
@@ -337,10 +320,8 @@ Petfinder <- function(key, secret) {
337320
for (pagein2:max_pages) {
338321
params['page']=page
339322

340-
r<-httr::GET(url,
341-
query=params,
342-
httr::add_headers(Authorization= paste0('Bearer',
343-
private$auth,sep='')))
323+
r<-private$get_request(url=url,
324+
parms=params)
344325

345326
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'),
346327
flatten=TRUE)$organizations
@@ -353,10 +334,8 @@ Petfinder <- function(key, secret) {
353334
else {
354335
params['page']=1
355336

356-
r<-httr::GET(url,
357-
query=params,
358-
httr::add_headers(Authorization= paste0('Bearer',
359-
private$auth,sep='')))
337+
r<-private$get_request(url=url,
338+
params=params)
360339

361340
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'),
362341
flatten=TRUE)
@@ -377,10 +356,8 @@ Petfinder <- function(key, secret) {
377356
for (pagein2:pages) {
378357
params['page']=page
379358

380-
r<-httr::GET(url,
381-
query=params,
382-
httr::add_headers(Authorization= paste0('Bearer',
383-
private$auth,sep='')))
359+
r<-private$get_request(url=url,
360+
params=params)
384361

385362
req_json<-jsonlite::fromJSON(httr::content(r,as='text',encoding='utf-8'),
386363
flatten=TRUE)
@@ -414,7 +391,57 @@ Petfinder <- function(key, secret) {
414391
"client_secret"=private$secret)
415392
);
416393

417-
return(httr::content(req)$access_token)
394+
if (req$status_code==401) {
395+
stop('Provided credentials are invalid.')
396+
}
397+
else {
398+
return(httr::content(req)$access_token)
399+
}
400+
},
401+
402+
get_request=function(url,
403+
body,
404+
params=NULL) {
405+
406+
if (is.null(params)) {
407+
r<-httr::GET(url=url,
408+
httr::add_headers(Authorization= paste0('Bearer',private$auth,sep='')))
409+
}
410+
411+
else {
412+
r<-httr::GET(url=url,
413+
query=params,
414+
httr::add_headers(Authorization= paste0('Bearer',private$auth,sep='')))
415+
}
416+
417+
if (r$status_code==400) {
418+
print(httr::content(req)$`invalid-params`)
419+
stop(paste0('There are invalid parameters in the API query. Status Code:',
420+
r$status_code,
421+
sep=''))
422+
}
423+
424+
if (r$status_code==403) {
425+
stop(paste0(
426+
'Insufficient access to requested resource. This is typically the result of current Petfinder API access token becoming invalid. Status Code:',
427+
r$status_code,
428+
sep=''))
429+
}
430+
431+
if (r$status_code==404) {
432+
stop(paste0('The requested resource could not be found. Status Code:',
433+
r$status_code,
434+
sep=''))
435+
}
436+
437+
if (r$status_code==500) {
438+
stop(paste0('There was an unexpected error in the Petfinder API. Please try again. Status Code:',
439+
r$status_code,
440+
sep=''))
441+
}
442+
443+
return(r)
444+
418445
},
419446

420447
parameters=function(breed=NULL,

‎tests/testthat/test-errors.R‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
context('test error checking')
2+
3+
4+
test_that('invalid credentials entered returns appropriate error message', {
5+
skip_on_cran()
6+
7+
expect_error(Petfinder(key='test',secret= Sys.getenv('PETFINDER_SECRET_KEY')))
8+
})
9+
10+
11+
test_that('accessing resource with insufficient access returns appropriate message', {
12+
skip_on_cran()
13+
14+
pf<- Petfinder(key= Sys.getenv('PETFINDER_KEY'),
15+
secret= Sys.getenv('PETFINDER_SECRET_KEY'))
16+
17+
pf$.__enclos_env__$private$host<-"https://api.petfinder.com/v3/"
18+
19+
expect_error(pf$animal_types())
20+
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp