Ejemplo n.º 1
0
        /// <summary>Executes a pipeline with the current app</summary>
        /// <param name="pipeline">The pipeline to execute.</param>
        /// <returns>A task containing the result of the pipeline that can be
        /// resolved on completion of the execution. </returns>
        public async Task <StitchResult <List <BsonDocument> > > ExecutePipeline(
            params PipelineStage[] pipeline)
        {
            if (!IsAuthenticated())
            {
                throw new Exception("Must first authenticate");
            }

            var    stitchResult = new StitchResult <List <BsonDocument> >();
            string pipeStr;

            try
            {
                pipeStr = pipeline.ToJson();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                stitchResult.Error = e;
                return(stitchResult);
            }

            return(await this.ExecuteRequest(
                       HttpMethod.Post,
                       GetResourcePath(Paths.Pipeline),
                       pipeStr,
                       rawUnmarshaller : (string res) =>
            {
                return BsonDocument.Parse(res)["result"]
                .AsBsonArray
                .ToList()
                .ConvertAll(obj => obj.AsBsonDocument);
            }
                       ));
        }
Ejemplo n.º 2
0
        private async Task <StitchResult <T> > ExecuteRequest <T>(
            HttpMethod httpMethod,
            string url,
            string payload = null,
            Func <JObject, T> unmarshaller   = null,
            Func <string, T> rawUnmarshaller = null,
            bool refreshOnFailure            = true,
            bool useRefreshToken             = false
            )
        {
            var stitchResult = new StitchResult <T>();

            try
            {
                var request = new HttpRequestMessage(
                    httpMethod,
                    url);

                if (IsAuthenticated())
                {
                    Console.WriteLine(Auth.AccessToken);
                    request.Headers.Add(
                        "Authorization",
                        "Bearer " + (useRefreshToken ?
                                     GetRefreshToken() : Auth.AccessToken));
                }

                if (payload != null)
                {
                    Console.WriteLine(payload);
                    request.Content = new StringContent(payload,
                                                        System.Text.Encoding.UTF8,
                                                        "application/json");
                }

                var response = await _httpClient.SendAsync(request);

                var content = await response.Content.ReadAsStringAsync();

                var json = JObject.Parse(content);

                Console.WriteLine("Request: {0}", payload);
                Console.WriteLine("Response: {0}", content);

                var error = json["error"];
                if (error != null)
                {
                    if (json["errorCode"] != null &&
                        json["errorCode"].ToString() == "InvalidSession")
                    {
                        Console.Error.Write(error);
                        if (!refreshOnFailure)
                        {
                            ClearAuth();
                            stitchResult.Error = new Exception(error.ToString());
                            return(stitchResult);
                        }

                        return(await HandleInvalidSession(
                                   httpMethod,
                                   url,
                                   payload,
                                   unmarshaller,
                                   rawUnmarshaller
                                   ));
                    }
                    else
                    {
                        stitchResult.Error = new Exception(error.ToString());
                    }
                }
                else
                {
                    if (rawUnmarshaller != null)
                    {
                        stitchResult.Value = rawUnmarshaller(content);
                    }
                    else
                    {
                        stitchResult.Value = unmarshaller(json);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e);
                stitchResult.Error = e;
            }

            return(stitchResult);
        }