Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
aa.gusti
/
opensipkd-tools
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 4a8316ba
authored
Aug 10, 2022
by
aagusti
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
perbaikan rpc error
1 parent
36805cfc
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
35 deletions
opensipkd/tools/api.py
opensipkd/tools/form_api.py
opensipkd/tools/api.py
View file @
4a8316b
...
@@ -6,6 +6,7 @@ import sys
...
@@ -6,6 +6,7 @@ import sys
from
datetime
import
datetime
,
date
from
datetime
import
datetime
,
date
import
requests
import
requests
from
pyramid.i18n
import
TranslationStringFactory
from
pyramid_rpc.jsonrpc
import
JsonRpcError
from
pyramid_rpc.jsonrpc
import
JsonRpcError
from
opensipkd.tools
import
get_settings
,
get_random_number
from
opensipkd.tools
import
get_settings
,
get_random_number
...
@@ -17,75 +18,87 @@ MSG_OK = 'Data Submitted'
...
@@ -17,75 +18,87 @@ MSG_OK = 'Data Submitted'
import
logging
import
logging
log
=
logging
.
getLogger
(
__name__
)
log
=
logging
.
getLogger
(
__name__
)
_
=
TranslationStringFactory
(
'json_error'
)
class
JsonRpcOtherError
(
JsonRpcError
):
class
JsonRpcOtherError
(
JsonRpcError
):
code
=
-
32604
code
=
-
32604
message
=
'Other Error'
message
=
_
(
'Other Error'
)
# error terkait database
# error terkait database
class
JsonRpcNotImplementedError
(
JsonRpcError
):
class
JsonRpcNotImplementedError
(
JsonRpcError
):
code
=
-
40000
code
=
-
40000
message
=
"Method Not Implemented"
message
=
_
(
"Method Not Implemented"
)
class
JsonRpcDbUrlNotFoundError
(
JsonRpcError
):
class
JsonRpcDbUrlNotFoundError
(
JsonRpcError
):
code
=
-
40001
code
=
-
40001
message
=
"DB URL Connection Not Found"
message
=
_
(
"DB URL Connection Not Found"
)
class
JsonRpcDbConnectionError
(
JsonRpcError
):
class
JsonRpcDbConnectionError
(
JsonRpcError
):
code
=
-
40002
code
=
-
40002
message
=
"DB Connection Not Found"
message
=
_
(
"DB Connection Not Found"
)
class
JsonRpcDataNotFoundError
(
JsonRpcError
):
class
JsonRpcDataNotFoundError
(
JsonRpcError
):
code
=
-
40003
code
=
-
40003
message
=
"Error Data Not Found"
message
=
_
(
"Error Data Not Found"
)
class
JsonRpcDataFoundError
(
JsonRpcError
):
class
JsonRpcDataFoundError
(
JsonRpcError
):
code
=
-
40004
code
=
-
40004
message
=
"Error Data Found"
message
=
_
(
"Error Data Already Found"
)
class
JsonRpcInvalidLoginError
(
JsonRpcError
):
class
JsonRpcInvalidLoginError
(
JsonRpcError
):
code
=
-
41001
code
=
-
41001
message
=
"Invalid RPC User/Password"
message
=
_
(
"Invalid RPC User/Password"
)
class
JsonRpcInvalidNikError
(
JsonRpcError
):
class
JsonRpcInvalidNikError
(
JsonRpcError
):
code
=
-
41002
code
=
-
41002
message
=
'NIK Tidak Valid'
message
=
_
(
'Invalid NIK'
)
class
JsonRpcNikFoundError
(
JsonRpcError
):
class
JsonRpcNikFoundError
(
JsonRpcError
):
code
=
-
41003
code
=
-
41003
message
=
'NIK Sudah Ada'
message
=
_
(
'NIK already found'
)
class
JsonRpcMobileFoundError
(
JsonRpcError
):
class
JsonRpcMobileFoundError
(
JsonRpcError
):
code
=
-
41004
code
=
-
41004
message
=
'No HP Sudah Digunakan'
message
=
_
(
'Mobile number already found'
)
class
JsonRpcInvalidMobileError
(
JsonRpcError
):
class
JsonRpcInvalidMobileError
(
JsonRpcError
):
code
=
-
41004
code
=
-
41004
message
=
'No HP Tidak Valid'
message
=
_
(
'Invalid Mobile Number'
)
class
JsonRpcInvalidDataError
(
JsonRpcError
):
class
JsonRpcInvalidDataError
(
JsonRpcError
):
code
=
-
41005
code
=
-
41005
message
=
'Data Tidak Valid'
message
=
_
(
'Data Invalid'
)
class
JsonRpcInvalidData
(
JsonRpcError
):
code
=
-
41005
message
=
_
(
'Invalid Data'
)
class
JsonRpcEmailFound
Error
(
JsonRpcError
):
class
JsonRpcEmailFound
(
JsonRpcError
):
code
=
-
41006
code
=
-
41006
message
=
'e-mail Sudah Digunakan'
message
=
_
(
'e-mail already found'
)
class
JsonRpcEmailFoundError
(
JsonRpcEmailFound
):
"""Compatibilty"""
class
JsonRpcInvalidEmailError
(
JsonRpcError
):
class
JsonRpcInvalidEmailError
(
JsonRpcError
):
code
=
-
41007
code
=
-
41007
message
=
'e-mail Tidak Valid'
message
=
_
(
'Invalid e-mail'
)
class
JsonRpcMailError
(
JsonRpcError
):
class
JsonRpcMailError
(
JsonRpcError
):
...
@@ -93,19 +106,27 @@ class JsonRpcMailError(JsonRpcError):
...
@@ -93,19 +106,27 @@ class JsonRpcMailError(JsonRpcError):
message
=
'Gagal autentikasi mail server'
message
=
'Gagal autentikasi mail server'
class
JsonRpcUserFound
Error
(
JsonRpcError
):
class
JsonRpcUserFound
(
JsonRpcError
):
code
=
-
41009
code
=
-
41009
message
=
'User sudah digunakan'
message
=
_
(
'User already found'
)
class
JsonRpcUserFoundError
(
JsonRpcUserFound
):
"""Compatibity """
class
JsonRpcRegisterFailError
(
JsonRpcError
):
class
JsonRpcRegisterFailError
(
JsonRpcError
):
code
=
-
41010
code
=
-
41010
message
=
'Gagal Registrasi User'
message
=
_
(
'User Registration Failed '
)
class
JsonRpcProfileFail
Error
(
JsonRpcError
):
class
JsonRpcProfileFail
(
JsonRpcError
):
code
=
-
41011
code
=
-
41011
message
=
'Gagal Update Profile'
message
=
_
(
'Update Profile Failed'
)
class
JsonRpcProfileFailError
(
JsonRpcError
):
"""Portabitas"""
class
JsonRpcGetPasswordError
(
JsonRpcError
):
class
JsonRpcGetPasswordError
(
JsonRpcError
):
...
@@ -218,7 +239,7 @@ class JsonRpcBillerError(JsonRpcError):
...
@@ -218,7 +239,7 @@ class JsonRpcBillerError(JsonRpcError):
class
JsonRpcProdukNotFoundError
(
JsonRpcError
):
class
JsonRpcProdukNotFoundError
(
JsonRpcError
):
code
=
-
56001
code
=
-
56001
message
=
'Produk Not Found'
message
=
_
(
'Items Not Found'
)
# Biller Status
# Biller Status
...
@@ -277,15 +298,16 @@ def json_rpc_header(userid, password, key=None):
...
@@ -277,15 +298,16 @@ def json_rpc_header(userid, password, key=None):
if
sys
.
version
<
'3'
:
if
sys
.
version
<
'3'
:
encoded_signature
=
base64
.
encodestring
(
signature
)
.
replace
(
'
\n
'
,
''
)
encoded_signature
=
base64
.
encodestring
(
signature
)
.
replace
(
'
\n
'
,
''
)
else
:
else
:
encoded_signature
=
base64
.
encodebytes
(
signature
)
.
decode
()
.
replace
(
'
\n
'
,
''
)
encoded_signature
=
base64
.
encodebytes
(
signature
)
.
decode
()
.
replace
(
'
\n
'
,
''
)
return
dict
(
userid
=
userid
,
signature
=
encoded_signature
,
key
=
key
)
return
dict
(
userid
=
userid
,
signature
=
encoded_signature
,
key
=
key
)
def
get_mandatory
(
data
,
values
):
def
get_mandatory
(
data
,
values
):
for
value
in
values
:
for
value
in
values
:
if
value
not
in
data
or
not
data
[
value
]:
if
value
not
in
data
or
not
data
[
value
]:
raise
JsonRpcInvalidDataError
(
message
=
"{} Not Found"
.
format
(
value
))
raise
JsonRpcDataInvalid
(
message
=
"{} Not Found"
.
format
(
value
))
def
validate_time
(
request
):
def
validate_time
(
request
):
global
lima_menit
global
lima_menit
...
@@ -302,11 +324,13 @@ def validate_time(request):
...
@@ -302,11 +324,13 @@ def validate_time(request):
return
time_stamp
return
time_stamp
def
get_jsonrpc
(
method
,
params
,
**
kwargs
):
def
get_jsonrpc
(
method
,
params
,
**
kwargs
):
rid
=
kwargs
.
get
(
"id"
)
or
int
(
get_random_number
(
6
))
rid
=
kwargs
.
get
(
"id"
)
or
int
(
get_random_number
(
6
))
return
dict
(
jsonrpc
=
'2.0'
,
method
=
method
,
params
=
params
,
return
dict
(
jsonrpc
=
'2.0'
,
method
=
method
,
params
=
params
,
id
=
rid
)
id
=
rid
)
def
send_rpc
(
auth
,
message
):
def
send_rpc
(
auth
,
message
):
"""
"""
Digunakan untuk mengirim data dengan methode JSONRPC 2.0 with os-auth
Digunakan untuk mengirim data dengan methode JSONRPC 2.0 with os-auth
...
@@ -330,7 +354,8 @@ def send_rpc(auth, message):
...
@@ -330,7 +354,8 @@ def send_rpc(auth, message):
log
.
info
(
"URL:{} timeout:{} detik"
.
format
(
url
,
timeout
))
log
.
info
(
"URL:{} timeout:{} detik"
.
format
(
url
,
timeout
))
log
.
warning
(
"REQUEST {}"
.
format
(
data
))
log
.
warning
(
"REQUEST {}"
.
format
(
data
))
try
:
try
:
results
=
requests
.
post
(
url
,
json
=
data
,
headers
=
headers
,
timeout
=
timeout
)
# data=jsondata,
results
=
requests
.
post
(
url
,
json
=
data
,
headers
=
headers
,
timeout
=
timeout
)
# data=jsondata,
except
Exception
as
e
:
except
Exception
as
e
:
log
.
warning
(
str
(
e
))
log
.
warning
(
str
(
e
))
return
return
...
@@ -342,14 +367,16 @@ def send_rpc(auth, message):
...
@@ -342,14 +367,16 @@ def send_rpc(auth, message):
log
.
info
(
"RESPONSE {}"
.
format
(
rows
))
log
.
info
(
"RESPONSE {}"
.
format
(
rows
))
return
rows
return
rows
##############################
##############################
# Web Service Authentication #
# Web Service Authentication #
##############################
##############################
lima_menit
=
300
lima_menit
=
300
def
json_serial
(
obj
):
def
json_serial
(
obj
):
"""JSON serializer for objects not serializable by default json code"""
"""JSON serializer for objects not serializable by default json code"""
if
isinstance
(
obj
,
(
datetime
,
date
)):
if
isinstance
(
obj
,
(
datetime
,
date
)):
return
obj
.
isoformat
()
return
obj
.
isoformat
()
raise
TypeError
(
"Type
%
s not serializable"
%
type
(
obj
))
\ No newline at end of file
\ No newline at end of file
raise
TypeError
(
"Type
%
s not serializable"
%
type
(
obj
))
opensipkd/tools/form_api.py
View file @
4a8316b
import
logging
import
traceback
import
traceback
import
colander
import
colander
import
deform.widget
import
deform.widget
import
logging
from
icecream
import
ic
from
icecream
import
ic
log
=
logging
.
getLogger
(
__name__
)
log
=
logging
.
getLogger
(
__name__
)
...
@@ -38,19 +39,22 @@ field2remove = (
...
@@ -38,19 +39,22 @@ field2remove = (
def
form2dict
(
obj
):
def
form2dict
(
obj
):
d
=
{}
d
=
{}
if
getattr
(
obj
,
"__dict__"
,
str
(
obj
)
):
if
hasattr
(
obj
,
"__dict__"
):
for
k
,
v
in
obj
.
__dict__
.
items
():
for
k
,
v
in
obj
.
__dict__
.
items
():
d
[
k
]
=
v
d
[
k
]
=
v
else
:
d
=
obj
ic
(
d
)
for
k
,
v
in
d
.
items
():
for
k
,
v
in
d
.
items
():
ic
(
k
,
v
,
type
(
v
))
try
:
try
:
if
k
in
field2remove
:
if
k
in
field2remove
:
d
[
k
]
=
""
d
[
k
]
=
""
elif
type
(
v
)
==
colander
.
Invalid
:
elif
type
(
v
)
==
colander
.
Invalid
:
d
[
k
]
=
v
.
asdict
()
d
[
k
]
=
v
.
asdict
()
elif
type
(
v
)
==
type
(
colander
.
null
):
elif
v
and
hasattr
(
v
,
"__dict__"
)
and
\
d
[
k
]
=
None
getattr
(
v
,
"__dict__"
,
str
(
v
)):
elif
isinstance
(
v
,
dict
):
log
.
debug
(
v
)
d
[
k
]
=
form2dict
(
v
)
d
[
k
]
=
form2dict
(
v
)
elif
isinstance
(
v
,
list
):
elif
isinstance
(
v
,
list
):
lv
=
[]
lv
=
[]
...
@@ -59,6 +63,12 @@ def form2dict(obj):
...
@@ -59,6 +63,12 @@ def form2dict(obj):
getattr
(
l
,
"__dict__"
,
str
(
l
)):
getattr
(
l
,
"__dict__"
,
str
(
l
)):
lv
.
append
(
form2dict
(
l
))
lv
.
append
(
form2dict
(
l
))
d
[
k
]
=
lv
d
[
k
]
=
lv
elif
v
and
hasattr
(
v
,
"__dict__"
)
and
\
getattr
(
v
,
"__dict__"
,
str
(
v
)):
log
.
debug
(
v
)
d
[
k
]
=
form2dict
(
v
)
elif
getattr
(
v
,
"__dict__"
,
str
(
v
))
==
{}:
elif
getattr
(
v
,
"__dict__"
,
str
(
v
))
==
{}:
d
[
k
]
=
{}
d
[
k
]
=
{}
...
@@ -71,11 +81,10 @@ def form2dict(obj):
...
@@ -71,11 +81,10 @@ def form2dict(obj):
if
k
==
"missing"
and
v
==
colander
.
drop
:
if
k
==
"missing"
and
v
==
colander
.
drop
:
d
[
k
]
=
True
d
[
k
]
=
True
except
Exception
as
e
:
except
Exception
as
e
:
log
.
error
(
e
)
log
.
error
(
e
)
traceback
.
print_exc
()
traceback
.
print_exc
()
ic
(
d
)
return
d
return
d
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment