Skip to content

Commit 7f812a7

Browse files
author
Konstantina Chremmou
committed
Updated language use. Removed redundant calls and initializers. Use Properties instead of public fields.
Signed-off-by: Konstantina Chremmou <[email protected]>
1 parent 84f38cd commit 7f812a7

File tree

2 files changed

+44
-57
lines changed

2 files changed

+44
-57
lines changed

ocaml/sdk-gen/csharp/autogen/src/HTTP.cs

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public TooManyRedirectsException(int redirect, Uri uri)
6161
this.uri = uri;
6262
}
6363

64-
public TooManyRedirectsException() : base() { }
64+
public TooManyRedirectsException() { }
6565

6666
public TooManyRedirectsException(string message) : base(message) { }
6767

@@ -91,7 +91,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
9191
[Serializable]
9292
public class BadServerResponseException : Exception
9393
{
94-
public BadServerResponseException() : base() { }
94+
public BadServerResponseException() { }
9595

9696
public BadServerResponseException(string message) : base(message) { }
9797

@@ -105,7 +105,7 @@ protected BadServerResponseException(SerializationInfo info, StreamingContext co
105105
[Serializable]
106106
public class CancelledException : Exception
107107
{
108-
public CancelledException() : base() { }
108+
public CancelledException() { }
109109

110110
public CancelledException(string message) : base(message) { }
111111

@@ -118,7 +118,7 @@ protected CancelledException(SerializationInfo info, StreamingContext context) :
118118
[Serializable]
119119
public class ProxyServerAuthenticationException : Exception
120120
{
121-
public ProxyServerAuthenticationException() : base() { }
121+
public ProxyServerAuthenticationException() { }
122122

123123
public ProxyServerAuthenticationException(string message) : base(message) { }
124124

@@ -142,6 +142,9 @@ protected ProxyServerAuthenticationException(SerializationInfo info, StreamingCo
142142
public const int DEFAULT_HTTPS_PORT = 443;
143143
private const int NONCE_LENGTH = 16;
144144

145+
private const int FILE_MOVE_MAX_RETRIES = 5;
146+
private const int FILE_MOVE_SLEEP_BETWEEN_RETRIES = 100;
147+
145148
public enum ProxyAuthenticationMethod
146149
{
147150
Basic = 0,
@@ -158,7 +161,7 @@ public enum ProxyAuthenticationMethod
158161

159162
private static void WriteLine(String txt, Stream stream)
160163
{
161-
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(String.Format("{0}\r\n", txt));
164+
byte[] bytes = Encoding.ASCII.GetBytes($"{txt}\r\n");
162165
stream.Write(bytes, 0, bytes.Length);
163166
}
164167

@@ -173,7 +176,7 @@ private static void WriteLine(Stream stream)
173176
// done here.
174177
private static string ReadLine(Stream stream)
175178
{
176-
System.Text.StringBuilder result = new StringBuilder();
179+
StringBuilder result = new StringBuilder();
177180
while (true)
178181
{
179182
int b = stream.ReadByte();
@@ -217,9 +220,8 @@ private static bool ReadHttpHeaders(ref Stream stream, IWebProxy proxy, bool nod
217220
// read chunk size
218221
string chunkSizeStr = ReadLine(stream);
219222
chunkSizeStr = chunkSizeStr.TrimEnd('\r', '\n');
220-
int chunkSize = 0;
221223
int.TryParse(chunkSizeStr, System.Globalization.NumberStyles.HexNumber,
222-
System.Globalization.CultureInfo.InvariantCulture, out chunkSize);
224+
System.Globalization.CultureInfo.InvariantCulture, out var chunkSize);
223225

224226
// read <chunkSize> number of bytes from the stream
225227
int totalNumberOfBytesRead = 0;
@@ -231,8 +233,8 @@ private static bool ReadHttpHeaders(ref Stream stream, IWebProxy proxy, bool nod
231233
totalNumberOfBytesRead += numberOfBytesRead;
232234
} while (numberOfBytesRead > 0 && totalNumberOfBytesRead < chunkSize);
233235

234-
string str = System.Text.Encoding.ASCII.GetString(bytes);
235-
string[] split = str.Split(new string[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
236+
string str = Encoding.ASCII.GetString(bytes);
237+
string[] split = str.Split(new [] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
236238
headers.AddRange(split);
237239

238240
entityBody += str;
@@ -276,7 +278,7 @@ private static bool ReadHttpHeaders(ref Stream stream, IWebProxy proxy, bool nod
276278

277279
private static int getResultCode(string line)
278280
{
279-
string[] bits = line.Split(new char[] { ' ' });
281+
string[] bits = line.Split(' ');
280282
return (bits.Length < 2 ? 0 : Int32.Parse(bits[1]));
281283
}
282284

@@ -426,7 +428,7 @@ public static Uri BuildUri(string hostname, string path, params object[] args)
426428

427429
private static string GetPartOrNull(string str, int partIndex)
428430
{
429-
string[] parts = str.Split(new char[] { ' ' }, partIndex + 2, StringSplitOptions.RemoveEmptyEntries);
431+
string[] parts = str.Split(new [] { ' ' }, partIndex + 2, StringSplitOptions.RemoveEmptyEntries);
430432
return partIndex < parts.Length - 1 ? parts[partIndex] : null;
431433
}
432434

@@ -457,8 +459,7 @@ private static NetworkStream ConnectSocket(Uri uri, bool nodelay, int timeoutMs)
457459
/// <param name="timeoutMs">Timeout, in ms. 0 for no timeout.</param>
458460
public static Stream ConnectStream(Uri uri, IWebProxy proxy, bool nodelay, int timeoutMs)
459461
{
460-
IMockWebProxy mockProxy = proxy as IMockWebProxy;
461-
if (mockProxy != null)
462+
if (proxy is IMockWebProxy mockProxy)
462463
return mockProxy.GetStream(uri);
463464

464465
Stream stream;
@@ -478,7 +479,7 @@ public static Stream ConnectStream(Uri uri, IWebProxy proxy, bool nodelay, int t
478479
{
479480
if (useProxy)
480481
{
481-
string line = string.Format("CONNECT {0}:{1} HTTP/1.0", uri.Host, uri.Port);
482+
string line = $"CONNECT {uri.Host}:{uri.Port} HTTP/1.0";
482483
WriteLine(line, stream);
483484
WriteLine(stream);
484485

@@ -490,8 +491,7 @@ public static Stream ConnectStream(Uri uri, IWebProxy proxy, bool nodelay, int t
490491

491492
if (UseSSL(uri))
492493
{
493-
SslStream sslStream = new SslStream(stream, false,
494-
new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
494+
SslStream sslStream = new SslStream(stream, false, ValidateServerCertificate, null);
495495
sslStream.AuthenticateAsClient("", null, SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12, true);
496496

497497
stream = sslStream;
@@ -523,7 +523,7 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
523523
}
524524

525525
if (proxy.Credentials == null)
526-
throw new BadServerResponseException(string.Format("Received error code {0} from the server", initialResponse[0]));
526+
throw new BadServerResponseException($"Received error code {initialResponse[0]} from the server");
527527

528528
NetworkCredential credentials = proxy.Credentials.GetCredential(uri, null);
529529

@@ -535,10 +535,9 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
535535
if (string.IsNullOrEmpty(basicField))
536536
throw new ProxyServerAuthenticationException("Basic authentication scheme is not supported/enabled by the proxy server.");
537537

538-
string authenticationFieldReply = string.Format("Proxy-Authorization: Basic {0}",
539-
Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials.UserName + ":" + credentials.Password)));
538+
var creds = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials.UserName + ":" + credentials.Password));
540539
WriteLine(header, stream);
541-
WriteLine(authenticationFieldReply, stream);
540+
WriteLine($"Proxy-Authorization: Basic {creds}", stream);
542541
WriteLine(stream);
543542
}
544543
else if (CurrentProxyAuthenticationMethod == ProxyAuthenticationMethod.Digest)
@@ -548,9 +547,7 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
548547
if (string.IsNullOrEmpty(digestField))
549548
throw new ProxyServerAuthenticationException("Digest authentication scheme is not supported/enabled by the proxy server.");
550549

551-
string authenticationFieldReply = string.Format(
552-
"Proxy-Authorization: Digest username=\"{0}\", uri=\"{1}:{2}\"",
553-
credentials.UserName, uri.Host, uri.Port);
550+
string authenticationFieldReply = $"Proxy-Authorization: Digest username=\"{credentials.UserName}\", uri=\"{uri.Host}:{uri.Port}\"";
554551

555552
int len = "Proxy-Authorization: Digest".Length;
556553
string directiveString = digestField.Substring(len, digestField.Length - len);
@@ -571,19 +568,19 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
571568
throw new ProxyServerAuthenticationException("Stale nonce in Digest authentication attempt.");
572569
break;
573570
case "realm=":
574-
authenticationFieldReply += string.Format(", realm=\"{0}\"", directives[++i]);
571+
authenticationFieldReply += $", realm=\"{directives[++i]}\"";
575572
realm = directives[i];
576573
break;
577574
case "nonce=":
578-
authenticationFieldReply += string.Format(", nonce=\"{0}\"", directives[++i]);
575+
authenticationFieldReply += $", nonce=\"{directives[++i]}\"";
579576
nonce = directives[i];
580577
break;
581578
case "opaque=":
582-
authenticationFieldReply += string.Format(", opaque=\"{0}\"", directives[++i]);
579+
authenticationFieldReply += $", opaque=\"{directives[++i]}\"";
583580
opaque = directives[i];
584581
break;
585582
case "algorithm=":
586-
authenticationFieldReply += string.Format(", algorithm={0}", directives[++i]); //unquoted; see RFC7616-3.4
583+
authenticationFieldReply += $", algorithm={directives[++i]}"; //unquoted; see RFC7616-3.4
587584
algorithm = directives[i];
588585
break;
589586
case "qop=":
@@ -593,21 +590,20 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
593590
qop = qops.FirstOrDefault(q => q.ToLowerInvariant() == "auth") ??
594591
qops.FirstOrDefault(q => q.ToLowerInvariant() == "auth-int");
595592
if (qop == null)
596-
throw new ProxyServerAuthenticationException(
597-
"Digest authentication's quality-of-protection directive is not supported.");
598-
authenticationFieldReply += string.Format(", qop={0}", qop); //unquoted; see RFC7616-3.4
593+
throw new ProxyServerAuthenticationException("Digest authentication's quality-of-protection directive is not supported.");
594+
authenticationFieldReply += $", qop={qop}"; //unquoted; see RFC7616-3.4
599595
}
600596
break;
601597
}
602598
}
603599

604600
string clientNonce = GenerateNonce();
605601
if (qop != null)
606-
authenticationFieldReply += string.Format(", cnonce=\"{0}\"", clientNonce);
602+
authenticationFieldReply += $", cnonce=\"{clientNonce}\"";
607603

608604
string nonceCount = "00000001"; // todo: track nonces and their corresponding nonce counts
609605
if (qop != null)
610-
authenticationFieldReply += string.Format(", nc={0}", nonceCount); //unquoted; see RFC7616-3.4
606+
authenticationFieldReply += $", nc={nonceCount}"; //unquoted; see RFC7616-3.4
611607

612608
Func<string, string> algFunc;
613609
var scratch1 = string.Join(":", credentials.UserName, realm, credentials.Password);
@@ -645,7 +641,7 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
645641
: new[] {HA1, nonce, nonceCount, clientNonce, qop, HA2};
646642
var response = algFunc(string.Join(":", array3));
647643

648-
authenticationFieldReply += string.Format(", response=\"{0}\"", response);
644+
authenticationFieldReply += $", response=\"{response}\"";
649645

650646
WriteLine(header, stream);
651647
WriteLine(authenticationFieldReply, stream);
@@ -654,8 +650,7 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
654650
else
655651
{
656652
string authType = GetPartOrNull(fields[0], 1);
657-
throw new ProxyServerAuthenticationException(
658-
string.Format("Proxy server's {0} authentication method is not supported.", authType ?? "chosen"));
653+
throw new ProxyServerAuthenticationException($"Proxy server's {authType ?? "chosen"} authentication method is not supported.");
659654
}
660655

661656
// handle authentication attempt response
@@ -671,12 +666,10 @@ private static void AuthenticateProxy(ref Stream stream, Uri uri, IWebProxy prox
671666
case 407:
672667
throw new ProxyServerAuthenticationException("Proxy server denied access due to wrong credentials.");
673668
default:
674-
throw new BadServerResponseException(string.Format(
675-
"Received error code {0} from the server", authenticatedResponse[0]));
669+
throw new BadServerResponseException($"Received error code {authenticatedResponse[0]} from the server");
676670
}
677671
}
678672

679-
680673
private static Stream DoHttp(Uri uri, IWebProxy proxy, bool noDelay, int timeoutMs, params string[] headers)
681674
{
682675
Stream stream = ConnectStream(uri, proxy, noDelay, timeoutMs);
@@ -838,9 +831,6 @@ public static void Get(DataCopiedDelegate dataCopiedDelegate, FuncBool cancellin
838831
}
839832
}
840833

841-
private const int FILE_MOVE_MAX_RETRIES = 5;
842-
private const int FILE_MOVE_SLEEP_BETWEEN_RETRIES = 100;
843-
844834
/// <summary>
845835
/// Move a file, retrying a few times with a short sleep between retries.
846836
/// If it still fails after these retries, then throw the error.

ocaml/sdk-gen/csharp/autogen/src/JsonRpc.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ internal abstract class JsonRequest
4949
{
5050
protected JsonRequest(int id, string method, JToken parameters)
5151
{
52-
this.Id = id;
53-
this.Method = method;
54-
this.Parameters = parameters;
52+
Id = id;
53+
Method = method;
54+
Parameters = parameters;
5555
}
5656

5757
public static JsonRequest Create(JsonRpcVersion jsonRpcVersion, int id, string method, JToken parameters)
@@ -105,18 +105,15 @@ public JsonRequestV2(int id, string method, JToken parameters)
105105
}
106106

107107
[JsonProperty("jsonrpc", Required = Required.Always)]
108-
public string JsonRPC
109-
{
110-
get { return "2.0"; }
111-
}
108+
public string JsonRPC => "2.0";
112109
}
113110

114111

115112
internal abstract class JsonResponse<T>
116113
{
117-
[JsonProperty("id", Required = Required.AllowNull)] public int Id = 0;
114+
[JsonProperty("id", Required = Required.AllowNull)] public int Id { get; set; }
118115

119-
[JsonProperty("result", Required = Required.Default)] public T Result = default(T);
116+
[JsonProperty("result", Required = Required.Default)] public T Result { get; set; }
120117

121118
public override string ToString()
122119
{
@@ -126,23 +123,23 @@ public override string ToString()
126123

127124
internal class JsonResponseV1<T> : JsonResponse<T>
128125
{
129-
[JsonProperty("error", Required = Required.AllowNull)] public JToken Error = null;
126+
[JsonProperty("error", Required = Required.AllowNull)] public JToken Error { get; set; }
130127
}
131128

132129
internal class JsonResponseV2<T> : JsonResponse<T>
133130
{
134-
[JsonProperty("error", Required = Required.DisallowNull)] public JsonResponseV2Error Error = null;
131+
[JsonProperty("error", Required = Required.DisallowNull)] public JsonResponseV2Error Error { get; set; }
135132

136-
[JsonProperty("jsonrpc", Required = Required.Always)] public string JsonRpc = null;
133+
[JsonProperty("jsonrpc", Required = Required.Always)] public string JsonRpc { get; set; }
137134
}
138135

139136
internal class JsonResponseV2Error
140137
{
141-
[JsonProperty("code", Required = Required.Always)] public int Code = 0;
138+
[JsonProperty("code", Required = Required.Always)] public int Code { get; set; }
142139

143-
[JsonProperty("message", Required = Required.Always)] public string Message = null;
140+
[JsonProperty("message", Required = Required.Always)] public string Message { get; set; }
144141

145-
[JsonProperty("data", Required = Required.Default)] public JToken Data = null;
142+
[JsonProperty("data", Required = Required.Default)] public JToken Data { get; set; }
146143

147144
public override string ToString()
148145
{

0 commit comments

Comments
 (0)