-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/getorganized #106
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
Merged
Merged
Feature/getorganized #106
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e262010
Added submodule for GetOrganized
bakspace-itk 789440b
added failing test case
bakspace-itk 0bc18ce
fixed bad endpoints and removed session from return, setting uuid to …
bakspace-itk b6d7039
Updated changelog, fixed test and function for deleting document and …
bakspace-itk 7d29722
Merge branch 'develop' into feature/getorganized
bakspace-itk 583b90f
linting and added inits
bakspace-itk 07b235f
Merge branch 'feature/getorganized' of https://github.com/itk-dev-rpa…
bakspace-itk c8ba80c
lint
bakspace-itk 86df0d1
linting ignores a weird abstract class bug
bakspace-itk c9d2d25
Update itk_dev_shared_components/getorganized/go_api.py
bakspace-itk 21d7c1a
Fixed docstrings, returns and added parameters to case creation
bakspace-itk 0f05254
Added parameters to function, added comments on GetOrganized variables
bakspace-itk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
Empty file.
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
"""Functions for working with the GetOrganized API.""" | ||
|
||
import json | ||
from urllib.parse import urljoin | ||
from typing import Literal | ||
|
||
from requests import Session | ||
from requests_ntlm import HttpNtlmAuth | ||
|
||
|
||
def create_session(username: str, password: str) -> Session: | ||
"""Create a session for accessing GetOrganized API. | ||
|
||
Args: | ||
username: Username for login. | ||
password: Password for login. | ||
|
||
Returns: | ||
Return the session object | ||
""" | ||
session = Session() | ||
session.headers.setdefault("Content-Type", "application/json") | ||
session.auth = HttpNtlmAuth(username, password) | ||
return session | ||
|
||
|
||
def upload_document(*, apiurl: str, file: bytearray, case_id: str, filename: str, agent_name: str | None = None, date_string: str | None = None, session: Session, doc_category: str | None = None, case_type: str = Literal["EMN", "GEO"], overwrite: bool = True) -> tuple[str, Session]: | ||
"""Upload a document to Get Organized. | ||
|
||
Args: | ||
apiurl: Base url for API. | ||
session: Session token for request. | ||
file: Bytearray of file to upload. | ||
case_id: Case ID already present in GO. | ||
filename: Name of file when saved in GO. | ||
agent_name: Agent name, used for creating a folder in GO. Defaults to None. | ||
date_string: A date to add as metadata to GetOrganized. Defaults to None. | ||
|
||
Returns: | ||
Return response text. | ||
""" | ||
url = apiurl + "/_goapi/Documents/AddToCase" | ||
payload = { | ||
"Bytes": list(file), | ||
"CaseId": case_id, | ||
"SiteUrl": urljoin(apiurl, f"/case{case_type}/{case_id}"), | ||
"ListName": "Dokumenter", | ||
"FolderPath": agent_name, | ||
"FileName": filename, | ||
"Metadata": f"<z:row xmlns:z='#RowsetSchema' ows_Dato='{date_string}' ows_Kategori='{doc_category}'/>", | ||
"Overwrite": overwrite | ||
} | ||
response = session.post(url, data=json.dumps(payload), timeout=60) | ||
response.raise_for_status() | ||
return response.text | ||
|
||
|
||
def delete_document(apiurl: str, document_id: int, session: Session) -> tuple[str, Session]: | ||
"""Delete a document from GetOrganized. | ||
|
||
Args: | ||
apiurl: Url of the GetOrganized API. | ||
session: Session object used for logging in. | ||
document_id: ID of the document to delete. | ||
|
||
Returns: | ||
Return the response. | ||
""" | ||
url = urljoin(apiurl, "/_goapi/Documents/ByDocumentId/") | ||
payload = { | ||
"DocId": document_id, | ||
"ForceDelete": True | ||
} | ||
response = session.delete(url, timeout=60, data=json.dumps(payload)) | ||
response.raise_for_status() | ||
return response | ||
|
||
|
||
def create_case(session: Session, apiurl: str, title: str, category: str, department: str, kle: str, case_type: str = Literal["EMN", "GEO"]) -> str: | ||
"""Create a case in GetOrganized. | ||
|
||
Args: | ||
apiurl: Url for the GetOrganized API. | ||
session: Session object to access API. | ||
title: Title of the case being created. | ||
category: Case category to create the case for. | ||
department: Department for the case. | ||
kle: KLE number for the case (https://www.kle-online.dk/emneplan/00/) | ||
|
||
Returns: | ||
Return the caseID of the created case. | ||
""" | ||
url = urljoin(apiurl, "/_goapi/Cases/") | ||
payload = { | ||
'CaseTypePrefix': case_type, | ||
'MetadataXml': f'''<z:row xmlns:z="#RowsetSchema" | ||
ows_Title="{title}" | ||
ows_CaseStatus="Åben" | ||
ows_CaseCategory="{category}" | ||
ows_Afdeling="{department}" | ||
ows_KLENummer="{kle}"/>''', | ||
'ReturnWhenCaseFullyCreated': False | ||
} | ||
response = session.post(url, data=json.dumps(payload), timeout=60) | ||
response.raise_for_status() | ||
return response.json()["CaseID"] | ||
|
||
|
||
def get_case_metadata(session: Session, apiurl: str, case_id: str) -> str: | ||
"""Get metadata for a GetOrganized case, to look through parameters and values. | ||
|
||
Args: | ||
session: Session token. | ||
apiurl: Base URL for the API. | ||
case_id: Case ID to get metadata on. | ||
|
||
Returns: | ||
Return the metadata for the case as an XML string. | ||
""" | ||
url = urljoin(apiurl, f"/_goapi/Cases/Metadata/{case_id}") | ||
response = session.get(url, timeout=60) | ||
response.raise_for_status() | ||
return response.json()["Metadata"] | ||
|
||
|
||
def find_case(session: Session, apiurl: str, case_title: str, case_type: str = Literal["EMN", "GEO"]) -> list[str]: | ||
"""Search for an existing case in GO with the given case title. | ||
The search finds any case that contains the given title in its title. | ||
|
||
Args: | ||
case_title: The title to search for. | ||
session: Session object to access the API. | ||
|
||
Returns: | ||
The case id of the found case(s) if any. | ||
""" | ||
url = apiurl + "/_goapi/Cases/FindByCaseProperties" | ||
payload = { | ||
"FieldProperties": [ | ||
{ | ||
"InternalName": "ows_Title", | ||
"Value": case_title, | ||
"ComparisonType": "Contains", | ||
} | ||
], | ||
"CaseTypePrefixes": [case_type], | ||
"LogicalOperator": "AND", | ||
"ExcludeDeletedCases": True | ||
} | ||
response = session.post(url, data=json.dumps(payload), timeout=60) | ||
response.raise_for_status() | ||
cases = response.json()['CasesInfo'] | ||
|
||
return [case['CaseID'] for case in cases] |
This file contains hidden or 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
This file contains hidden or 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
Empty file.
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"""Tests related to the GetOrganized module.""" | ||
|
||
import unittest | ||
import os | ||
import re | ||
import json | ||
from uuid import uuid4 | ||
|
||
from dotenv import load_dotenv | ||
|
||
from itk_dev_shared_components.getorganized import go_api | ||
|
||
load_dotenv() | ||
|
||
|
||
class CaseTest(unittest.TestCase): | ||
"""Test the Case functionality of GetOrganized integration""" | ||
test_case = None | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
user, password = os.getenv("GO_LOGIN").split(",") | ||
|
||
cls.session = go_api.create_session(user, password) | ||
uuid = uuid4() | ||
cls.test_case = go_api.create_case(session=cls.session, | ||
apiurl=os.getenv("GO_APIURL"), | ||
title=uuid, | ||
bakspace-itk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case_type="EMN", | ||
category=os.getenv("GO_CATEGORY"), | ||
department=os.getenv("GO_DEPARTMENT"), | ||
kle=os.getenv("GO_KLE") | ||
) | ||
|
||
def test_case_created(self): | ||
"""Test case is created.""" | ||
self.assertIsNotNone(self.test_case) | ||
|
||
def test_document(self): | ||
"""Test upload and delete of a document.""" | ||
test_data = bytearray(b"Testdata") | ||
document = go_api.upload_document(session=self.session, apiurl=os.getenv("GO_APIURL"), case_id=self.test_case, filename="Testfil", file=test_data) | ||
self.assertIsNotNone(document) | ||
response = go_api.delete_document(session=self.session, apiurl=os.getenv("GO_APIURL"), document_id=json.loads(document)['DocId']) | ||
self.assertEqual(response.status_code, 200) | ||
|
||
def test_find_case(self): | ||
"""Test finding a case and getting metadata.""" | ||
metadata = go_api.get_case_metadata(self.session, os.getenv("GO_APIURL"), self.test_case) | ||
self.assertIsNotNone(metadata) | ||
|
||
test_case_title = re.match('.*ows_Title="([^"]+)"', metadata)[1] | ||
case_found = go_api.find_case(session=self.session, apiurl=os.getenv("GO_APIURL"), case_title=test_case_title, case_type="EMN") | ||
if isinstance(case_found, list): | ||
self.assertIn(self.test_case, case_found) | ||
else: | ||
self.assertEqual(self.test_case, case_found) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.