FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
client-sdk-cpp/src/token_source_json.cpp at main · livekit/client-sdk-cpp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
livekit
/
client-sdk-cpp
Public
Notifications
You must be signed in to change notification settings
Fork
38
Star
67
Code
Issues
2
Pull requests
5
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
client-sdk-cpp
/
src
/
token_source_json.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
115 lines (97 loc) · 4.03 KB
Breadcrumbs
client-sdk-cpp
/
src
/
token_source_json.cpp
Copy path
File metadata and controls
115 lines (97 loc) · 4.03 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright 2026 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the License governing permissions and limitations.
*/
#
include
<
nlohmann/json.hpp
>
#
include
<
optional
>
#
include
<
string
>
#
include
"
token_source_internal.h
"
namespace
livekit
{
namespace
{
using
nlohmann::json;
//
Read a string field, accepting either the snake_case or camelCase spelling.
//
Returns the value only when it is a non-empty JSON string.
std::optional<std::string>
readStringField
(
const
json& obj,
const
char
* snake_key,
const
char
* camel_key) {
for
(
const
char
* key : {snake_key, camel_key}) {
const
auto
it = obj.
find
(key);
if
(it != obj.
end
() && it->
is_string
()) {
std::string value = it->
get
<std::string>();
if
(!value.
empty
()) {
return
value;
}
}
}
return
std::
nullopt
;
}
}
//
namespace
std::string
buildTokenSourceRequestJson
(
const
TokenRequestOptions& options) {
json body =
json::object
();
const
auto
set_optional = [&body](
const
char
* key,
const
std::optional<std::string>& value) {
if
(value.
has_value
() && !value->
empty
()) {
body[key] = *value;
}
};
set_optional
(
"
room_name
"
, options.
room_name
);
set_optional
(
"
participant_name
"
, options.
participant_name
);
set_optional
(
"
participant_identity
"
, options.
participant_identity
);
set_optional
(
"
participant_metadata
"
, options.
participant_metadata
);
if
(!options.
participant_attributes
.
empty
()) {
json attributes =
json::object
();
for
(
const
auto
& [key, value] : options.
participant_attributes
) {
if
(!key.
empty
()) {
attributes[key] = value;
}
}
body[
"
participant_attributes
"
] =
std::move
(attributes);
}
if
(options.
agent_name
.
has_value
() || options.
agent_metadata
.
has_value
() || options.
agent_deployment
.
has_value
()) {
json agent =
json::object
();
if
(options.
agent_name
.
has_value
() && !options.
agent_name
->
empty
()) {
agent[
"
agent_name
"
] = *options.
agent_name
;
}
if
(options.
agent_metadata
.
has_value
() && !options.
agent_metadata
->
empty
()) {
agent[
"
metadata
"
] = *options.
agent_metadata
;
}
if
(options.
agent_deployment
.
has_value
() && !options.
agent_deployment
->
empty
()) {
agent[
"
deployment
"
] = *options.
agent_deployment
;
}
body[
"
room_config
"
] = json{{
"
agents
"
,
json::array
({
std::move
(agent)})}};
}
return
body.
dump
();
}
Result<TokenSourceResponse, TokenSourceError>
parseTokenSourceResponseJson
(
const
std::string& json_text) {
//
Parse without exceptions: malformed input yields a discarded value, which we
//
treat the same as a response missing the required fields.
const
json parsed =
json::parse
(json_text,
nullptr
,
/*
allow_exceptions=
*/
false
);
TokenSourceResponse details;
if
(parsed.
is_object
()) {
if
(
const
auto
server_url =
readStringField
(parsed,
"
server_url
"
,
"
serverUrl
"
)) {
details.
server_url
= *server_url;
}
if
(
const
auto
participant_token =
readStringField
(parsed,
"
participant_token
"
,
"
participantToken
"
)) {
details.
participant_token
= *participant_token;
}
details.
participant_name
=
readStringField
(parsed,
"
participant_name
"
,
"
participantName
"
);
details.
room_name
=
readStringField
(parsed,
"
room_name
"
,
"
roomName
"
);
}
if
(details.
server_url
.
empty
()) {
return
Result<TokenSourceResponse, TokenSourceError>::
failure
(
TokenSourceError{
"
token server response missing server_url
"
});
}
if
(details.
participant_token
.
empty
()) {
return
Result<TokenSourceResponse, TokenSourceError>::
failure
(
TokenSourceError{
"
token server response missing participant_token
"
});
}
return
Result<TokenSourceResponse, TokenSourceError>::
success
(
std::move
(details));
}
}
//
namespace livekit
Back
|
FazBrowse Home
|
New Git URL