ConstructorIO.ConstructorIORequest.GetURI C# (CSharp) Méthode

GetURI() private méthode

private GetURI ( string APIKey, string Host ) : Uri
APIKey string
Host string
Résultat System.Uri
        internal Uri GetURI(string APIKey, string Host)
        {
            var tempParams = new HashArgs(_extraArgs); //Create a new instance
            //Don't want to have the API key floating around in each object
            tempParams["autocomplete_key"] = APIKey;

            var uriBuilder = new UriBuilder(_uriScheme, Host)
            {
                Path = _apiPath,
                Query = Util.SerializeParams(tempParams)
            };

            return uriBuilder.Uri;
        }

Usage Example

        internal async Task<Tuple<bool, string>> MakeRequest(ConstructorIORequest APIRequest, Func<HttpWebResponse, bool> ResponseCheck)
        {
            Uri requestURI = APIRequest.GetURI(_autocompleteKey, _host);

            bool validResponse = false;
            bool errorRaised = false;
            string responseText = "";

            string jsonBody = null;
            HttpWebResponse serverResponse = null;

            try
            {
                if (APIRequest.RequestBody != null)
                {
                    JObject jobj = JObject.FromObject(APIRequest.RequestBody);
                    jsonBody = jobj.ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error generating JSON body. See inner exception for details.", ex);
            }
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestURI);

                string creds = Convert.ToBase64String(Encoding.ASCII.GetBytes(_apiKey + ":"));
                request.Headers[HttpRequestHeader.Authorization] = String.Format("Basic {0}", creds);
                request.Method = APIRequest.Method;

                if (request.Method != "GET")
                {
                    request.ContentType = "application/json";

                    using (Stream requestStream = await request.GetRequestStreamAsync())
                    {
                        using (StreamWriter writer = new StreamWriter(requestStream))
                        {
                            writer.Write(jsonBody);
                        }
                        await requestStream.FlushAsync();
                    }
                }

                serverResponse = (HttpWebResponse)await request.GetResponseAsync();
                validResponse = ResponseCheck(serverResponse);
            }
            catch (WebException webException)
            {
                errorRaised = true;

                if (webException.Response as HttpWebResponse != null)
                {
                    serverResponse = webException.Response as HttpWebResponse;
                }
                else
                {
                    throw new Exception("Error Downlaoding. See inner exception for details", webException);
                }
            }
            catch (Exception e)
            {
                throw new Exception("Error Downlaoding. See inner exception for details", e);
            }

            try
            {
                if (serverResponse != null)
                    using (Stream dataStream = serverResponse.GetResponseStream())
                    using (StreamReader reader = new StreamReader(dataStream))
                        _lastBody = responseText = await reader.ReadToEndAsync();
            }
            catch (Exception ex)
            {
                throw new Exception("Error reading server response. See inner exception for details", ex);
            }

            if (responseText != null)
            {
                if (errorRaised)
                {
                    //Best way to check for valid jsontext is try ot 
                    try
                    {
                        var jsonResponse = JObject.Parse(responseText);
                        if (jsonResponse["message"] != null)
                        {
                            throw new ConstructorIOException(jsonResponse["message"].ToString());
                        }
                    }
                    catch (JsonReaderException ex)
                    {
                        throw new Exception("Error occured during request. Response:" + _lastBody);
                    }
                }
            }

            return Tuple.Create(validResponse, responseText);
        }