Last active
July 21, 2021 15:51
-
-
Save jweisman/ae25df88e1e6c517ba8d9608ca009ac9 to your computer and use it in GitHub Desktop.
Swagger Codegen with Alma APIs
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
import java.time.LocalDate; | |
import java.time.LocalDateTime; | |
import java.time.format.DateTimeFormatter; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; | |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; | |
import io.swagger.client.ApiClient; | |
import io.swagger.client.ApiException; | |
import io.swagger.client.Configuration; | |
import io.swagger.client.api.RequestsApi; | |
import io.swagger.client.auth.ApiKeyAuth; | |
import io.swagger.client.model.*; | |
import io.swagger.client.model.UserRequest.*; | |
public class SwaggerClientTest { | |
public static void main(String[] args) { | |
ApiClient defaultClient = Configuration.getDefaultApiClient(); | |
// Configure API key authorization: ApiKeyAuth | |
ApiKeyAuth ApiKeyAuth = (ApiKeyAuth) defaultClient.getAuthentication("ApiKeyAuth"); | |
ApiKeyAuth.setApiKey(System.getenv("ALMA_APIKEY")); | |
// Configure date serializers | |
RequestsApi apiInstance = new RequestsApi(); | |
JavaTimeModule module = new JavaTimeModule(); | |
module.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ISO_OFFSET_DATE_TIME)); | |
module.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ISO_OFFSET_DATE)); | |
ObjectMapper om = new ObjectMapper().registerModule(module); | |
/* GET USER REQUESTS */ | |
if (args.length<2) { | |
System.out.println("Missing parameters: user_id mms_id"); | |
System.exit(1); | |
} | |
String userId = args[0]; | |
Integer limit = 5; | |
String requestType = "HOLD"; | |
try { | |
Object result = apiInstance.getalmawsv1usersuserIdrequests(userId, requestType, null, limit, null, null); | |
System.out.println(result); | |
} catch (ApiException e) { | |
System.err.println("Exception when calling RequestsApi#getalmawsv1usersuserIdrequests"); | |
e.printStackTrace(); | |
} | |
/* CREATE USER REQUEST */ | |
UserRequest userRequest = new UserRequest(); | |
userRequest.setRequestType(RequestTypeEnum.HOLD); | |
userRequest.setPickupLocationType(PickupLocationTypeEnum.LIBRARY); | |
userRequest.setPickupLocationLibrary("MAIN"); | |
String mmsId = args[1]; | |
String requestId = ""; | |
try { | |
Object result = apiInstance.postalmawsv1usersuserIdrequests(om.writeValueAsString(userRequest), mmsId, "", userId, null); | |
UserRequest2 request = om.convertValue(result, UserRequest2.class); | |
System.out.println(String.format("Created Request PID %s", request.getRequestId())); | |
requestId = request.getRequestId(); | |
} catch (ApiException | JsonProcessingException e) { | |
System.err.println("Exception when calling RequestsApi#postalmawsv1usersuserIdrequests"); | |
e.printStackTrace(); | |
} | |
/* CANCEL USER REQUEST */ | |
String reason = "PatronNotInterested"; | |
try { | |
apiInstance.deletealmawsv1usersuserIdrequestsrequestId(userId, requestId, reason, null, null); | |
System.out.println("Request cancelled"); | |
} catch (ApiException e) { | |
System.err.println("Exception when calling RequestsApi#deletealmawsv1usersuserIdrequestsrequestId"); | |
e.printStackTrace(); | |
} | |
} | |
} |
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 | |
require_once(__DIR__ . '/php-client-generated/SwaggerClient-php/vendor/autoload.php'); | |
// Configure API key authorization: ApiKeyAuth | |
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('apikey', $_ENV['ALMA_APIKEY']); | |
$requestsApi = new Swagger\Client\Api\RequestsApi( | |
new GuzzleHttp\Client(), | |
$config | |
); | |
/* GET USER REQUESTS */ | |
if (count($argv) < 3) exit("Usage: php SwaggerClientTest.php user_id mms_id\n"); | |
$user_id = $argv[1]; | |
$limit = 5; | |
$request_type = 'HOLD'; | |
try { | |
$result = $requestsApi->getalmawsv1usersuserIdrequests($user_id, $request_type, $user_id_type, $limit, $offset, $status); | |
print_r($result); | |
} catch (Exception $e) { | |
echo 'Exception when calling DepositsApi->getalmawsv1usersuserIddeposits: ', $e->getMessage(), PHP_EOL; | |
} | |
/* CREATE USER REQUEST */ | |
$data = array( | |
"request_type" => 'HOLD', | |
"pickup_location_type" => 'LIBRARY', | |
"pickup_location_library" => 'MAIN' | |
); | |
$body =new Swagger\Client\Model\UserRequest($data); | |
$mms_id = $argv[2]; | |
$item_pid = ''; | |
try { | |
$result = $requestsApi->postalmawsv1usersuserIdrequests($body, $mms_id, $item_pid, $user_id, $user_id_type); | |
print_r($result); | |
} catch (Exception $e) { | |
echo 'Exception when calling RequestsApi->postalmawsv1usersuserIdrequests: ', $e->getMessage(), PHP_EOL; | |
} | |
/* GET USER REQUEST */ | |
$request_id = $result['request_id']; | |
try { | |
$result = $requestsApi->getalmawsv1usersuserIdrequestsrequestId($user_id, $request_id, $user_id_type); | |
print_r($result); | |
} catch (Exception $e) { | |
echo 'Exception when calling RequestsApi->getalmawsv1usersuserIdrequestsrequestId: ', $e->getMessage(), PHP_EOL; | |
} | |
/* CANCEL USER REQUEST */ | |
$reason = 'PatronNotInterested'; | |
try { | |
$requestsApi->deletealmawsv1usersuserIdrequestsrequestId($user_id, $request_id, $reason, $note, $notify_user); | |
echo "Request cancelled\n"; | |
} catch (Exception $e) { | |
echo 'Exception when calling RequestsApi->deletealmawsv1usersuserIdrequestsrequestId: ', $e->getMessage(), PHP_EOL; | |
} | |
?> |
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
from __future__ import print_function | |
import time | |
import swagger_client | |
from swagger_client.rest import ApiException | |
from pprint import pprint | |
import os | |
import sys | |
# Configure API key authorization: ApiKeyAuth | |
configuration = swagger_client.Configuration() | |
configuration.api_key['apikey'] = os.environ['ALMA_APIKEY'] | |
# create an instance of the API class | |
api_instance = swagger_client.RequestsApi(swagger_client.ApiClient(configuration)) | |
# GET USER REQUESTS | |
if len(sys.argv) < 3: | |
exit("Usage: python SwaggerTestClient.py user_id mms_id") | |
user_id = sys.argv[1] | |
limit = 5 | |
request_type = 'HOLD' | |
try: | |
# Requests by user id | |
api_response = api_instance.getalmawsv1usersuser_idrequests(user_id, request_type, limit=limit) | |
pprint(api_response) | |
except ApiException as e: | |
print("Exception when calling RequestsApi->getalmawsv1usersuser_idrequests: %s\n" % e) | |
# CREATE USER REQUEST | |
mms_id = sys.argv[2] | |
item_pid = '' | |
body = swagger_client.UserRequest(request_type='HOLD', pickup_location_type='LIBRARY', pickup_location_library='MAIN') | |
try: | |
# Create user request | |
api_response = api_instance.postalmawsv1usersuser_idrequests(body, mms_id, item_pid, user_id) | |
pprint(api_response) | |
except ApiException as e: | |
print("Exception when calling RequestsApi->postalmawsv1usersuser_idrequests: %s\n" % e) | |
# CANCEL USER REQUEST | |
request_id = api_response['request_id'] | |
reason = 'PatronNotInterested' | |
try: | |
# Cancel user request | |
api_instance.deletealmawsv1usersuser_idrequestsrequest_id(user_id, request_id, reason) | |
print("Request cancelled.") | |
except ApiException as e: | |
print("Exception when calling RequestsApi->deletealmawsv1usersuser_idrequestsrequest_id: %s\n" % e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Download the PHP, Java and Python codegen packages from the Developer Network, for examples Users. Unzip them.
For PHP:
For Java:
For Python:
Looks for an API Key in a
ALMA_APIKEY
environment variable. Don't forget to use a valid username and mms_id for your environment.