Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
eb618109
Commit
eb618109
authored
Nov 10, 2016
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(HttpAPI)(python): заложил скелет реализации python API для работы с Http API uniset
parent
c72fbd62
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
109 additions
and
0 deletions
+109
-0
UniSetHTTP.py
python/lib/UniSetHTTP.py
+82
-0
testUHttp.py
python/tests/UHttp/testUHttp.py
+27
-0
No files found.
python/lib/UniSetHTTP.py
0 → 100644
View file @
eb618109
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016 Pavel Vainerman <pv@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, version 2.1.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import
json
import
urllib2
UHTTP_API_VERSION
=
'v01'
class
UHTTPError
:
def
__init__
(
self
,
err
=
''
):
self
.
message
=
err
class
UniSetHTTPService
:
def
__init__
(
self
,
_settings
):
self
.
settings
=
_settings
if
'url'
not
in
self
.
settings
:
raise
ValueError
(
'В настройках нужно задать url'
)
self
.
apiver
=
UHTTP_API_VERSION
if
'api'
in
self
.
settings
:
self
.
aviver
=
self
.
settings
[
'api'
]
def
request
(
self
,
query
,
method
=
'GET'
,
data
=
None
):
"""Послать запрос и получить ответ."""
url
=
self
.
settings
.
get
(
'url'
)
+
"/api/"
+
self
.
apiver
+
query
headers
=
{}
if
data
is
not
None
:
data
=
urllib
.
urlencode
(
data
)
try
:
request
=
urllib2
.
Request
(
url
,
data
,
headers
)
request
.
get_method
=
lambda
:
method
resp
=
urllib2
.
urlopen
(
request
)
return
json
.
loads
(
resp
.
read
())
except
urllib2
.
URLError
,
e
:
if
hasattr
(
e
,
'reason'
):
err
=
'We failed to reach a server. Reason: '
,
e
.
reason
raise
UHTTPError
(
err
)
elif
hasattr
(
e
,
'code'
):
err
=
'The server couldn
\'
t fulfill the request. Error code: '
,
e
.
code
raise
UHTTPError
(
err
)
raise
UHTTPError
(
'Unknown error'
)
class
SharedMemoryAPI
(
UniSetHTTPService
):
"""Работа с SharedMemory."""
def
__init__
(
self
,
_settings
):
UniSetHTTPService
.
__init__
(
self
,
_settings
)
if
'smID'
not
in
self
.
settings
:
raise
ValueError
(
'(SharedMemoryAPI): В настройках нужно задать smID'
)
def
request
(
self
,
query
,
method
=
'GET'
,
data
=
None
):
return
UniSetHTTPService
.
request
(
self
,
'/'
+
self
.
settings
[
'smID'
]
+
'/'
+
query
)
def
consumers
(
self
,
sens
=
''
):
"""Получить список заказчиков"""
query
=
'/consumers'
if
sens
!=
''
:
query
+=
'?
%
s'
%
sens
return
self
.
request
(
query
)[
self
.
settings
[
'smID'
]][
'consumers'
]
python/tests/UHttp/testUHttp.py
0 → 100755
View file @
eb618109
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
sys
sys
.
path
.
append
(
'../../'
)
sys
.
path
.
append
(
'../../lib'
)
sys
.
path
.
append
(
'../../lib/pyUniSet/'
)
sys
.
path
.
append
(
'../../lib/pyUniSet/.libs'
)
# from lib import *
from
UniSetHTTP
import
*
if
__name__
==
"__main__"
:
try
:
settings
=
{}
settings
[
'url'
]
=
'http://localhost:8080'
settings
[
'smID'
]
=
'SharedMemory'
shm
=
SharedMemoryAPI
(
settings
)
print
shm
.
consumers
()
except
UHTTPError
,
e
:
print
e
.
message
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