Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
briar-cli-python-demo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
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
Torsten Grote
briar-cli-python-demo
Commits
677277c9
Verified
Commit
677277c9
authored
6 years ago
by
Torsten Grote
Browse files
Options
Downloads
Patches
Plain Diff
Add support for Authorization Token
parent
cdb4b463
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
briar-cli.py
+29
-16
29 additions, 16 deletions
briar-cli.py
with
29 additions
and
16 deletions
briar-cli.py
+
29
−
16
View file @
677277c9
...
...
@@ -2,6 +2,7 @@
import
asyncio
import
json
import
sys
import
threading
from
datetime
import
datetime
...
...
@@ -18,33 +19,41 @@ PROMPT = '$> '
def
main
():
load_history
()
if
len
(
sys
.
argv
)
!=
2
:
print
(
"
Please provide auth token: %s <token>
"
%
sys
.
argv
[
0
])
sys
.
exit
(
1
)
threading
.
Thread
(
target
=
get_message_stdin
).
start
()
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
(
get_message_websocket
())
loop
.
create_task
(
get_message_websocket
(
auth_token
))
loop
.
run_forever
()
loop
.
close
()
async
def
get_message_websocket
():
async
with
websockets
.
connect
(
URL_WS
)
as
ws
:
message
=
await
ws
.
recv
()
m
=
json
.
loads
(
message
)
if
m
[
'
name
'
]
==
'
org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent
'
:
print
()
# line-break
print_message
(
m
[
'
data
'
])
print
(
PROMPT
,
end
=
''
,
flush
=
True
)
async
def
get_message_websocket
(
token
):
url
=
URL_WS
.
replace
(
'
ws://
'
,
'
ws://%s@
'
%
token
)
async
with
websockets
.
connect
(
url
)
as
ws
:
while
not
ws
.
closed
and
not
asyncio
.
get_event_loop
().
is_closed
():
message
=
await
ws
.
recv
()
m
=
json
.
loads
(
message
)
if
m
[
'
name
'
]
==
'
org.briarproject.briar.api.messaging
'
+
\
'
.event.PrivateMessageReceivedEvent
'
:
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
())
asyncio
.
get_event_loop
().
create_task
(
get_message_websocket
(
token
))
def
get_message_stdin
():
def
get_message_stdin
(
token
):
while
True
:
body
=
input
(
PROMPT
)
if
len
(
body
)
>
0
:
print_message
(
requests
.
post
(
URL
,
data
=
{
'
message
'
:
body
}).
json
())
res
=
requests
.
post
(
URL
,
headers
=
get_auth_header
(
token
),
data
=
{
'
message
'
:
body
}).
json
()
print_message
(
res
)
def
print_message
(
message
):
...
...
@@ -57,11 +66,15 @@ def get_timestamp(date):
return
date
.
strftime
(
'
%Y-%m-%dT%H:%M:%S
'
)
def
load_history
():
history
=
requests
.
get
(
URL
).
json
()
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