Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
it-bott-integrasjoner
cim-client
Commits
4bf4934b
Commit
4bf4934b
authored
Apr 26, 2021
by
Trond Aasan
Browse files
CIM-58 Rename update_person to upsert_person
The API endpoint is called upsert
parent
a0949cae
Changes
6
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
4bf4934b
...
...
@@ -60,20 +60,20 @@ from cim_client import CimClient, CimEndpoints
from
cim_client.models
import
PersonBase
,
PersonList
c
=
CimClient
(
CimEndpoints
(
url
=
'https://example.com'
,
up
date
_person_url
=
'/_webservices/?ws=contacts/upsert/1.0'
,
up
sert
_person_url
=
'/_webservices/?ws=contacts/upsert/1.0'
,
delete_person_url
=
'/_webservices/?ws=contacts/delete/1.0'
),
tokens
=
{
'api_key'
:
{
'X-Gravitee-API-Key'
:
'c-d-a-b'
},})
upd_schema
=
c
.
get_up
date
_person_schema
()
upd_schema
=
c
.
get_up
sert
_person_schema
()
del_schema
=
c
.
get_delete_person_schema
()
person
=
PersonBase
.
from_dict
({
'username'
:
'foo'
,
'firstname'
:
'John'
,
'lastname'
:
'Doe'
})
response1
=
c
.
up
date
_person
(
person
)
response1
=
c
.
up
sert
_person
(
person
)
person2
=
PersonBase
.
from_dict
({
'username'
:
'bar'
,
'firstname'
:
'Petter'
,
'lastname'
:
'Smart'
})
persons
=
[
person
,
person2
]
personlist
=
PersonList
(
persons
=
persons
)
# Note that delete_person supports both PersonList and [Person, Person, ...]
response2
=
c
.
up
date
_person
(
personlist
)
response2
=
c
.
up
sert
_person
(
personlist
)
response3
=
c
.
delete_person
(
person
.
username
)
```
cim_client/client.py
View file @
4bf4934b
...
...
@@ -69,13 +69,13 @@ class CimEndpoints:
def
__init__
(
self
,
url
:
str
,
up
date
_person_url
:
Optional
[
str
]
=
None
,
up
sert
_person_url
:
Optional
[
str
]
=
None
,
delete_person_url
:
Optional
[
str
]
=
None
,
import_organisations_url
:
Optional
[
str
]
=
None
,
):
""" Get endpoints relative to the CIM API URL. """
self
.
baseurl
=
url
self
.
up
date
_person_url
=
up
date
_person_url
or
"?ws=contacts/upsert/1.0"
self
.
up
sert
_person_url
=
up
sert
_person_url
or
"?ws=contacts/upsert/1.0"
self
.
delete_person_url
=
delete_person_url
or
"?ws=contacts/delete/1.0"
self
.
import_organisations_url
=
(
import_organisations_url
or
"?ws=contacts/organisations/1.0"
...
...
@@ -84,14 +84,14 @@ class CimEndpoints:
def
__repr__
(
self
)
->
str
:
return
"{cls.__name__}({url!r})"
.
format
(
cls
=
type
(
self
),
url
=
self
.
baseurl
)
def
up
date
_person
(
self
)
->
str
:
return
urllib
.
parse
.
urljoin
(
self
.
baseurl
,
self
.
up
date
_person_url
)
def
up
sert
_person
(
self
)
->
str
:
return
urllib
.
parse
.
urljoin
(
self
.
baseurl
,
self
.
up
sert
_person_url
)
def
delete_person
(
self
)
->
str
:
return
urllib
.
parse
.
urljoin
(
self
.
baseurl
,
self
.
delete_person_url
)
def
get_up
date
_person_schema
(
self
)
->
str
:
return
urllib
.
parse
.
urljoin
(
self
.
baseurl
,
self
.
up
date
_person_url
)
def
get_up
sert
_person_schema
(
self
)
->
str
:
return
urllib
.
parse
.
urljoin
(
self
.
baseurl
,
self
.
up
sert
_person_url
)
def
get_delete_person_schema
(
self
)
->
str
:
return
urllib
.
parse
.
urljoin
(
self
.
baseurl
,
self
.
delete_person_url
)
...
...
@@ -192,7 +192,7 @@ class CimClient(object):
return
data
return
cls
.
from_dict
(
data
)
def
get_up
date
_person_schema
(
self
)
->
JsonType
:
def
get_up
sert
_person_schema
(
self
)
->
JsonType
:
"""GETs the current schema from CIM
The schema can change depending on the installation.
...
...
@@ -201,7 +201,7 @@ class CimClient(object):
checks whether the schema has changed lately so you don't update
using an outdated schema.
"""
url
=
self
.
endpoints
.
get_up
date
_person_schema
()
url
=
self
.
endpoints
.
get_up
sert
_person_schema
()
response
=
self
.
get
(
url
,
auth
=
self
.
auth
,
...
...
@@ -226,7 +226,7 @@ class CimClient(object):
)
return
response
def
pos
t_person
(
def
upser
t_person
(
self
,
persondata
:
Union
[
List
[
PersonBase
],
PersonBase
,
PersonList
],
)
->
Optional
[
Tuple
[
str
,
bytes
]]:
...
...
@@ -239,7 +239,7 @@ class CimClient(object):
Note also that the response will not contain info about failing to find
someone. You will have to compare with what you posted.
"""
url
=
self
.
endpoints
.
up
date
_person
()
url
=
self
.
endpoints
.
up
sert
_person
()
if
isinstance
(
persondata
,
PersonList
):
persondata
=
persondata
.
persons
...
...
@@ -291,17 +291,6 @@ class CimClient(object):
response
.
raise_for_status
()
return
None
def
update_person
(
self
,
persons
:
Union
[
List
[
PersonBase
],
PersonBase
,
PersonList
],
)
->
Union
[
Tuple
[
str
,
bytes
],
None
]:
"""Convenience method for updating
:param persons: The person or people we want to update in CIM
:return: String describing status or None if not found
"""
return
self
.
post_person
(
persons
)
def
get_import_organisations_schema
(
self
)
->
JsonType
:
"""GETs the current schema from CIM
...
...
example-config.json
View file @
4bf4934b
...
...
@@ -3,7 +3,7 @@
"client"
:
{
"endpoints"
:
{
"url"
:
"https://api-gateway.uio.no/sws/v2/"
,
"up
date
_person_url"
:
"_webservices/?ws=contacts/upsert/1.0"
,
"up
sert
_person_url"
:
"_webservices/?ws=contacts/upsert/1.0"
,
"delete_person_url"
:
"_webservices/?ws=contacts/delete/1.0"
,
"import_organisations_url"
:
"_webservices/?ws=contacts/organisations/1.0"
},
...
...
tests/fixtures/config.json
View file @
4bf4934b
{
"endpoints"
:
{
"url"
:
"https://example.com"
,
"up
date
_person_url"
:
"update_person_url"
,
"up
sert
_person_url"
:
"update_person_url"
,
"delete_person_url"
:
"delete_person_url"
,
"import_organisations_url"
:
"import_organisations_url"
},
...
...
tests/test_client.py
View file @
4bf4934b
...
...
@@ -70,23 +70,23 @@ def test_init_from_dict(config_example):
e
=
client
.
endpoints
assert
isinstance
(
e
,
CimEndpoints
)
assert
e
.
baseurl
==
config_example
[
"endpoints"
][
"url"
]
assert
e
.
up
date
_person_url
==
"update_person_url"
assert
e
.
up
sert
_person_url
==
"update_person_url"
assert
e
.
delete_person_url
==
"delete_person_url"
assert
e
.
import_organisations_url
==
"import_organisations_url"
assert
e
.
baseurl
==
"https://example.com"
def
test_get_up
date
_person_schema
(
client
,
requests_mock
):
"""Ensure getting up
date
schema works"""
def
test_get_up
sert
_person_schema
(
client
,
requests_mock
):
"""Ensure getting up
sert
schema works"""
requests_mock
.
get
(
"https://localhost/_webservices/?ws=contacts/upsert/1.0"
,
json
=
{
"foo"
:
"bar"
}
)
response
=
client
.
get_up
date
_person_schema
()
response
=
client
.
get_up
sert
_person_schema
()
assert
response
==
{
"foo"
:
"bar"
}
def
test_get_delete_person_schema
(
client
,
requests_mock
):
"""Ensure getting
upda
te schema works"""
"""Ensure getting
dele
te schema works"""
requests_mock
.
get
(
"https://localhost/_webservices/?ws=contacts/delete/1.0"
,
json
=
{
"foo"
:
"bar"
}
)
...
...
@@ -127,42 +127,42 @@ def test_delete_person_failure_404(client, john_doe, requests_mock):
client
.
delete_person
(
Person
.
from_dict
(
john_doe
).
username
)
def
test_up
date_one
_person
(
client
,
john_doe
,
requests_mock
):
"""Ensure up
da
ting one works"""
def
test_up
sert
_person
s_one
(
client
,
john_doe
,
requests_mock
):
"""Ensure up
ser
ting one works"""
requests_mock
.
post
(
"https://localhost/_webservices/?ws=contacts/upsert/1.0"
)
response
=
client
.
up
date
_person
(
Person
.
from_dict
(
john_doe
))
response
=
client
.
up
sert
_person
(
Person
.
from_dict
(
john_doe
))
assert
response
==
(
"Import success"
,
b
""
)
def
test_up
date_many
_person
(
client
,
john_doe
,
jane_doe
,
requests_mock
):
"""Ensure up
da
ting multiple works"""
def
test_up
sert
_person
s_many
(
client
,
john_doe
,
jane_doe
,
requests_mock
):
"""Ensure up
ser
ting multiple works"""
personlist
=
[
Person
.
from_dict
(
i
)
for
i
in
(
john_doe
,
jane_doe
)]
requests_mock
.
post
(
"https://localhost/_webservices/?ws=contacts/upsert/1.0"
)
response
=
client
.
up
date
_person
(
personlist
)
response
=
client
.
up
sert
_person
(
personlist
)
assert
response
==
(
"Import success"
,
b
""
)
def
test_up
date
_failure_400
(
client
,
john_doe
,
requests_mock
):
def
test_up
sert_persons
_failure_400
(
client
,
john_doe
,
requests_mock
):
"""Ensure 400 raises an error"""
requests_mock
.
post
(
"https://localhost/_webservices/?ws=contacts/upsert/1.0"
,
status_code
=
400
)
with
pytest
.
raises
(
HTTPError
):
client
.
up
date
_person
(
Person
.
from_dict
(
john_doe
))
client
.
up
sert
_person
(
Person
.
from_dict
(
john_doe
))
def
test_up
date
_failure_500
(
client
,
john_doe
,
requests_mock
):
def
test_up
sert_persons
_failure_500
(
client
,
john_doe
,
requests_mock
):
"""Ensure 500 raises an error"""
requests_mock
.
post
(
"https://localhost/_webservices/?ws=contacts/upsert/1.0"
,
status_code
=
500
)
with
pytest
.
raises
(
HTTPError
):
client
.
up
date
_person
(
Person
.
from_dict
(
john_doe
))
client
.
up
sert
_person
(
Person
.
from_dict
(
john_doe
))
def
test_get_import_organisations_schema
(
client
,
requests_mock
):
"""Ensure getting
update
schema works"""
"""Ensure getting
import organisations
schema works"""
requests_mock
.
get
(
"https://localhost/_webservices/?ws=contacts/organisations/1.0"
,
json
=
{
"foo"
:
"bar"
},
...
...
tests/test_endpoints.py
View file @
4bf4934b
...
...
@@ -6,8 +6,8 @@ def test_init(baseurl):
assert
endpoints
.
baseurl
==
baseurl
def
test_up
date
_person
(
baseurl
,
endpoints
):
url
=
endpoints
.
up
date
_person
()
def
test_up
sert
_person
(
baseurl
,
endpoints
):
url
=
endpoints
.
up
sert
_person
()
assert
url
==
baseurl
+
"?ws=contacts/upsert/1.0"
...
...
@@ -16,8 +16,8 @@ def test_delete_person(baseurl, endpoints):
assert
url
==
baseurl
+
"?ws=contacts/delete/1.0"
def
test_get_up
date
_person_schema
(
baseurl
,
endpoints
):
url
=
endpoints
.
get_up
date
_person_schema
()
def
test_get_up
sert
_person_schema
(
baseurl
,
endpoints
):
url
=
endpoints
.
get_up
sert
_person_schema
()
assert
url
==
baseurl
+
"?ws=contacts/upsert/1.0"
...
...
@@ -26,9 +26,9 @@ def test_get_delete_person_schema(baseurl, endpoints):
assert
url
==
baseurl
+
"?ws=contacts/delete/1.0"
def
test_custom_up
date
_person
(
custom_endpoints
,
baseurl
):
def
test_custom_up
sert
_person
(
custom_endpoints
,
baseurl
):
assert
(
custom_endpoints
.
up
date
_person
()
==
baseurl
+
"custom/?ws=contacts/upsert/1.0"
custom_endpoints
.
up
sert
_person
()
==
baseurl
+
"custom/?ws=contacts/upsert/1.0"
)
...
...
@@ -38,9 +38,9 @@ def test_custom_delete_person(custom_endpoints, baseurl):
)
def
test_custom_get_up
date
_person_schema
(
custom_endpoints
,
baseurl
):
def
test_custom_get_up
sert
_person_schema
(
custom_endpoints
,
baseurl
):
assert
(
custom_endpoints
.
get_up
date
_person_schema
()
custom_endpoints
.
get_up
sert
_person_schema
()
==
baseurl
+
"custom/?ws=contacts/upsert/1.0"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment