Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Briar GTK
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
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
Briar GTK
Commits
871bc44c
Commit
871bc44c
authored
5 years ago
by
Nico
Browse files
Options
Downloads
Patches
Plain Diff
Remove copy of briar-cli-python-demo
parent
235cd02e
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/briar/api/briar.py
+0
-92
0 additions, 92 deletions
src/briar/api/briar.py
with
0 additions
and
92 deletions
src/briar/api/briar.py
deleted
100644 → 0
+
0
−
92
View file @
235cd02e
# Copyright (c) 2019 Torsten Grote
# SPDX-License-Identifier: AGPL-3.0-only
# License-Filename: LICENSE.md
#
# Originally coming from
# https://code.briarproject.org/grote/briar-cli-python-demo/blob/master/briar-cli.py
import
asyncio
import
json
import
sys
import
threading
from
datetime
import
datetime
import
requests
import
websockets
HOST
=
'
localhost:7000
'
CONTACT_ID
=
'
1
'
URL
=
'
http://%s/v1/messages/%s
'
%
(
HOST
,
CONTACT_ID
)
URL_WS
=
'
ws://%s/v1/ws
'
%
HOST
PROMPT
=
'
$>
'
def
main
():
if
len
(
sys
.
argv
)
!=
2
:
print
(
"
Please provide auth token: %s <token>
"
%
sys
.
argv
[
0
])
sys
.
exit
(
1
)
auth_token
=
sys
.
argv
[
1
]
load_history
(
auth_token
)
threading
.
Thread
(
target
=
get_message_stdin
,
args
=
[
auth_token
]).
start
()
loop
=
asyncio
.
get_event_loop
()
loop
.
create_task
(
connect_websocket
(
auth_token
))
loop
.
run_forever
()
loop
.
close
()
async
def
connect_websocket
(
token
):
async
with
websockets
.
connect
(
URL_WS
)
as
ws
:
await
ws
.
send
(
token
)
await
get_message_websocket
(
ws
)
async
def
get_message_websocket
(
ws
):
while
not
ws
.
closed
and
not
asyncio
.
get_event_loop
().
is_closed
():
message
=
await
ws
.
recv
()
m
=
json
.
loads
(
message
)
if
m
[
'
name
'
]
==
'
ConversationMessageReceivedEvent
'
:
print
()
# line-break
print_message
(
m
[
'
data
'
])
print
(
PROMPT
,
end
=
''
,
flush
=
True
)
if
not
asyncio
.
get_event_loop
().
is_closed
():
asyncio
.
get_event_loop
().
create_task
(
get_message_websocket
(
ws
))
def
get_message_stdin
(
token
):
while
True
:
body
=
input
(
PROMPT
)
if
len
(
body
)
>
0
:
res
=
requests
.
post
(
URL
,
headers
=
get_auth_header
(
token
),
json
=
{
'
text
'
:
body
})
if
res
.
status_code
==
200
:
print_message
(
res
.
json
())
else
:
print
(
"
Error: %d
"
%
res
.
status_code
)
def
print_message
(
message
):
prefix
=
'
>
'
if
message
[
'
local
'
]
else
'
<
'
time
=
get_timestamp
(
datetime
.
fromtimestamp
(
message
[
'
timestamp
'
]
/
1000
))
print
(
f
"
{
time
}
{
prefix
}
{
message
[
'
text
'
]
}
"
)
def
get_timestamp
(
date
):
return
date
.
strftime
(
'
%Y-%m-%dT%H:%M:%S
'
)
def
load_history
(
token
):
history
=
requests
.
get
(
URL
,
headers
=
get_auth_header
(
token
)).
json
()
for
message
in
history
:
print_message
(
message
)
def
get_auth_header
(
token
):
return
{
'
Authorization
'
:
'
Bearer %s
'
%
token
}
if
__name__
==
"
__main__
"
:
main
()
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