From 9b59aba5c573fd4c7b1e5d84250aaf1b0b336efe Mon Sep 17 00:00:00 2001 From: Helen Bailey Date: Thu, 23 Feb 2023 15:57:38 -0500 Subject: [PATCH] Add Alma API client Why these changes are being introduced: Initial functionality for this app involves interacting with the Alma Acquisitions API. How this addresses that need: * Adds alma module with AlmaClient class, including methods to retrieve the necessary data for credit card slips processing. * Adds Alma configuration function to config module. * Adds tests and fixtures to reflect changes. * Updates README to include new required and optional ENV variables. Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/IN-715 --- Pipfile | 3 + Pipfile.lock | 194 +++++++++++++++++++++++----- README.md | 14 +- ccslips/alma.py | 148 +++++++++++++++++++++ ccslips/config.py | 10 +- tests/conftest.py | 97 +++++++++++++- tests/fixtures/fund_records.json | 5 + tests/fixtures/po_line_records.json | 23 ++++ tests/test_alma.py | 63 +++++++++ tests/test_config.py | 19 ++- 10 files changed, 538 insertions(+), 38 deletions(-) create mode 100644 ccslips/alma.py create mode 100644 tests/fixtures/fund_records.json create mode 100644 tests/fixtures/po_line_records.json create mode 100644 tests/test_alma.py diff --git a/Pipfile b/Pipfile index b98d66c..e206331 100644 --- a/Pipfile +++ b/Pipfile @@ -5,6 +5,7 @@ name = "pypi" [packages] click = "*" +requests = "*" sentry-sdk = "*" [dev-packages] @@ -16,6 +17,8 @@ freezegun = "*" mypy = "*" pylama = {extras = ["all"], version = "*"} pytest = "*" +requests-mock = "*" +types-requests = "*" [requires] python_version = "3.11" diff --git a/Pipfile.lock b/Pipfile.lock index 534cf46..7f463af 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "61d2fd14efe70fda1d9bda997f61a3cd9376fe67c84e1c3dc331553c82a79d23" + "sha256": "32a3482a9638b2fc1bd0ed61586d6ce16e4a362809b39a54cbb39f3f592faf15" }, "pipfile-spec": 6, "requires": { @@ -24,6 +24,99 @@ "markers": "python_version >= '3.6'", "version": "==2022.12.7" }, + "charset-normalizer": { + "hashes": [ + "sha256:00d3ffdaafe92a5dc603cb9bd5111aaa36dfa187c8285c543be562e61b755f6b", + "sha256:024e606be3ed92216e2b6952ed859d86b4cfa52cd5bc5f050e7dc28f9b43ec42", + "sha256:0298eafff88c99982a4cf66ba2efa1128e4ddaca0b05eec4c456bbc7db691d8d", + "sha256:02a51034802cbf38db3f89c66fb5d2ec57e6fe7ef2f4a44d070a593c3688667b", + "sha256:083c8d17153ecb403e5e1eb76a7ef4babfc2c48d58899c98fcaa04833e7a2f9a", + "sha256:0a11e971ed097d24c534c037d298ad32c6ce81a45736d31e0ff0ad37ab437d59", + "sha256:0bf2dae5291758b6f84cf923bfaa285632816007db0330002fa1de38bfcb7154", + "sha256:0c0a590235ccd933d9892c627dec5bc7511ce6ad6c1011fdf5b11363022746c1", + "sha256:0f438ae3532723fb6ead77e7c604be7c8374094ef4ee2c5e03a3a17f1fca256c", + "sha256:109487860ef6a328f3eec66f2bf78b0b72400280d8f8ea05f69c51644ba6521a", + "sha256:11b53acf2411c3b09e6af37e4b9005cba376c872503c8f28218c7243582df45d", + "sha256:12db3b2c533c23ab812c2b25934f60383361f8a376ae272665f8e48b88e8e1c6", + "sha256:14e76c0f23218b8f46c4d87018ca2e441535aed3632ca134b10239dfb6dadd6b", + "sha256:16a8663d6e281208d78806dbe14ee9903715361cf81f6d4309944e4d1e59ac5b", + "sha256:292d5e8ba896bbfd6334b096e34bffb56161c81408d6d036a7dfa6929cff8783", + "sha256:2c03cc56021a4bd59be889c2b9257dae13bf55041a3372d3295416f86b295fb5", + "sha256:2e396d70bc4ef5325b72b593a72c8979999aa52fb8bcf03f701c1b03e1166918", + "sha256:2edb64ee7bf1ed524a1da60cdcd2e1f6e2b4f66ef7c077680739f1641f62f555", + "sha256:31a9ddf4718d10ae04d9b18801bd776693487cbb57d74cc3458a7673f6f34639", + "sha256:356541bf4381fa35856dafa6a965916e54bed415ad8a24ee6de6e37deccf2786", + "sha256:358a7c4cb8ba9b46c453b1dd8d9e431452d5249072e4f56cfda3149f6ab1405e", + "sha256:37f8febc8ec50c14f3ec9637505f28e58d4f66752207ea177c1d67df25da5aed", + "sha256:39049da0ffb96c8cbb65cbf5c5f3ca3168990adf3551bd1dee10c48fce8ae820", + "sha256:39cf9ed17fe3b1bc81f33c9ceb6ce67683ee7526e65fde1447c772afc54a1bb8", + "sha256:3ae1de54a77dc0d6d5fcf623290af4266412a7c4be0b1ff7444394f03f5c54e3", + "sha256:3b590df687e3c5ee0deef9fc8c547d81986d9a1b56073d82de008744452d6541", + "sha256:3e45867f1f2ab0711d60c6c71746ac53537f1684baa699f4f668d4c6f6ce8e14", + "sha256:3fc1c4a2ffd64890aebdb3f97e1278b0cc72579a08ca4de8cd2c04799a3a22be", + "sha256:4457ea6774b5611f4bed5eaa5df55f70abde42364d498c5134b7ef4c6958e20e", + "sha256:44ba614de5361b3e5278e1241fda3dc1838deed864b50a10d7ce92983797fa76", + "sha256:4a8fcf28c05c1f6d7e177a9a46a1c52798bfe2ad80681d275b10dcf317deaf0b", + "sha256:4b0d02d7102dd0f997580b51edc4cebcf2ab6397a7edf89f1c73b586c614272c", + "sha256:502218f52498a36d6bf5ea77081844017bf7982cdbe521ad85e64cabee1b608b", + "sha256:503e65837c71b875ecdd733877d852adbc465bd82c768a067badd953bf1bc5a3", + "sha256:5995f0164fa7df59db4746112fec3f49c461dd6b31b841873443bdb077c13cfc", + "sha256:59e5686dd847347e55dffcc191a96622f016bc0ad89105e24c14e0d6305acbc6", + "sha256:601f36512f9e28f029d9481bdaf8e89e5148ac5d89cffd3b05cd533eeb423b59", + "sha256:608862a7bf6957f2333fc54ab4399e405baad0163dc9f8d99cb236816db169d4", + "sha256:62595ab75873d50d57323a91dd03e6966eb79c41fa834b7a1661ed043b2d404d", + "sha256:70990b9c51340e4044cfc394a81f614f3f90d41397104d226f21e66de668730d", + "sha256:71140351489970dfe5e60fc621ada3e0f41104a5eddaca47a7acb3c1b851d6d3", + "sha256:72966d1b297c741541ca8cf1223ff262a6febe52481af742036a0b296e35fa5a", + "sha256:74292fc76c905c0ef095fe11e188a32ebd03bc38f3f3e9bcb85e4e6db177b7ea", + "sha256:761e8904c07ad053d285670f36dd94e1b6ab7f16ce62b9805c475b7aa1cffde6", + "sha256:772b87914ff1152b92a197ef4ea40efe27a378606c39446ded52c8f80f79702e", + "sha256:79909e27e8e4fcc9db4addea88aa63f6423ebb171db091fb4373e3312cb6d603", + "sha256:7e189e2e1d3ed2f4aebabd2d5b0f931e883676e51c7624826e0a4e5fe8a0bf24", + "sha256:7eb33a30d75562222b64f569c642ff3dc6689e09adda43a082208397f016c39a", + "sha256:81d6741ab457d14fdedc215516665050f3822d3e56508921cc7239f8c8e66a58", + "sha256:8499ca8f4502af841f68135133d8258f7b32a53a1d594aa98cc52013fff55678", + "sha256:84c3990934bae40ea69a82034912ffe5a62c60bbf6ec5bc9691419641d7d5c9a", + "sha256:87701167f2a5c930b403e9756fab1d31d4d4da52856143b609e30a1ce7160f3c", + "sha256:88600c72ef7587fe1708fd242b385b6ed4b8904976d5da0893e31df8b3480cb6", + "sha256:8ac7b6a045b814cf0c47f3623d21ebd88b3e8cf216a14790b455ea7ff0135d18", + "sha256:8b8af03d2e37866d023ad0ddea594edefc31e827fee64f8de5611a1dbc373174", + "sha256:8c7fe7afa480e3e82eed58e0ca89f751cd14d767638e2550c77a92a9e749c317", + "sha256:8eade758719add78ec36dc13201483f8e9b5d940329285edcd5f70c0a9edbd7f", + "sha256:911d8a40b2bef5b8bbae2e36a0b103f142ac53557ab421dc16ac4aafee6f53dc", + "sha256:93ad6d87ac18e2a90b0fe89df7c65263b9a99a0eb98f0a3d2e079f12a0735837", + "sha256:95dea361dd73757c6f1c0a1480ac499952c16ac83f7f5f4f84f0658a01b8ef41", + "sha256:9ab77acb98eba3fd2a85cd160851816bfce6871d944d885febf012713f06659c", + "sha256:9cb3032517f1627cc012dbc80a8ec976ae76d93ea2b5feaa9d2a5b8882597579", + "sha256:9cf4e8ad252f7c38dd1f676b46514f92dc0ebeb0db5552f5f403509705e24753", + "sha256:9d9153257a3f70d5f69edf2325357251ed20f772b12e593f3b3377b5f78e7ef8", + "sha256:a152f5f33d64a6be73f1d30c9cc82dfc73cec6477ec268e7c6e4c7d23c2d2291", + "sha256:a16418ecf1329f71df119e8a65f3aa68004a3f9383821edcb20f0702934d8087", + "sha256:a60332922359f920193b1d4826953c507a877b523b2395ad7bc716ddd386d866", + "sha256:a8d0fc946c784ff7f7c3742310cc8a57c5c6dc31631269876a88b809dbeff3d3", + "sha256:ab5de034a886f616a5668aa5d098af2b5385ed70142090e2a31bcbd0af0fdb3d", + "sha256:c22d3fe05ce11d3671297dc8973267daa0f938b93ec716e12e0f6dee81591dc1", + "sha256:c2ac1b08635a8cd4e0cbeaf6f5e922085908d48eb05d44c5ae9eabab148512ca", + "sha256:c512accbd6ff0270939b9ac214b84fb5ada5f0409c44298361b2f5e13f9aed9e", + "sha256:c75ffc45f25324e68ab238cb4b5c0a38cd1c3d7f1fb1f72b5541de469e2247db", + "sha256:c95a03c79bbe30eec3ec2b7f076074f4281526724c8685a42872974ef4d36b72", + "sha256:cadaeaba78750d58d3cc6ac4d1fd867da6fc73c88156b7a3212a3cd4819d679d", + "sha256:cd6056167405314a4dc3c173943f11249fa0f1b204f8b51ed4bde1a9cd1834dc", + "sha256:db72b07027db150f468fbada4d85b3b2729a3db39178abf5c543b784c1254539", + "sha256:df2c707231459e8a4028eabcd3cfc827befd635b3ef72eada84ab13b52e1574d", + "sha256:e62164b50f84e20601c1ff8eb55620d2ad25fb81b59e3cd776a1902527a788af", + "sha256:e696f0dd336161fca9adbb846875d40752e6eba585843c768935ba5c9960722b", + "sha256:eaa379fcd227ca235d04152ca6704c7cb55564116f8bc52545ff357628e10602", + "sha256:ebea339af930f8ca5d7a699b921106c6e29c617fe9606fa7baa043c1cdae326f", + "sha256:f4c39b0e3eac288fedc2b43055cfc2ca7a60362d0e5e87a637beac5d801ef478", + "sha256:f5057856d21e7586765171eac8b9fc3f7d44ef39425f85dbcccb13b3ebea806c", + "sha256:f6f45710b4459401609ebebdbcfb34515da4fc2aa886f95107f556ac69a9147e", + "sha256:f97e83fa6c25693c7a35de154681fcc257c1c41b38beb0304b9c4d2d9e164479", + "sha256:f9d0c5c045a3ca9bedfc35dca8526798eb91a07aa7a2c0fee134c6c6f321cbd7", + "sha256:ff6f3db31555657f3163b15a6b7c6938d08df7adbfc9dd13d9d19edad678f1e8" + ], + "version": "==3.0.1" + }, "click": { "hashes": [ "sha256:7682dc8afb30297001674575ea00d1814d808d6a36af415a82bd481d37ba7b8e", @@ -32,6 +125,22 @@ "index": "pypi", "version": "==8.1.3" }, + "idna": { + "hashes": [ + "sha256:814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4", + "sha256:90b77e79eaa3eba6de819a0c442c0b4ceefc341a7a2ab77d7562bf49f425c5c2" + ], + "markers": "python_version >= '3.5'", + "version": "==3.4" + }, + "requests": { + "hashes": [ + "sha256:64299f4909223da747622c030b781c0d7811e359c37124b4bd368fb8c6518baa", + "sha256:98b1b2782e3c6c4904938b84c0eb932721069dfdb9134313beff7c83c2df24bf" + ], + "index": "pypi", + "version": "==2.28.2" + }, "sentry-sdk": { "hashes": [ "sha256:69ecbb2e1ff4db02a06c4f20f6f69cb5dfe3ebfbc06d023e40d77cf78e9c37e7", @@ -45,7 +154,7 @@ "sha256:076907bf8fd355cde77728471316625a4d2f7e713c125f51953bb5b3eecf4f72", "sha256:75edcdc2f7d85b137124a6c3c9fc3933cdeaa12ecb9a6a959f22797a0feca7e1" ], - "markers": "python_version >= '3.6'", + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'", "version": "==1.26.14" } }, @@ -421,35 +530,35 @@ }, "mypy": { "hashes": [ - "sha256:01b1b9e1ed40544ef486fa8ac022232ccc57109f379611633ede8e71630d07d2", - "sha256:0ab090d9240d6b4e99e1fa998c2d0aa5b29fc0fb06bd30e7ad6183c95fa07593", - "sha256:14d776869a3e6c89c17eb943100f7868f677703c8a4e00b3803918f86aafbc52", - "sha256:1ace23f6bb4aec4604b86c4843276e8fa548d667dbbd0cb83a3ae14b18b2db6c", - "sha256:2efa963bdddb27cb4a0d42545cd137a8d2b883bd181bbc4525b568ef6eca258f", - "sha256:2f6ac8c87e046dc18c7d1d7f6653a66787a4555085b056fe2d599f1f1a2a2d21", - "sha256:3ae4c7a99e5153496243146a3baf33b9beff714464ca386b5f62daad601d87af", - "sha256:3cfad08f16a9c6611e6143485a93de0e1e13f48cfb90bcad7d5fde1c0cec3d36", - "sha256:4e5175026618c178dfba6188228b845b64131034ab3ba52acaffa8f6c361f805", - "sha256:50979d5efff8d4135d9db293c6cb2c42260e70fb010cbc697b1311a4d7a39ddb", - "sha256:5cd187d92b6939617f1168a4fe68f68add749902c010e66fe574c165c742ed88", - "sha256:5cfca124f0ac6707747544c127880893ad72a656e136adc935c8600740b21ff5", - "sha256:5e398652d005a198a7f3c132426b33c6b85d98aa7dc852137a2a3be8890c4072", - "sha256:67cced7f15654710386e5c10b96608f1ee3d5c94ca1da5a2aad5889793a824c1", - "sha256:7306edca1c6f1b5fa0bc9aa645e6ac8393014fa82d0fa180d0ebc990ebe15964", - "sha256:7cc2c01dfc5a3cbddfa6c13f530ef3b95292f926329929001d45e124342cd6b7", - "sha256:87edfaf344c9401942883fad030909116aa77b0fa7e6e8e1c5407e14549afe9a", - "sha256:8845125d0b7c57838a10fd8925b0f5f709d0e08568ce587cc862aacce453e3dd", - "sha256:92024447a339400ea00ac228369cd242e988dd775640755fa4ac0c126e49bb74", - "sha256:a86b794e8a56ada65c573183756eac8ac5b8d3d59daf9d5ebd72ecdbb7867a43", - "sha256:bb2782a036d9eb6b5a6efcdda0986774bf798beef86a62da86cb73e2a10b423d", - "sha256:be78077064d016bc1b639c2cbcc5be945b47b4261a4f4b7d8923f6c69c5c9457", - "sha256:c7cf862aef988b5fbaa17764ad1d21b4831436701c7d2b653156a9497d92c83c", - "sha256:e0626db16705ab9f7fa6c249c017c887baf20738ce7f9129da162bb3075fc1af", - "sha256:f34495079c8d9da05b183f9f7daec2878280c2ad7cc81da686ef0b484cea2ecf", - "sha256:fe523fcbd52c05040c7bee370d66fee8373c5972171e4fbc323153433198592d" + "sha256:0af4f0e20706aadf4e6f8f8dc5ab739089146b83fd53cb4a7e0e850ef3de0bb6", + "sha256:15b5a824b58c7c822c51bc66308e759243c32631896743f030daf449fe3677f3", + "sha256:17455cda53eeee0a4adb6371a21dd3dbf465897de82843751cf822605d152c8c", + "sha256:2013226d17f20468f34feddd6aae4635a55f79626549099354ce641bc7d40262", + "sha256:24189f23dc66f83b839bd1cce2dfc356020dfc9a8bae03978477b15be61b062e", + "sha256:27a0f74a298769d9fdc8498fcb4f2beb86f0564bcdb1a37b58cbbe78e55cf8c0", + "sha256:28cea5a6392bb43d266782983b5a4216c25544cd7d80be681a155ddcdafd152d", + "sha256:448de661536d270ce04f2d7dddaa49b2fdba6e3bd8a83212164d4174ff43aa65", + "sha256:48525aec92b47baed9b3380371ab8ab6e63a5aab317347dfe9e55e02aaad22e8", + "sha256:5bc8d6bd3b274dd3846597855d96d38d947aedba18776aa998a8d46fabdaed76", + "sha256:5deb252fd42a77add936b463033a59b8e48eb2eaec2976d76b6878d031933fe4", + "sha256:5f546ac34093c6ce33f6278f7c88f0f147a4849386d3bf3ae193702f4fe31407", + "sha256:5fdd63e4f50e3538617887e9aee91855368d9fc1dea30da743837b0df7373bc4", + "sha256:65b122a993d9c81ea0bfde7689b3365318a88bde952e4dfa1b3a8b4ac05d168b", + "sha256:71a808334d3f41ef011faa5a5cd8153606df5fc0b56de5b2e89566c8093a0c9a", + "sha256:920169f0184215eef19294fa86ea49ffd4635dedfdea2b57e45cb4ee85d5ccaf", + "sha256:93a85495fb13dc484251b4c1fd7a5ac370cd0d812bbfc3b39c1bafefe95275d5", + "sha256:a2948c40a7dd46c1c33765718936669dc1f628f134013b02ff5ac6c7ef6942bf", + "sha256:c6c2ccb7af7154673c591189c3687b013122c5a891bb5651eca3db8e6c6c55bd", + "sha256:c96b8a0c019fe29040d520d9257d8c8f122a7343a8307bf8d6d4a43f5c5bfcc8", + "sha256:d42a98e76070a365a1d1c220fcac8aa4ada12ae0db679cb4d910fabefc88b994", + "sha256:dbeb24514c4acbc78d205f85dd0e800f34062efcc1f4a4857c57e4b4b8712bff", + "sha256:e60d0b09f62ae97a94605c3f73fd952395286cf3e3b9e7b97f60b01ddfbbda88", + "sha256:e64f48c6176e243ad015e995de05af7f22bbe370dbb5b32bd6988438ec873919", + "sha256:e831662208055b006eef68392a768ff83596035ffd6d846786578ba1714ba8f6", + "sha256:eda5c8b9949ed411ff752b9a01adda31afe7eae1e53e946dbdf9db23865e66c4" ], "index": "pypi", - "version": "==1.0.0" + "version": "==1.0.1" }, "mypy-extensions": { "hashes": [ @@ -615,9 +724,17 @@ "sha256:64299f4909223da747622c030b781c0d7811e359c37124b4bd368fb8c6518baa", "sha256:98b1b2782e3c6c4904938b84c0eb932721069dfdb9134313beff7c83c2df24bf" ], - "markers": "python_version >= '3.7' and python_version < '4'", + "index": "pypi", "version": "==2.28.2" }, + "requests-mock": { + "hashes": [ + "sha256:2fdbb637ad17ee15c06f33d31169e71bf9fe2bdb7bc9da26185be0dd8d842699", + "sha256:59c9c32419a9fb1ae83ec242d98e889c45bd7d7a65d48375cc243ec08441658b" + ], + "index": "pypi", + "version": "==1.10.0" + }, "six": { "hashes": [ "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926", @@ -665,6 +782,21 @@ "markers": "python_version >= '3.6'", "version": "==0.11.6" }, + "types-requests": { + "hashes": [ + "sha256:232792870b60adb07d23175451ab4e6190021b0c584edf052d92d9b993118f06", + "sha256:f84613b0d4c5d0eeb7879dfa05e14a3702b9c1f7a4ee81dfe9b4321b13fe93a1" + ], + "index": "pypi", + "version": "==2.28.11.14" + }, + "types-urllib3": { + "hashes": [ + "sha256:28d2d7f5c31ff8ed4d9d2e396ce906c49d37523c3ec207d03d3b1695755a7199", + "sha256:df4d3e5472bf8830bd74eac12d56e659f88662ba040c7d106bf3a5bee26fff28" + ], + "version": "==1.26.25.7" + }, "typing-extensions": { "hashes": [ "sha256:5cb5f4a79139d699607b3ef622a1dedafa84e115ab0024e0d9c044a9479ca7cb", @@ -678,7 +810,7 @@ "sha256:076907bf8fd355cde77728471316625a4d2f7e713c125f51953bb5b3eecf4f72", "sha256:75edcdc2f7d85b137124a6c3c9fc3933cdeaa12ecb9a6a959f22797a0feca7e1" ], - "markers": "python_version >= '3.6'", + "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'", "version": "==1.26.14" }, "vulture": { diff --git a/README.md b/README.md index 9fae915..11f8af5 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,14 @@ A CLI application to generate and email credit card slips for Alma invoices via - To lint the repo: `make lint` - To run the app: `pipenv run ccslips --help` -## ENV Variables +## Required ENV Variables -- `LOG_LEVEL` = Optional, set to a valid Python logging level (e.g. `DEBUG`, case-insensitive) if desired. Can also be passed as an option directly to the ccslips command. Defaults to `INFO` if not set or passed to the command. -- `SENTRY_DSN` = If set to a valid Sentry DSN, enables Sentry exception monitoring. This is not needed for local development. -- `WORKSPACE` = Set to `dev` for local development, this will be set to `stage` and `prod` in those environments by Terraform. +- `ALMA_API_URL`: Base URL for the Alma API. +- `ALMA_API_READ_KEY`: Read-only key for the appropriate Alma instance (sandbox or prod) Acquisitions API. +- `WORKSPACE`: Set to `dev` for local development, this will be set to `stage` and `prod` in those environments by Terraform. + +## Optional ENV Variables + +- `ALMA_API_TIMEOUT`: Request timeout for Alma API calls. Defaults to 30 seconds if not set. +- `LOG_LEVEL`: Set to a valid Python logging level (e.g. `DEBUG`, case-insensitive) if desired. Can also be passed as an option directly to the ccslips command. Defaults to `INFO` if not set or passed to the command. +- `SENTRY_DSN`: If set to a valid Sentry DSN enables Sentry exception monitoring. This is not needed for local development. diff --git a/ccslips/alma.py b/ccslips/alma.py new file mode 100644 index 0000000..2ddd2e1 --- /dev/null +++ b/ccslips/alma.py @@ -0,0 +1,148 @@ +import logging +import time +from typing import Generator, Optional +from urllib.parse import urljoin + +import requests + +from ccslips.config import load_alma_config + +logger = logging.getLogger(__name__) + + +class AlmaClient: + """AlmaClient class. + + An Alma API client with specific functionality necessary for credit card slips + processing. + + Notes: + - All requests to the Alma API include a 0.1 second wait to ensure we don't + exceed the API rate limit. + - If no records are found for a given endpoint with the provided parameters, + Alma will still return a 200 success response with a json object of + {"total_record_count": 0} and these methods will return that object. + """ + + def __init__(self) -> None: + """Initialize AlmaClient instance.""" + alma_config = load_alma_config() + self.base_url = alma_config["BASE_URL"] + self.headers = { + "Authorization": f"apikey {alma_config['API_KEY']}", + "Accept": "application/json", + "Content-Type": "application/json", + } + self.timeout = float(alma_config["TIMEOUT"]) + + def get_paged( + self, + endpoint: str, + record_type: str, + params: Optional[dict] = None, + limit: int = 100, + _offset: int = 0, + _records_retrieved: int = 0, + ) -> Generator[dict, None, None]: + """Retrieve paginated results from the Alma API for a given endpoint. + + Args: + endpoint: The paged Alma API endpoint to call, e.g. "acq/invoices". + record_type: The type of record returned by the Alma API for the specified + endpoint, e.g. "invoice" record_type returned by the "acq/invoices" + endpoint. See for example. + params: Any endpoint-specific params to supply to the GET request. + limit: The maximum number of records to retrieve per page. Valid values are + 0-100. + _offset: The offset value to supply to paged request. Should only be used + internally by this method's recursion. + _records_retrieved: The number of records retrieved so far for a given + paged endpoint. Should only be used internally by this method's + recursion. + """ + params = params or {} + params["limit"] = str(limit) + params["offset"] = str(_offset) + response = requests.get( + url=urljoin(self.base_url, endpoint), + params=params, + headers=self.headers, + timeout=self.timeout, + ) + response.raise_for_status() + time.sleep(0.1) + total_record_count = int(response.json()["total_record_count"]) + records = response.json().get(record_type, []) + records_retrieved = _records_retrieved + len(records) + for record in records: + yield record + if records_retrieved < total_record_count: + yield from self.get_paged( + endpoint, + record_type, + params=params, + limit=limit, + _offset=_offset + limit, + _records_retrieved=records_retrieved, + ) + + def get_brief_po_lines( + self, acquisition_method: Optional[str] = None + ) -> Generator[dict, None, None]: + """ + Get brief PO line records, optionally filtered by acquisition_method. + + The PO line records retrieved from this endpoint do not contain all of the PO + line data and users may wish to retrieve the full PO line records with the + get_full_po_lines method. + """ + po_line_params = { + "status": "ACTIVE", + "acquisition_method": acquisition_method, + } + return self.get_paged( + endpoint="acq/po-lines", record_type="po_line", params=po_line_params + ) + + def get_full_po_line(self, po_line_id: str) -> dict: + """Get a single full PO line record using the PO line ID.""" + response = requests.get( + url=str(urljoin(self.base_url, f"acq/po-lines/{po_line_id}")), + headers=self.headers, + timeout=self.timeout, + ) + response.raise_for_status() + time.sleep(0.1) + return response.json() + + def get_full_po_lines( + self, + acquisition_method: Optional[str] = None, + date: Optional[str] = None, + ) -> Generator[dict, None, None]: + """Get full PO line records, optionally filtered by acquisition_method/date.""" + for line in self.get_brief_po_lines(acquisition_method): + number = line["number"] + if date is None: + yield self.get_full_po_line(number) + elif line.get("created_date") == f"{date}Z": + yield self.get_full_po_line(number) + + def get_fund_by_code(self, fund_code: str) -> dict: + """Get fund details using the fund code. + + Note: this technically returns a list of funds as the request uses a search + query rather than getting a single fund directly, which is not supported by the + API. Theoretically the result could include multiple funds, however in practice + we expect there to only be one. + """ + response = requests.get( + urljoin(self.base_url, "acq/funds"), + headers=self.headers, + params={"q": f"fund_code~{fund_code}", "view": "full"}, + timeout=self.timeout, + ) + response.raise_for_status() + time.sleep(0.1) + return response.json() diff --git a/ccslips/config.py b/ccslips/config.py index e69ab13..330e046 100644 --- a/ccslips/config.py +++ b/ccslips/config.py @@ -28,9 +28,17 @@ def configure_logger(logger: logging.Logger, log_level_string: str) -> str: def configure_sentry() -> str: - env = os.getenv("WORKSPACE") + env = os.environ["WORKSPACE"] sentry_dsn = os.getenv("SENTRY_DSN") if sentry_dsn and sentry_dsn.lower() != "none": sentry_sdk.init(sentry_dsn, environment=env) return f"Sentry DSN found, exceptions will be sent to Sentry with env={env}" return "No Sentry DSN found, exceptions will not be sent to Sentry" + + +def load_alma_config() -> dict[str, str]: + return { + "API_KEY": os.environ["ALMA_API_READ_KEY"], + "BASE_URL": os.environ["ALMA_API_URL"], + "TIMEOUT": os.getenv("ALMA_API_TIMEOUT", "30"), + } diff --git a/tests/conftest.py b/tests/conftest.py index 5072cf1..05b31ba 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,15 +1,110 @@ +import json import os import pytest +import requests_mock from click.testing import CliRunner +from ccslips.alma import AlmaClient + +# Env fixture @pytest.fixture(autouse=True) def test_env(): - os.environ = {"SENTRY_DSN": None, "WORKSPACE": "test"} + os.environ = { + "ALMA_API_URL": "https://example.com", + "ALMA_API_READ_KEY": "just-for-testing", + "ALMA_API_TIMEOUT": "10", + "SENTRY_DSN": "None", + "WORKSPACE": "test", + } yield +# CLI fixture @pytest.fixture() def runner(): return CliRunner() + + +# Record fixtures +@pytest.fixture(name="fund_records", scope="session") +def fund_records_fixture(): + with open("tests/fixtures/fund_records.json", encoding="utf-8") as funds_file: + return json.load(funds_file) + + +@pytest.fixture(name="po_line_records", scope="session") +def po_line_records_fixture(): + with open("tests/fixtures/po_line_records.json", encoding="utf-8") as po_lines_file: + return json.load(po_lines_file) + + +# API fixtures +@pytest.fixture() +def alma_client(): + return AlmaClient() + + +@pytest.fixture(autouse=True) +def mocked_alma(fund_records, po_line_records): + with requests_mock.Mocker() as mocker: + # Generic paged endpoints + mocker.get( + "https://example.com/paged?limit=10&offset=0", + complete_qs=True, + json={ + "fake_records": [{"record_number": i} for i in range(10)], + "total_record_count": 15, + }, + ) + mocker.get( + "https://example.com/paged?limit=10&offset=10", + complete_qs=True, + json={ + "fake_records": [{"record_number": i} for i in range(10, 15)], + "total_record_count": 15, + }, + ) + + # Fund endpoints + mocker.get( + "https://example.com/acq/funds?q=fund_code~FUND-abc", + json={"fund": [fund_records["abc"]], "total_record_count": 1}, + ) + + # PO Line endpoints + mocker.get( + "https://example.com/acq/po-lines?status=ACTIVE", + json={ + "po_line": [po_line_records["other_acq_method"]], + "total_record_count": 1, + }, + ) + mocker.get( + ( + "https://example.com/acq/po-lines?status=ACTIVE&" + "acquisition_method=PURCHASE_NOLETTER" + ), + json={ + "po_line": [ + po_line_records["all_fields"], + po_line_records["wrong_date"], + ], + "total_record_count": 2, + }, + ) + mocker.get( + "https://example.com/acq/po-lines/POL-all-fields", + json=po_line_records["all_fields"], + ) + mocker.get( + "https://example.com/acq/po-lines/POL-other-acq-method", + json=po_line_records["other_acq_method"], + ) + mocker.get( + "https://example.com/acq/po-lines/POL-wrong-date", + json=po_line_records["wrong_date"], + ) + + yield mocker diff --git a/tests/fixtures/fund_records.json b/tests/fixtures/fund_records.json new file mode 100644 index 0000000..4f8b285 --- /dev/null +++ b/tests/fixtures/fund_records.json @@ -0,0 +1,5 @@ +{ + "abc": { + "code": "FUND-abc" + } +} diff --git a/tests/fixtures/po_line_records.json b/tests/fixtures/po_line_records.json new file mode 100644 index 0000000..dc2ec86 --- /dev/null +++ b/tests/fixtures/po_line_records.json @@ -0,0 +1,23 @@ +{ + "all_fields": { + "acquisition_method": { + "desc": "Credit Card" + }, + "created_date": "2023-01-02Z", + "number": "POL-all-fields" + }, + "other_acq_method": { + "acquisition_method": { + "desc": "Something else" + }, + "created_date": "2023-01-02Z", + "number": "POL-other-acq-method" + }, + "wrong_date": { + "acquisition_method": { + "desc": "Credit Card" + }, + "created_date": "2023-12-11Z", + "number": "POL-wrong-date" + } +} diff --git a/tests/test_alma.py b/tests/test_alma.py new file mode 100644 index 0000000..e74b4e6 --- /dev/null +++ b/tests/test_alma.py @@ -0,0 +1,63 @@ +from ccslips.alma import AlmaClient + + +def test_client_initializes_with_expected_values(): + client = AlmaClient() + assert client.base_url == "https://example.com" + assert client.headers == { + "Authorization": "apikey just-for-testing", + "Accept": "application/json", + "Content-Type": "application/json", + } + assert client.timeout == 10 + + +def test_get_paged(alma_client): + records = alma_client.get_paged( + endpoint="paged", + record_type="fake_records", + limit=10, + ) + assert len(list(records)) == 15 + + +def test_get_brief_po_lines_without_acquisition_method(alma_client): + result = list(alma_client.get_brief_po_lines()) + assert len(result) == 1 + assert result[0]["number"] == "POL-other-acq-method" + + +def test_get_brief_po_lines_with_acquisition_method(alma_client): + result = list(alma_client.get_brief_po_lines("PURCHASE_NOLETTER")) + assert len(result) == 2 + assert result[0]["number"] == "POL-all-fields" + assert result[1]["number"] == "POL-wrong-date" + + +def test_get_full_po_line(alma_client): + assert alma_client.get_full_po_line("POL-all-fields") == { + "acquisition_method": {"desc": "Credit Card"}, + "created_date": "2023-01-02Z", + "number": "POL-all-fields", + } + + +def test_get_full_po_lines_with_defaults(alma_client): + result = list(alma_client.get_full_po_lines()) + assert len(result) == 1 + assert result[0]["number"] == "POL-other-acq-method" + + +def test_get_full_po_lines_with_parameters(alma_client): + result = list( + alma_client.get_full_po_lines( + acquisition_method="PURCHASE_NOLETTER", date="2023-01-02" + ) + ) + assert len(result) == 1 + assert result[0]["number"] == "POL-all-fields" + + +def test_alma_get_fund_by_code(alma_client): + fund = alma_client.get_fund_by_code("FUND-abc") + assert fund["fund"][0]["code"] == "FUND-abc" diff --git a/tests/test_config.py b/tests/test_config.py index 01d7e26..d98a183 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -2,7 +2,7 @@ import pytest -from ccslips.config import configure_logger, configure_sentry +from ccslips.config import configure_logger, configure_sentry, load_alma_config def test_configure_logger_with_invalid_level_raises_error(): @@ -42,3 +42,20 @@ def test_configure_sentry_env_variable_is_dsn(monkeypatch): monkeypatch.setenv("SENTRY_DSN", "https://1234567890@00000.ingest.sentry.io/123456") result = configure_sentry() assert result == "Sentry DSN found, exceptions will be sent to Sentry with env=test" + + +def test_load_alma_config_from_env(): + assert load_alma_config() == { + "API_KEY": "just-for-testing", + "BASE_URL": "https://example.com", + "TIMEOUT": "10", + } + + +def test_load_alma_config_from_defaults(monkeypatch): + monkeypatch.delenv("ALMA_API_TIMEOUT", raising=False) + assert load_alma_config() == { + "API_KEY": "just-for-testing", + "BASE_URL": "https://example.com", + "TIMEOUT": "30", + }