Skip to content
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
35 changes: 17 additions & 18 deletions lib/authoritex/loc/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ defmodule Authoritex.LOC.Base do

@impl Authoritex
def search(query, max_results \\ 30) do
query_params = [{:q, query} | unquote(query_filter)]
path = [unquote(subauthority), "suggest2"] |> Enum.reject(&is_nil/1) |> Path.join()

request =
HttpClient.get(
"https://id.loc.gov/search/",
[],
params: query_params ++ [count: max_results, format: "xml+atom"]
"https://id.loc.gov/#{path}",
[accept: "application/ld+json"],
params: [q: query, count: max_results, searchtype: "keyword"]
)
|> autoretry()

Expand Down Expand Up @@ -117,24 +117,23 @@ defmodule Authoritex.LOC.Base do
defp parse_fetch_result(%{status_code: status_code}), do: {:error, status_code}

defp parse_search_result(%{body: response, status_code: 200}) do
with doc <- SweetXml.parse(response) do
case doc |> SweetXml.xpath(~x"/feed") do
nil ->
{:error, {:bad_response, response}}

feed ->
{:ok,
SweetXml.xpath(feed, ~x"//entry"l,
id: ~x{./link[@rel="alternate"][not(@type)]/@href}s,
label: ~x"./title/text()"s,
hint: ~x"./no_hint/text()"
)}
end
end
Jason.decode!(response, keys: :atoms)
|> parse_search_result()
rescue
_ -> {:error, {:bad_response, response}}
end

defp parse_search_result(%{count: count, hits: hits}) do
{:ok,
Enum.map(hits, fn hit ->
%{
id: hit.uri,
label: hit.aLabel,
hint: nil
}
end)}
end

defp parse_search_result(%{status_code: status_code}), do: {:error, status_code}
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/custom_cassettes/lcbase_500.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"request": {
"url": "https://id.loc.gov/search/?q=Authority+Down&q=scheme%3Ahttp%3A%2F%2Fid.loc.gov%2Fauthorities%2FgenreForms&count=30&format=xml%2Batom"
"url": "https://id.loc.gov/authorities/genreForms/suggest2?q=Authority+Down&count=30&searchtype=keyword"
},
"response": {
"status_code": 500,
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/custom_cassettes/lcbase_bad_200.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"request": {
"url": "https://id.loc.gov/search/?q=Authority+Busted&q=scheme%3Ahttp%3A%2F%2Fid.loc.gov%2Fauthorities%2FgenreForms&count=30&format=xml%2Batom"
"url": "https://id.loc.gov/authorities/genreForms/suggest2?q=Authority+Busted&count=30&searchtype=keyword"
},
"response": {
"status_code": 200,
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/vcr_cassettes/authoritex_fetch_failure.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions test/fixtures/vcr_cassettes/authoritex_fetch_success.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
"body": "",
"url": "https://id.loc.gov/authorities/names/no2011087251.rdf",
"headers": {
"User-Agent": "Authoritex/1.2.1 (https://github.com/nulib/authoritex; contact: [email protected]) httpoison/1.8.2 hackney/1.23.0"
"User-Agent": "Authoritex/1.2.2 (https://github.com/nulib/authoritex; contact: [email protected]) httpoison/1.8.2 hackney/1.23.0"
},
"method": "get",
"request_body": ""
},
"response": {
"binary": false,
"type": "ok",
"body": "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:bf=\"http://id.loc.gov/ontologies/bibframe/\" xmlns:bflc=\"http://id.loc.gov/ontologies/bflc/\" xmlns:owl=\"http://www.w3.org/2002/07/owl#\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">\n <madsrdf:PersonalName rdf:about=\"http://id.loc.gov/authorities/names/no2011087251\" xmlns:madsrdf=\"http://www.loc.gov/mads/rdf/v1#\">\n <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#Authority\"/>\n <madsrdf:authoritativeLabel>Valim, Jose</madsrdf:authoritativeLabel>\n <madsrdf:elementList rdf:parseType=\"Collection\">\n <madsrdf:FullNameElement>\n\t<madsrdf:elementValue>Valim, Jose</madsrdf:elementValue>\n </madsrdf:FullNameElement>\n </madsrdf:elementList>\n <bflc:marcKey>1001 $aValim, Jose</bflc:marcKey>\n <madsrdf:isMemberOfMADSCollection rdf:resource=\"http://id.loc.gov/authorities/names/collection_NamesAuthorizedHeadings\"/>\n <madsrdf:isMemberOfMADSCollection rdf:resource=\"http://id.loc.gov/authorities/names/collection_LCNAF\"/>\n <madsrdf:hasExactExternalAuthority rdf:resource=\"http://viaf.org/viaf/sourceID/LC%7Cno2011087251#skos:Concept\"/>\n <madsrdf:identifiesRWO>\n <madsrdf:RWO rdf:about=\"http://id.loc.gov/rwo/agents/no2011087251\">\n\t<rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/Person\"/>\n\t<rdf:type rdf:resource=\"http://xmlns.com/foaf/0.1/Person\"/>\n\t<rdfs:label>Valim, Jose</rdfs:label>\n\t<madsrdf:isIdentifiedByAuthority rdf:resource=\"http://id.loc.gov/authorities/names/no2011087251\"/>\n\t<bflc:contributorTo>\n\t <bf:Work rdf:about=\"http://id.loc.gov/resources/works/23201596\">\n\t <bflc:aap>McCord, Chris (Computer programmer). Programming Phoenix 1.4</bflc:aap>\n\t </bf:Work>\n\t</bflc:contributorTo>\n\t<bflc:contributorTo>\n\t <bf:Work rdf:about=\"http://id.loc.gov/resources/works/19492088\">\n\t <bflc:aap>Valim, Jose. Crafting Rails 4 applications</bflc:aap>\n\t </bf:Work>\n\t</bflc:contributorTo>\n\t<bflc:contributorTo>\n\t <bf:Work rdf:about=\"http://id.loc.gov/resources/works/17032688\">\n\t <bflc:aap>Valim, Jose. Crafting rails applications</bflc:aap>\n\t </bf:Work>\n\t</bflc:contributorTo>\n </madsrdf:RWO>\n </madsrdf:identifiesRWO>\n <madsrdf:identifiesRWO rdf:resource=\"https://d-nb.info/gnd/1106268318\"/>\n <madsrdf:identifiesRWO rdf:resource=\"https://isni.org/isni/0000000120491044\"/>\n <madsrdf:isMemberOfMADSScheme rdf:resource=\"http://id.loc.gov/authorities/names\"/>\n <identifiers:lccn xmlns:identifiers=\"http://id.loc.gov/vocabulary/identifiers/\">no2011087251</identifiers:lccn>\n <identifiers:local xmlns:identifiers=\"http://id.loc.gov/vocabulary/identifiers/\">(OCoLC)oca08877947</identifiers:local>\n <madsrdf:hasSource>\n <madsrdf:Source>\n\t<madsrdf:citationStatus>found</madsrdf:citationStatus>\n\t<madsrdf:citationSource>Crafting Rails applications, c2011:</madsrdf:citationSource>\n\t<madsrdf:citationNote>t.p. (Jose Valim)</madsrdf:citationNote>\n </madsrdf:Source>\n </madsrdf:hasSource>\n <madsrdf:adminMetadata>\n <ri:RecordInfo xmlns:ri=\"http://id.loc.gov/ontologies/RecordInfo#\">\n\t<ri:recordChangeDate rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2011-06-06T00:00:00</ri:recordChangeDate>\n\t<ri:recordStatus rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">new</ri:recordStatus>\n\t<ri:recordContentSource rdf:resource=\"http://id.loc.gov/vocabulary/organizations/oco\"/>\n\t<ri:languageOfCataloging rdf:resource=\"http://id.loc.gov/vocabulary/iso639-2/eng\"/>\n </ri:RecordInfo>\n </madsrdf:adminMetadata>\n <madsrdf:adminMetadata>\n <ri:RecordInfo xmlns:ri=\"http://id.loc.gov/ontologies/RecordInfo#\">\n\t<ri:recordChangeDate rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2011-06-07T06:21:47</ri:recordChangeDate>\n\t<ri:recordStatus rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">revised</ri:recordStatus>\n\t<ri:recordContentSource rdf:resource=\"http://id.loc.gov/vocabulary/organizations/oco\"/>\n\t<ri:languageOfCataloging rdf:resource=\"http://id.loc.gov/vocabulary/iso639-2/eng\"/>\n </ri:RecordInfo>\n </madsrdf:adminMetadata>\n <rdf:type rdf:resource=\"http://www.w3.org/2004/02/skos/core#Concept\"/>\n <skos:prefLabel>Valim, Jose</skos:prefLabel>\n <skos:exactMatch rdf:resource=\"http://viaf.org/viaf/sourceID/LC%7Cno2011087251#skos:Concept\"/>\n <skos:inScheme rdf:resource=\"http://id.loc.gov/authorities/names\"/>\n <skos:changeNote>\n <cs:ChangeSet xmlns:cs=\"http://purl.org/vocab/changeset/schema#\">\n\t<cs:subjectOfChange rdf:resource=\"http://id.loc.gov/authorities/names/no2011087251\"/>\n\t<cs:creatorName rdf:resource=\"http://id.loc.gov/vocabulary/organizations/oco\"/>\n\t<cs:createdDate rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2011-06-06T00:00:00</cs:createdDate>\n\t<cs:changeReason rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">new</cs:changeReason>\n </cs:ChangeSet>\n </skos:changeNote>\n <skos:changeNote>\n <cs:ChangeSet xmlns:cs=\"http://purl.org/vocab/changeset/schema#\">\n\t<cs:subjectOfChange rdf:resource=\"http://id.loc.gov/authorities/names/no2011087251\"/>\n\t<cs:creatorName rdf:resource=\"http://id.loc.gov/vocabulary/organizations/oco\"/>\n\t<cs:createdDate rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2011-06-07T06:21:47</cs:createdDate>\n\t<cs:changeReason rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">revised</cs:changeReason>\n </cs:ChangeSet>\n </skos:changeNote>\n </madsrdf:PersonalName>\n</rdf:RDF>\n",
"body": "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:bf=\"http://id.loc.gov/ontologies/bibframe/\" xmlns:bflc=\"http://id.loc.gov/ontologies/bflc/\" xmlns:owl=\"http://www.w3.org/2002/07/owl#\" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\">\n <madsrdf:PersonalName rdf:about=\"http://id.loc.gov/authorities/names/no2011087251\" xmlns:madsrdf=\"http://www.loc.gov/mads/rdf/v1#\">\n <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#Authority\"/>\n <madsrdf:authoritativeLabel>Valim, Jose</madsrdf:authoritativeLabel>\n <madsrdf:elementList rdf:parseType=\"Collection\">\n <madsrdf:FullNameElement>\n\t<madsrdf:elementValue>Valim, Jose</madsrdf:elementValue>\n </madsrdf:FullNameElement>\n </madsrdf:elementList>\n <bflc:marcKey>1001 $aValim, Jose</bflc:marcKey>\n <madsrdf:identifiesRWO>\n <madsrdf:RWO rdf:about=\"http://id.loc.gov/rwo/agents/no2011087251\">\n\t<rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/Person\"/>\n\t<rdf:type rdf:resource=\"http://xmlns.com/foaf/0.1/Person\"/>\n\t<rdfs:label>Valim, Jose</rdfs:label>\n\t<madsrdf:isIdentifiedByAuthority>\n\t <madsrdf:PersonalName rdf:about=\"http://id.loc.gov/authorities/names/no2011087251\">\n\t <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#Authority\"/>\n\t <madsrdf:authoritativeLabel>Valim, Jose</madsrdf:authoritativeLabel>\n\t </madsrdf:PersonalName>\n\t</madsrdf:isIdentifiedByAuthority>\n </madsrdf:RWO>\n </madsrdf:identifiesRWO>\n <madsrdf:hasSource>\n <madsrdf:Source>\n\t<madsrdf:citationStatus>found</madsrdf:citationStatus>\n\t<madsrdf:citationSource>Crafting Rails applications, c2011:</madsrdf:citationSource>\n\t<madsrdf:citationNote>t.p. (Jose Valim)</madsrdf:citationNote>\n </madsrdf:Source>\n </madsrdf:hasSource>\n <identifiers:lccn xmlns:identifiers=\"http://id.loc.gov/vocabulary/identifiers/\">no2011087251</identifiers:lccn>\n <identifiers:local xmlns:identifiers=\"http://id.loc.gov/vocabulary/identifiers/\">(OCoLC)oca08877947</identifiers:local>\n <madsrdf:isMemberOfMADSCollection rdf:resource=\"http://id.loc.gov/authorities/names/collection_NamesAuthorizedHeadings\"/>\n <madsrdf:isMemberOfMADSScheme rdf:resource=\"http://id.loc.gov/authorities/names\"/>\n <madsrdf:isMemberOfMADSCollection rdf:resource=\"http://id.loc.gov/authorities/names/collection_LCNAF\"/>\n <madsrdf:hasExactExternalAuthority rdf:resource=\"http://viaf.org/viaf/sourceID/LC%7Cno2011087251#skos:Concept\"/>\n <madsrdf:identifiesRWO>\n <madsrdf:PersonalName rdf:about=\"https://d-nb.info/gnd/1106268318\">\n\t<rdfs:label>Valim, José</rdfs:label>\n\t<madsrdf:authoritativeLabel>Valim, José</madsrdf:authoritativeLabel>\n </madsrdf:PersonalName>\n </madsrdf:identifiesRWO>\n <madsrdf:identifiesRWO rdf:resource=\"https://isni.org/isni/0000000120491044\"/>\n <madsrdf:adminMetadata>\n <ri:RecordInfo xmlns:ri=\"http://id.loc.gov/ontologies/RecordInfo#\">\n\t<ri:recordChangeDate rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2011-06-06T00:00:00</ri:recordChangeDate>\n\t<ri:recordStatus rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">new</ri:recordStatus>\n\t<ri:recordContentSource>\n\t <madsrdf:CorporateName rdf:about=\"http://id.loc.gov/vocabulary/organizations/oco\">\n\t <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#Authority\"/>\n\t <madsrdf:authoritativeLabel>Columbus Public Library</madsrdf:authoritativeLabel>\n\t <madsrdf:code rdf:datatype=\"http://id.loc.gov/datatypes/orgs/code\">OCo</madsrdf:code>\n\t <madsrdf:code rdf:datatype=\"http://id.loc.gov/datatypes/orgs/normalized\">oco</madsrdf:code>\n\t <madsrdf:code rdf:datatype=\"http://id.loc.gov/datatypes/orgs/iso15511\">US-oco</madsrdf:code>\n\t </madsrdf:CorporateName>\n\t</ri:recordContentSource>\n\t<ri:languageOfCataloging>\n\t <madsrdf:Language rdf:about=\"http://id.loc.gov/vocabulary/iso639-2/eng\">\n\t <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#Authority\"/>\n\t <rdf:type rdf:resource=\"http://id.loc.gov/vocabulary/iso639-2/iso639-2_Language\"/>\n\t <madsrdf:authoritativeLabel xml:lang=\"en\">English</madsrdf:authoritativeLabel>\n\t <madsrdf:authoritativeLabel xml:lang=\"fr\">anglais</madsrdf:authoritativeLabel>\n\t <madsrdf:authoritativeLabel xml:lang=\"de\">Englisch</madsrdf:authoritativeLabel>\n\t <madsrdf:code rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">eng</madsrdf:code>\n\t </madsrdf:Language>\n\t</ri:languageOfCataloging>\n </ri:RecordInfo>\n </madsrdf:adminMetadata>\n <madsrdf:adminMetadata>\n <ri:RecordInfo xmlns:ri=\"http://id.loc.gov/ontologies/RecordInfo#\">\n\t<ri:recordChangeDate rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2011-06-07T06:21:47</ri:recordChangeDate>\n\t<ri:recordStatus rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">revised</ri:recordStatus>\n\t<ri:recordContentSource>\n\t <madsrdf:CorporateName rdf:about=\"http://id.loc.gov/vocabulary/organizations/oco\">\n\t <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#Authority\"/>\n\t <madsrdf:authoritativeLabel>Columbus Public Library</madsrdf:authoritativeLabel>\n\t <madsrdf:code rdf:datatype=\"http://id.loc.gov/datatypes/orgs/code\">OCo</madsrdf:code>\n\t <madsrdf:code rdf:datatype=\"http://id.loc.gov/datatypes/orgs/normalized\">oco</madsrdf:code>\n\t <madsrdf:code rdf:datatype=\"http://id.loc.gov/datatypes/orgs/iso15511\">US-oco</madsrdf:code>\n\t </madsrdf:CorporateName>\n\t</ri:recordContentSource>\n\t<ri:languageOfCataloging>\n\t <madsrdf:Language rdf:about=\"http://id.loc.gov/vocabulary/iso639-2/eng\">\n\t <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#Authority\"/>\n\t <rdf:type rdf:resource=\"http://id.loc.gov/vocabulary/iso639-2/iso639-2_Language\"/>\n\t <madsrdf:authoritativeLabel xml:lang=\"en\">English</madsrdf:authoritativeLabel>\n\t <madsrdf:authoritativeLabel xml:lang=\"fr\">anglais</madsrdf:authoritativeLabel>\n\t <madsrdf:authoritativeLabel xml:lang=\"de\">Englisch</madsrdf:authoritativeLabel>\n\t <madsrdf:code rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">eng</madsrdf:code>\n\t </madsrdf:Language>\n\t</ri:languageOfCataloging>\n </ri:RecordInfo>\n </madsrdf:adminMetadata>\n <rdf:type rdf:resource=\"http://www.w3.org/2004/02/skos/core#Concept\"/>\n <skos:prefLabel>Valim, Jose</skos:prefLabel>\n <skos:exactMatch rdf:resource=\"http://viaf.org/viaf/sourceID/LC%7Cno2011087251#skos:Concept\"/>\n <skos:inScheme rdf:resource=\"http://id.loc.gov/authorities/names\"/>\n <skos:changeNote>\n <cs:ChangeSet xmlns:cs=\"http://purl.org/vocab/changeset/schema#\">\n\t<cs:subjectOfChange rdf:resource=\"http://id.loc.gov/authorities/names/no2011087251\"/>\n\t<cs:creatorName>Columbus Public LibraryOCoocoUS-oco</cs:creatorName>\n\t<cs:createdDate rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2011-06-06T00:00:00</cs:createdDate>\n\t<cs:changeReason rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">new</cs:changeReason>\n </cs:ChangeSet>\n </skos:changeNote>\n <skos:changeNote>\n <cs:ChangeSet xmlns:cs=\"http://purl.org/vocab/changeset/schema#\">\n\t<cs:subjectOfChange rdf:resource=\"http://id.loc.gov/authorities/names/no2011087251\"/>\n\t<cs:creatorName>Columbus Public LibraryOCoocoUS-oco</cs:creatorName>\n\t<cs:createdDate rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\">2011-06-07T06:21:47</cs:createdDate>\n\t<cs:changeReason rdf:datatype=\"http://www.w3.org/2001/XMLSchema#string\">revised</cs:changeReason>\n </cs:ChangeSet>\n </skos:changeNote>\n </madsrdf:PersonalName>\n</rdf:RDF>\n",
"headers": {
"Date": "Tue, 20 May 2025 19:23:57 GMT",
"Date": "Tue, 05 Aug 2025 17:28:53 GMT",
"Content-Type": "application/rdf+xml; charset=UTF-8",
"Content-Length": "5588",
"Content-Length": "7586",
"Connection": "keep-alive",
"x-rwo-uri": "http://id.loc.gov/rwo/agents/no2011087251",
"x-frame-options": "DENY",
Expand All @@ -28,19 +28,19 @@
"access-control-allow-headers": "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With",
"access-control-expose-headers": "Content-Type, Cache-Control, X-URI, X-PrefLabel, X-PrefLabel-Escaped, X-PrefLabel-Encoded",
"strict-transport-security": "max-age=31536000",
"etag": "b6a59be270e16c6074b5cba461d88496",
"etag": "ab0ee107ba71fc18cb3f578c828bba03",
"x-preflabel": "Valim, Jose",
"x-preflabel-escaped": "Valim, Jose",
"x-preflabel-encoded": "Valim%2C%20Jose",
"x-uri": "http://id.loc.gov/authorities/names/no2011087251",
"Cache-Control": "public, max-age=2419200",
"x-varnish": "218860362",
"Age": "0",
"x-varnish": "275950552 288766965",
"Age": "4904",
"via": "1.1 varnish (Varnish/6.0)",
"accept-ranges": "bytes",
"cf-cache-status": "DYNAMIC",
"Server": "cloudflare",
"CF-RAY": "942e361f8dec123d-ORD"
"CF-RAY": "96a803755b3c8238-IAD"
},
"status_code": 200
}
Expand Down
40 changes: 21 additions & 19 deletions test/fixtures/vcr_cassettes/authoritex_search_count.json

Large diffs are not rendered by default.

Loading