Skip to content

Commit 0174bce

Browse files
[dotnet] Refactor away private constructor from Response (#14877)
1 parent 28e9a3a commit 0174bce

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

dotnet/src/webdriver/Response.cs

+23-23
Original file line numberDiff line numberDiff line change
@@ -56,57 +56,69 @@ public Response(SessionId sessionId)
5656
}
5757
}
5858

59-
private Response(Dictionary<string, object> rawResponse)
59+
/// <summary>
60+
/// Returns a new <see cref="Response"/> from a JSON-encoded string.
61+
/// </summary>
62+
/// <param name="value">The JSON string to deserialize into a <see cref="Response"/>.</param>
63+
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
64+
public static Response FromJson(string value)
6065
{
66+
Dictionary<string, object> rawResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions)
67+
?? throw new WebDriverException("JSON success response returned \"null\" value");
68+
69+
var response = new Response();
70+
6171
if (rawResponse.ContainsKey("sessionId"))
6272
{
6373
if (rawResponse["sessionId"] != null)
6474
{
65-
this.SessionId = rawResponse["sessionId"].ToString();
75+
response.SessionId = rawResponse["sessionId"].ToString();
6676
}
6777
}
6878

69-
if (rawResponse.TryGetValue("value", out object value))
79+
if (rawResponse.TryGetValue("value", out object valueObj))
7080
{
71-
this.Value = value;
81+
response.Value = valueObj;
7282
}
7383

7484
// If the returned object does *not* have a "value" property
7585
// the response value should be the entirety of the response.
7686
// TODO: Remove this if statement altogether; there should
7787
// never be a spec-compliant response that does not contain a
7888
// value property.
79-
if (!rawResponse.ContainsKey("value") && this.Value == null)
89+
if (!rawResponse.ContainsKey("value") && response.Value == null)
8090
{
8191
// Special-case for the new session command, where the "capabilities"
8292
// property of the response is the actual value we're interested in.
8393
if (rawResponse.ContainsKey("capabilities"))
8494
{
85-
this.Value = rawResponse["capabilities"];
95+
response.Value = rawResponse["capabilities"];
8696
}
8797
else
8898
{
89-
this.Value = rawResponse;
99+
response.Value = rawResponse;
90100
}
91101
}
92102

93-
if (this.Value is Dictionary<string, object> valueDictionary)
103+
if (response.Value is Dictionary<string, object> valueDictionary)
94104
{
95105
// Special case code for the new session command. If the response contains
96106
// sessionId and capabilities properties, fix up the session ID and value members.
97107
if (valueDictionary.ContainsKey("sessionId"))
98108
{
99-
this.SessionId = valueDictionary["sessionId"].ToString();
109+
response.SessionId = valueDictionary["sessionId"].ToString();
100110
if (valueDictionary.TryGetValue("capabilities", out object capabilities))
101111
{
102-
this.Value = capabilities;
112+
response.Value = capabilities;
103113
}
104114
else
105115
{
106-
this.Value = valueDictionary["value"];
116+
response.Value = valueDictionary["value"];
107117
}
108118
}
109119
}
120+
121+
return response;
110122
}
111123

112124
/// <summary>
@@ -124,18 +136,6 @@ private Response(Dictionary<string, object> rawResponse)
124136
/// </summary>
125137
public WebDriverResult Status { get; set; }
126138

127-
/// <summary>
128-
/// Returns a new <see cref="Response"/> from a JSON-encoded string.
129-
/// </summary>
130-
/// <param name="value">The JSON string to deserialize into a <see cref="Response"/>.</param>
131-
/// <returns>A <see cref="Response"/> object described by the JSON string.</returns>
132-
public static Response FromJson(string value)
133-
{
134-
Dictionary<string, object> deserializedResponse = JsonSerializer.Deserialize<Dictionary<string, object>>(value, s_jsonSerializerOptions)
135-
?? throw new WebDriverException("JSON success response returned \"null\" value");
136-
137-
return new Response(deserializedResponse);
138-
}
139139

140140
/// <summary>
141141
/// Returns a new <see cref="Response"/> from a JSON-encoded string.

0 commit comments

Comments
 (0)