From af09306544fa2de406996c5d3ac56075130d1737 Mon Sep 17 00:00:00 2001 From: Peter Hultqvist Date: Tue, 11 Aug 2020 19:49:22 +0200 Subject: [PATCH] Validate AccountSid on Init - Throws exception if a API call tries to use AccountSid when it's not set. --- src/Twilio/Clients/TwilioRestClient.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Twilio/Clients/TwilioRestClient.cs b/src/Twilio/Clients/TwilioRestClient.cs index b337a30f2..01d725a9b 100644 --- a/src/Twilio/Clients/TwilioRestClient.cs +++ b/src/Twilio/Clients/TwilioRestClient.cs @@ -28,7 +28,7 @@ public class TwilioRestClient : ITwilioRestClient /// /// Account SID to use for requests /// - public string AccountSid { get; } + public string AccountSid => _accountSid ?? throw new ArgumentException("AccountSID not set in " + nameof(TwilioClient) + "." + nameof(TwilioClient.Init)); /// /// Twilio region to make requests to @@ -42,6 +42,7 @@ public class TwilioRestClient : ITwilioRestClient private readonly string _username; private readonly string _password; + private readonly string _accountSid; /// /// Constructor for a TwilioRestClient @@ -65,7 +66,16 @@ public TwilioRestClient( _username = username; _password = password; - AccountSid = accountSid ?? username; + _accountSid = accountSid; + //Validate prefix in accountSid, https://www.twilio.com/docs/glossary/what-is-a-sid#common-sid-prefixes + if (_accountSid?.StartsWith("AC", StringComparison.Ordinal) == false) + throw new ArgumentException("AccountSid must start with \"AC\"", nameof(accountSid)); + if (_accountSid == null) + { + if (username.StartsWith("AC", StringComparison.Ordinal)) + _accountSid = username; + } + HttpClient = httpClient ?? DefaultClient(); Region = region;