/// <summary> /// <see cref="TryValidate" /> ensures that /// <see cref="newRelicInsightsMetadata" /> /// is instantiated correctly. If any <see cref="NewRelicInsightsMetadata" /> /// properties are not instantiated correctly, the method returns <c>false</c>, /// and outputs a <see cref="NewRelicInsightsMetadataException" />. /// </summary> /// <param name="newRelicInsightsMetadata"> /// The /// <see cref="NewRelicInsightsMetadata" /> instance to validate. /// </param> /// <param name="newRelicInsightsMetadataException"> /// A /// <see cref="NewRelicInsightsMetadataException" />, returned if any /// <see cref="NewRelicInsightsMetadata" /> properties are not instantiated /// correctly. /// </param> /// <returns> /// <c>True</c> if all <see cref="newRelicInsightsMetadata" /> properties are /// instantiated correctly. /// </returns> public static bool TryValidate(NewRelicInsightsMetadata newRelicInsightsMetadata, out NewRelicInsightsMetadataException newRelicInsightsMetadataException) { newRelicInsightsMetadataException = null; if (newRelicInsightsMetadata == null) { newRelicInsightsMetadataException = new NewRelicInsightsMetadataException( "New Relic Insights metadata not specified."); return(false); } if (string.IsNullOrEmpty(newRelicInsightsMetadata.AccountID)) { newRelicInsightsMetadataException = new NewRelicInsightsMetadataException("Invalid Account ID."); return(false); } if (string.IsNullOrEmpty(newRelicInsightsMetadata.APIKey)) { newRelicInsightsMetadataException = new NewRelicInsightsMetadataException("Invalid API key."); return(false); } if (newRelicInsightsMetadata.URI == null) { newRelicInsightsMetadataException = new NewRelicInsightsMetadataException("URI not specified."); return(false); } if (newRelicInsightsMetadata.UseWebProxy && newRelicInsightsMetadata.WebProxy == null) { newRelicInsightsMetadataException = new NewRelicInsightsMetadataException( "UseWebProxy is true, but no proxy is specified."); return(false); } if (newRelicInsightsMetadata.UseNonDefaultTimeout && newRelicInsightsMetadata.NonDefaultTimeout == TimeSpan.Zero) { newRelicInsightsMetadataException = new NewRelicInsightsMetadataException( "UseNonDefaultTimeout is true, but no timeout is specified."); return(false); } return(true); }
/// <summary> /// <see cref="TryInjectAPIKey" /> attempts to inject a custom HTTP header, /// composed of <see cref="headerName" /> and <see cref="headerValue" />, into /// <see cref="httpRequestHeaders" />. If successful, the New Relic Insights /// custom HTTP header will be added to <see cref="httpRequestHeaders" />. /// </summary> /// <param name="headerName">The name of the custom HTTP header.</param> /// <param name="headerValue">The value of the custom HTTP header.</param> /// <param name="httpRequestHeaders"> /// The <see cref="HttpRequestHeaders" /> to which /// the custom HTTP header should be added. /// </param> /// <param name="newRelicInsightsMetadataException"> /// A /// <see cref="NewRelicInsightsMetadataException" /> returned if the New Relic /// custom HTTP header cannot be added. /// </param> /// <returns> /// <c>True</c>, if the custom HTTP header is successfully added. Otherwise, /// false. /// </returns> public static bool TryInjectAPIKey(string headerName, string headerValue, HttpRequestHeaders httpRequestHeaders, out NewRelicInsightsMetadataException newRelicInsightsMetadataException) { newRelicInsightsMetadataException = null; var apiKeyIsAdded = httpRequestHeaders.TryAddWithoutValidation(headerName, headerValue); if (apiKeyIsAdded) { return(true); } newRelicInsightsMetadataException = new NewRelicInsightsMetadataException( "The New Relic Insights header-name, or API key does not conform to a valid HTTP header format."); return(false); }