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
Merge requests
!6
Full test coverage for Api and ApiThread
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Full test coverage for Api and ApiThread
api-tests
into
master
Overview
0
Commits
1
Pipelines
2
Changes
5
Merged
Nico
requested to merge
api-tests
into
master
4 years ago
Overview
0
Commits
1
Pipelines
2
Changes
5
Expand
0
0
Merge request reports
Compare
master
version 1
fe44110c
4 years ago
master (base)
and
latest version
latest version
32a5e05f
1 commit,
4 years ago
version 1
fe44110c
1 commit,
4 years ago
5 files
+
378
−
135
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
5
Search (e.g. *.vue) (Ctrl+P)
briar_wrapper/api.py
+
13
−
48
Options
@@ -3,13 +3,10 @@
# License-Filename: LICENSE.md
import
os
from
subprocess
import
Popen
,
PIPE
,
STDOUT
from
threading
import
Thread
from
time
import
sleep
from
urllib.error
import
HTTPError
,
URLError
from
urllib.request
import
urlopen
from
briar_wrapper.constants
import
BASE_HTTP_URL
,
BRIAR_AUTH_TOKEN
,
BRIAR_DB
from
briar_wrapper.api_thread
import
ApiThread
from
briar_wrapper.constants
import
BRIAR_AUTH_TOKEN
,
BRIAR_DB
from
briar_wrapper.models.socket_listener
import
SocketListener
@@ -18,76 +15,44 @@ class Api:
auth_token
=
None
socket_listener
=
None
_
process
=
None
_
api_thread
=
None
def
__init__
(
self
,
headless_jar
):
self
.
_
command
=
[
"
java
"
,
"
-jar
"
,
headless_jar
]
self
.
_
api_thread
=
ApiThread
(
self
,
headless_jar
)
@staticmethod
def
has_account
():
return
os
.
path
.
isfile
(
BRIAR_DB
)
def
is_running
(
self
):
return
(
self
.
_
process
is
not
None
)
and
(
self
.
_process
.
poll
()
is
None
)
return
self
.
_
api_thread
.
is_running
(
)
def
login
(
self
,
password
,
callback
):
self
.
_start_and_watch
(
callback
)
startup_thread
=
Thread
(
target
=
self
.
_
login
,
args
=
(
password
,)
,
daemon
=
True
)
startup_thread
=
Thread
(
target
=
self
.
_
api_thread
.
login
,
args
=
(
password
,),
daemon
=
True
)
startup_thread
.
start
()
def
register
(
self
,
credentials
,
callback
):
if
len
(
credentials
)
!=
2
:
raise
Exception
(
"
Can
'
t process credentials
"
)
self
.
_start_and_watch
(
callback
)
startup_thread
=
Thread
(
target
=
self
.
_
register
,
args
=
(
credentials
,)
,
daemon
=
True
)
startup_thread
=
Thread
(
target
=
self
.
_
api_thread
.
register
,
args
=
(
credentials
,),
daemon
=
True
)
startup_thread
.
start
()
def
stop
(
self
):
if
not
self
.
is_running
():
raise
Exception
(
"
Nothing to stop
"
)
self
.
_process
.
terminate
()
self
.
_api_thread
.
stop
()
def
_start_and_watch
(
self
,
callback
):
if
self
.
is_running
():
raise
Exception
(
"
API already running
"
)
self
.
_process
=
Popen
(
self
.
_command
,
stdin
=
PIPE
,
stdout
=
PIPE
,
stderr
=
STDOUT
)
watch_thread
=
Thread
(
target
=
self
.
_watch_thread
,
args
=
(
callback
,),
daemon
=
True
)
watch_thread
.
start
()
self
.
_api_thread
.
start
()
self
.
_api_thread
.
watch
(
callback
)
def
_watch_thread
(
self
,
callback
):
while
self
.
is_running
():
try
:
urlopen
(
BASE_HTTP_URL
)
sleep
(
0.1
)
except
HTTPError
as
http_error
:
if
http_error
.
code
==
404
:
return
self
.
_on_successful_startup
(
callback
)
except
URLError
as
url_error
:
if
not
isinstance
(
url_error
.
reason
,
ConnectionRefusedError
):
raise
url_error
callback
(
False
)
def
_on_successful_startup
(
self
,
callback
):
def
on_successful_startup
(
self
,
callback
):
self
.
_load_auth_token
()
self
.
socket_listener
=
SocketListener
(
self
)
callback
(
True
)
def
_login
(
self
,
password
):
if
not
self
.
is_running
():
raise
Exception
(
"
Can
'
t login; API not running
"
)
self
.
_process
.
communicate
((
f
"
{
password
}
\n
"
).
encode
(
"
utf-8
"
))
def
_register
(
self
,
credentials
):
if
not
self
.
is_running
():
raise
Exception
(
"
Can
'
t register; API not running
"
)
self
.
_process
.
communicate
((
credentials
[
0
]
+
'
\n
'
+
credentials
[
1
]
+
'
\n
'
+
credentials
[
1
]
+
'
\n
'
).
encode
(
"
utf-8
"
))
def
_load_auth_token
(
self
):
if
not
Api
.
has_account
():
raise
Exception
(
"
Can
'
t load authentication token
"
)
Loading