Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Python Briar Wrapper
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
briar
Python Briar Wrapper
Commits
a121c076
Commit
a121c076
authored
4 years ago
by
Nico
Browse files
Options
Downloads
Patches
Plain Diff
Add method to watch connection state of contacts
Related to: *
briar-gtk#19
*
briar!1260
parent
acd8c780
No related branches found
No related tags found
No related merge requests found
Pipeline
#4630
failed
4 years ago
Stage: test
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
briar_wrapper/models/contacts.py
+22
-0
22 additions, 0 deletions
briar_wrapper/models/contacts.py
tests/briar_wrapper/models/test_contacts.py
+27
-0
27 additions, 0 deletions
tests/briar_wrapper/models/test_contacts.py
with
49 additions
and
0 deletions
briar_wrapper/models/contacts.py
+
22
−
0
View file @
a121c076
...
@@ -15,6 +15,9 @@ from briar_wrapper.model import Model
...
@@ -15,6 +15,9 @@ from briar_wrapper.model import Model
class
Contacts
(
Model
):
class
Contacts
(
Model
):
API_ENDPOINT
=
"
contacts/
"
API_ENDPOINT
=
"
contacts/
"
CONNECTION_EVENTS
=
(
"
ContactConnectedEvent
"
,
"
ContactDisconnectedEvent
"
)
_connections_callback
=
None
def
add_pending
(
self
,
link
,
alias
):
def
add_pending
(
self
,
link
,
alias
):
url
=
urljoin
(
BASE_HTTP_URL
,
self
.
API_ENDPOINT
+
"
add/pending/
"
)
url
=
urljoin
(
BASE_HTTP_URL
,
self
.
API_ENDPOINT
+
"
add/pending/
"
)
...
@@ -32,6 +35,25 @@ class Contacts(Model):
...
@@ -32,6 +35,25 @@ class Contacts(Model):
request
=
_get
(
url
,
headers
=
self
.
_headers
).
json
()
request
=
_get
(
url
,
headers
=
self
.
_headers
).
json
()
return
request
[
'
link
'
]
return
request
[
'
link
'
]
def
watch_connections
(
self
,
callback
):
self
.
_connections_callback
=
callback
signal_ids
=
list
()
event_callback
=
self
.
_handle_connections_callback
for
event
in
self
.
CONNECTION_EVENTS
:
signal_id
=
self
.
_api
.
socket_listener
.
connect
(
event
,
event_callback
)
signal_ids
.
append
(
signal_id
)
return
signal_ids
def
_handle_connections_callback
(
self
,
message
):
contact_id
=
message
[
"
data
"
][
"
contactId
"
]
if
message
[
"
name
"
]
==
"
ContactConnectedEvent
"
:
self
.
_connections_callback
(
contact_id
,
True
)
elif
message
[
"
name
"
]
==
"
ContactDisconnectedEvent
"
:
self
.
_connections_callback
(
contact_id
,
False
)
else
:
raise
Exception
(
f
"
Wrong event in callback:
{
message
[
'
name
'
]
}
"
)
def
_sort_contact_list
(
contacts
):
def
_sort_contact_list
(
contacts
):
contacts
.
sort
(
key
=
itemgetter
(
"
lastChatActivity
"
),
contacts
.
sort
(
key
=
itemgetter
(
"
lastChatActivity
"
),
reverse
=
True
)
reverse
=
True
)
...
...
This diff is collapsed.
Click to expand it.
tests/briar_wrapper/models/test_contacts.py
+
27
−
0
View file @
a121c076
...
@@ -7,6 +7,7 @@ import json
...
@@ -7,6 +7,7 @@ import json
import
requests_mock
import
requests_mock
from
briar_wrapper.models.contacts
import
Contacts
from
briar_wrapper.models.contacts
import
Contacts
from
briar_wrapper.models.socket_listener
import
SocketListener
BASE_HTTP_URL
=
"
http://localhost:7000/v1/contacts/
"
BASE_HTTP_URL
=
"
http://localhost:7000/v1/contacts/
"
...
@@ -96,3 +97,29 @@ def test_get_link(api, request_headers, requests_mock):
...
@@ -96,3 +97,29 @@ def test_get_link(api, request_headers, requests_mock):
request_headers
=
request_headers
,
request_headers
=
request_headers
,
text
=
json
.
dumps
(
response
))
text
=
json
.
dumps
(
response
))
assert
contacts
.
get_link
()
==
TEST_LINK
assert
contacts
.
get_link
()
==
TEST_LINK
def
test_watch_signal_added
(
api
,
mocker
):
event_mock
=
mocker
.
MagicMock
()
callback_mock
=
mocker
.
MagicMock
()
contacts
=
Contacts
(
api
)
contacts
.
_api
.
socket_listener
=
SocketListener
(
None
)
contacts
.
_api
.
socket_listener
.
_highest_signal_id
=
136
assert
contacts
.
_api
.
socket_listener
.
_signals
==
dict
()
contacts
.
watch_connections
(
callback_mock
)
expected_signals
=
{
137
:
{
"
event
"
:
"
ContactConnectedEvent
"
,
"
callback
"
:
callback_mock
},
138
:
{
"
event
"
:
"
ContactDisconnectedEvent
"
,
"
callback
"
:
callback_mock
}
}
assert
contacts
.
_api
.
socket_listener
.
_signals
==
expected_signals
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment