Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GBIF throws 404 (not 410) for deleted resources #156

Merged
merged 3 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion GBIF/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GBIF"
uuid = "ee291a33-5a6c-5552-a3c8-0f29a1181037"
authors = ["Timothée Poisot <timothee.poisot@umontreal.ca>"]
version = "0.4.0"
version = "0.4.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
29 changes: 22 additions & 7 deletions GBIF/src/occurrence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,30 @@ function pairs_to_querystring(query::Pair...)
end

"""
occurrence(key::Union{String, Integer})
occurrence(key::String)

Returns a GBIF occurrence identified by a key. The key can be given as a string or as an integer.
Returns a GBIF occurrence identified by a key. The key can be given as a string
or as an integer (there is a second method for integer keys). In case the status
of the HTTP request is anything other than 200 (success), this function *will*
throw an error.
"""
function occurrence(key::Union{String, Integer})
occ_url = gbifurl * "occurrence/" * string(key)
occ_key_req = HTTP.get(occ_url)
result = JSON.parse(String(occ_key_req.body))
return GBIFRecord(result)
function occurrence(key::String)::GBIFRecord
occ_url = gbifurl * "occurrence/" * key
try
occ_key_req = HTTP.get(occ_url)
result = JSON.parse(String(occ_key_req.body))
return GBIFRecord(result)
catch err
if err isa HTTP.Exceptions.StatusError
throw("Occurrence $(key) (at $(occ_url)) cannot be accessed - error code: $(err.status)")
else
throw("Occurrence $(key) cannot be accessed")
end
end
end

function occurrence(key::Integer)::GBIFRecord
return occurrence(string(key))
end

function _internal_occurrences_getter(query::Pair...)
Expand Down
9 changes: 7 additions & 2 deletions GBIF/test/occurrence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ module TestOccurrence
using GBIF
using Test

k = 1258202889
# This occurrence exists
k = 3986160931
o = occurrence(k)
@test typeof(o) == GBIFRecord
@test o.key == k

# Piece of shit uncorrectly formatted occurence
# This occurrence has been deleted, so this needs some wrapping
k = 1258202889
@test_throws "cannot be accessed - error code" occurrence(k)

# This occurence is incorrectly formatted for some reason
k = 1039645472
o = occurrence(k)
@test typeof(o) == GBIFRecord
Expand Down