Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mtxclient
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Nheko Reborn
mtxclient
Commits
2ac9d746
Commit
2ac9d746
authored
6 years ago
by
Konstantinos Sideris
Browse files
Options
Downloads
Patches
Plain Diff
Small refactorings
parent
b98bf576
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/client.hpp
+7
-55
7 additions, 55 deletions
src/client.hpp
src/session.hpp
+22
-0
22 additions, 0 deletions
src/session.hpp
src/utils.hpp
+30
-0
30 additions, 0 deletions
src/utils.hpp
with
59 additions
and
55 deletions
src/client.hpp
+
7
−
55
View file @
2ac9d746
...
...
@@ -299,34 +299,6 @@ private:
}
}
template
<
class
T
>
inline
T
deserialize
(
const
std
::
string
&
data
)
{
return
json
::
parse
(
data
);
}
template
<
>
inline
std
::
string
deserialize
<
std
::
string
>
(
const
std
::
string
&
data
)
{
return
data
;
}
template
<
class
T
>
inline
std
::
string
serialize
(
const
T
&
obj
)
{
return
json
(
obj
).
dump
();
}
template
<
>
inline
std
::
string
serialize
<
std
::
string
>
(
const
std
::
string
&
obj
)
{
return
obj
;
}
template
<
class
Request
,
class
Response
>
void
mtx
::
client
::
Client
::
post
(
const
std
::
string
&
endpoint
,
...
...
@@ -342,15 +314,8 @@ mtx::client::Client::post(const std::string &endpoint,
return
;
setup_auth
(
session
.
get
(),
requires_auth
);
session
->
request
.
method
(
boost
::
beast
::
http
::
verb
::
post
);
session
->
request
.
target
(
"/_matrix"
+
endpoint
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
user_agent
,
"mtxclient v0.1.0"
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
content_type
,
content_type
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
accept_encoding
,
"gzip,deflate"
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
host
,
session
->
host
);
session
->
request
.
body
()
=
serialize
<
Request
>
(
req
);
session
->
request
.
prepare_payload
();
setup_headers
<
Request
,
boost
::
beast
::
http
::
verb
::
post
>
(
session
.
get
(),
req
,
endpoint
,
content_type
);
do_request
(
std
::
move
(
session
));
}
...
...
@@ -370,15 +335,8 @@ mtx::client::Client::put(const std::string &endpoint,
return
;
setup_auth
(
session
.
get
(),
requires_auth
);
session
->
request
.
method
(
boost
::
beast
::
http
::
verb
::
put
);
session
->
request
.
target
(
"/_matrix"
+
endpoint
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
user_agent
,
"mtxclient v0.1.0"
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
content_type
,
"application/json"
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
accept_encoding
,
"gzip,deflate"
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
host
,
session
->
host
);
session
->
request
.
body
()
=
serialize
<
Request
>
(
req
);
session
->
request
.
prepare_payload
();
setup_headers
<
Request
,
boost
::
beast
::
http
::
verb
::
put
>
(
session
.
get
(),
req
,
endpoint
,
"application/json"
);
do_request
(
std
::
move
(
session
));
}
...
...
@@ -410,13 +368,7 @@ mtx::client::Client::get(const std::string &endpoint,
return
;
setup_auth
(
session
.
get
(),
requires_auth
);
session
->
request
.
method
(
boost
::
beast
::
http
::
verb
::
get
);
session
->
request
.
target
(
"/_matrix"
+
endpoint
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
user_agent
,
"mtxclient v0.1.0"
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
accept_encoding
,
"gzip,deflate"
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
host
,
session
->
host
);
session
->
request
.
prepare_payload
();
setup_headers
<
std
::
string
,
boost
::
beast
::
http
::
verb
::
get
>
(
session
.
get
(),
{},
endpoint
);
do_request
(
std
::
move
(
session
));
}
...
...
@@ -454,7 +406,7 @@ mtx::client::Client::create_session(HeadersCallback<Response> callback)
// Try to parse the response in case we have an endpoint that
// doesn't return an error struct for non 200 requests.
try
{
response_data
=
deserialize
<
Response
>
(
body
);
response_data
=
utils
::
deserialize
<
Response
>
(
body
);
}
catch
(
const
nlohmann
::
json
::
exception
&
e
)
{
}
...
...
@@ -475,7 +427,7 @@ mtx::client::Client::create_session(HeadersCallback<Response> callback)
// If we reach that point we most likely have a valid output from the
// homeserver.
try
{
callback
(
deserialize
<
Response
>
(
body
),
header
,
{});
callback
(
utils
::
deserialize
<
Response
>
(
body
),
header
,
{});
}
catch
(
const
nlohmann
::
json
::
exception
&
e
)
{
client_error
.
parse_error
=
std
::
string
(
e
.
what
())
+
": "
+
body
;
callback
(
response_data
,
header
,
client_error
);
...
...
This diff is collapsed.
Click to expand it.
src/session.hpp
+
22
−
0
View file @
2ac9d746
...
...
@@ -4,6 +4,8 @@
#include
<boost/asio/ssl.hpp>
#include
<boost/beast.hpp>
#include
"utils.hpp"
namespace
mtx
{
namespace
client
{
...
...
@@ -59,5 +61,25 @@ struct Session
//! Function to be called when the request fails.
FailureCallback
on_failure
;
};
template
<
class
Request
,
boost
::
beast
::
http
::
verb
HttpVerb
>
void
setup_headers
(
mtx
::
client
::
Session
*
session
,
const
Request
&
req
,
const
std
::
string
&
endpoint
,
const
std
::
string
&
content_type
=
""
)
{
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
user_agent
,
"mtxclient v0.1.0"
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
accept_encoding
,
"gzip,deflate"
);
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
host
,
session
->
host
);
session
->
request
.
method
(
HttpVerb
);
session
->
request
.
target
(
"/_matrix"
+
endpoint
);
session
->
request
.
body
()
=
utils
::
serialize
(
req
);
session
->
request
.
prepare_payload
();
if
(
!
content_type
.
empty
())
session
->
request
.
set
(
boost
::
beast
::
http
::
field
::
content_type
,
content_type
);
}
}
}
This diff is collapsed.
Click to expand it.
src/utils.hpp
+
30
−
0
View file @
2ac9d746
...
...
@@ -4,6 +4,8 @@
#include
<iosfwd>
#include
<map>
#include
<json.hpp>
namespace
mtx
{
namespace
client
{
namespace
utils
{
...
...
@@ -19,6 +21,34 @@ query_params(const std::map<std::string, std::string> ¶ms) noexcept;
//! Decompress a response.
std
::
string
decompress
(
const
boost
::
iostreams
::
array_source
&
src
,
const
std
::
string
&
type
)
noexcept
;
template
<
class
T
>
inline
T
deserialize
(
const
std
::
string
&
data
)
{
return
nlohmann
::
json
::
parse
(
data
);
}
template
<
>
inline
std
::
string
deserialize
<
std
::
string
>
(
const
std
::
string
&
data
)
{
return
data
;
}
template
<
class
T
>
inline
std
::
string
serialize
(
const
T
&
obj
)
{
return
nlohmann
::
json
(
obj
).
dump
();
}
template
<
>
inline
std
::
string
serialize
<
std
::
string
>
(
const
std
::
string
&
obj
)
{
return
obj
;
}
}
}
}
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