Last active
March 10, 2022 19:43
-
-
Save jweisman/671b179bb66b0bce78d6f85a7cfb9d7f to your computer and use it in GitHub Desktop.
Alma API Error Handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
URL=https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/fdsa | |
curl ${URL} -vs -H 'accept: application/json' -H "authorization: apikey $ALMA_APIKEY" | jq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install got | |
const got = require('got'); | |
got.get('https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/jsfdsa', | |
{ | |
headers: { | |
authorization: `apikey ${process.env.ALMA_APIKEY}` | |
}, | |
responseType: 'json' | |
} | |
) | |
.then(res => { | |
const users = res.body; | |
for(user of users.user) { | |
console.log(`Got user with id: ${user.primary_id}, name: ${user.first_name} ${user.last_name}`); | |
} | |
}) | |
.catch(err => { | |
const error = err.response.body.errorList?.error[0]; | |
console.log(error.errorMessage) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$curl = curl_init(); | |
$url = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/fdsa'; | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
'Authorization: apikey ' .$_ENV["ALMA_APIKEY"], | |
'Accept: application/json', | |
)); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl, CURLOPT_FAILONERROR, false); | |
$result = curl_exec($curl); | |
$httpCode = (int)curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
curl_close($curl); | |
$response = json_decode($result, true); | |
if ($httpCode != 200) { | |
$error = $response['errorList']['error'][0]; | |
print($error['errorMessage'] . "\n"); | |
} else { | |
$response = json_decode($result, true); | |
$count = $response['total_record_count']; | |
print("Count: " .$count ."\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python -m pip install requests | |
import requests, os, json | |
headers = { | |
'authorization': f'apikey {os.environ.get("ALMA_APIKEY")}', | |
'accept': 'application/json' | |
} | |
url = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/fdsa' | |
try: | |
r = requests.get(url, headers=headers) | |
r.raise_for_status() | |
print(r.json()) | |
except requests.exceptions.HTTPError as err: | |
error = json.loads(err.response.text)['errorList']['error'][0] | |
print(error['errorMessage']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# bundle install faraday faraday_middleware | |
require 'faraday' | |
require 'faraday_middleware' | |
require 'json' | |
connection = Faraday.new do |f| | |
f.authorization :apikey, ENV['ALMA_APIKEY'] | |
f.response :json | |
f.headers['accept'] = "application/json" | |
f.use Faraday::Response::RaiseError | |
end | |
begin | |
url = 'https://api-na.hosted.exlibrisgroup.com/almaws/v1/users/fdsa' | |
response = connection.get url | |
p response.body | |
rescue Faraday::Error => e | |
error = JSON.parse(e.response[:body])['errorList']['error'][0] | |
p error['errorMessage'] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment