FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
openapi/python/src/http_client.rs at main · tsui66/openapi · GitHub
tsui66
/
openapi
Public
forked from
longbridge/openapi
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
openapi
/
python
/
src
/
http_client.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
85 lines (78 loc) · 2.76 KB
Breadcrumbs
openapi
/
python
/
src
/
http_client.rs
Copy path
File metadata and controls
85 lines (78 loc) · 2.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use
std
::
collections
::
HashMap
;
use
longport
::
httpclient
::
{
HttpClient
as
LbHttpClient
,
HttpClientConfig
,
HttpClientError
,
Json
,
Method
,
}
;
use
pyo3
::
{
exceptions
::
PyRuntimeError
,
prelude
::
*
,
types
::
PyType
}
;
use
serde_json
::
Value
;
use
crate
::
error
::
ErrorNewType
;
#
[
pyclass
]
pub
(
crate
)
struct
HttpClient
(
LbHttpClient
)
;
#
[
pymethods
]
impl
HttpClient
{
#
[
new
]
fn
new
(
http_url
:
String
,
app_key
:
String
,
app_secret
:
String
,
access_token
:
String
,
)
->
PyResult
<
Self
>
{
Ok
(
Self
(
LbHttpClient
::
new
(
HttpClientConfig
::
new
(
app_key
,
app_secret
,
access_token
)
.
http_url
(
http_url
)
,
)
)
)
}
#
[
classmethod
]
fn
from_env
(
_cls
:
Bound
<
PyType
>
)
->
PyResult
<
Self
>
{
Ok
(
Self
(
LbHttpClient
::
from_env
(
)
.
map_err
(
|err|
{
ErrorNewType
(
longport
::
Error
::
HttpClient
(
err
)
)
}
)
?
)
)
}
#
[
pyo3
(
signature =
(
method
,
path
,
headers=
None
,
body=
None
)
)
]
fn
request
(
&
self
,
method
:
String
,
path
:
String
,
headers
:
Option
<
HashMap
<
String
,
String
>
>
,
body
:
Option
<
Bound
<
PyAny
>
>
,
)
->
PyResult
<
PyObject
>
{
let
body = body
.
map
(
|body| pythonize
::
depythonize
::
<
Value
>
(
&
body
)
)
.
transpose
(
)
.
map_err
(
|err|
PyRuntimeError
::
new_err
(
err
.
to_string
(
)
)
)
?
;
let
req =
self
.
0
.
request
(
method
.
to_uppercase
(
)
.
parse
::
<
Method
>
(
)
.
map_err
(
|_|
{
ErrorNewType
(
longport
::
Error
::
HttpClient
(
HttpClientError
::
InvalidRequestMethod
,
)
)
}
)
?
,
path
,
)
;
let
req = headers
.
unwrap_or_default
(
)
.
into_iter
(
)
.
fold
(
req
,
|acc
,
(
name
,
value
)
| acc
.
header
(
name
,
value
)
)
;
match
body
{
Some
(
body
)
=>
{
let
resp = tokio
::
runtime
::
Runtime
::
new
(
)
.
unwrap
(
)
.
block_on
(
req
.
body
(
Json
(
body
)
)
.
response
::
<
Json
<
Value
>
>
(
)
.
send
(
)
)
.
map_err
(
|err|
ErrorNewType
(
longport
::
Error
::
HttpClient
(
err
)
)
)
?
;
Python
::
with_gil
(
|py|
{
Ok
(
pythonize
::
pythonize
(
py
,
&
resp
.
0
)
.
map_err
(
|err|
PyRuntimeError
::
new_err
(
err
.
to_string
(
)
)
)
?
.
into
(
)
)
}
)
}
None
=>
{
let
resp = tokio
::
runtime
::
Runtime
::
new
(
)
.
unwrap
(
)
.
block_on
(
req
.
response
::
<
Json
<
Value
>
>
(
)
.
send
(
)
)
.
map_err
(
|err|
ErrorNewType
(
longport
::
Error
::
HttpClient
(
err
)
)
)
?
;
Python
::
with_gil
(
|py|
{
Ok
(
pythonize
::
pythonize
(
py
,
&
resp
.
0
)
.
map_err
(
|err|
PyRuntimeError
::
new_err
(
err
.
to_string
(
)
)
)
?
.
into
(
)
)
}
)
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL