Esempio n. 1
0
        public async Task <IgResponse <AuthenticationResponse> > Authenticate(ConversationContext conversationContext, string identifier, string password)
        {
            var localRespVar = new IgResponse <AuthenticationResponse> {
                Response = default(AuthenticationResponse), StatusCode = HttpStatusCode.OK
            };

            try
            {
                _conversationContext = conversationContext;
                JObject joj = await IgRestService.Authenticate(_conversationContext, _uri + "session", identifier, password);

                AuthenticationResponse json = JsonConvert(joj);
                localRespVar.Response = json;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            return(localRespVar);
        }
Esempio n. 2
0
        public async Task <IgResponse <T> > RestfulService <T>(string uri, HttpMethod method, string version,
                                                               ConversationContext conversationContext, Object bodyInput = null)
        {
            StringContent scontent;

            var localVar = new IgResponse <T> {
                Response = default(T), StatusCode = HttpStatusCode.OK
            };

            if (bodyInput != null)
            {
                scontent = new StringContent(JsonConvert.SerializeObject(bodyInput));
                scontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            }
            else
            {
                scontent = null;
            }

            var client = new HttpClient();

            SetDefaultRequestHeaders(client, conversationContext, version);

            var response = new HttpResponseMessage();

            string content = null;

            switch (method.Method)
            {
            case "POST":
                var myPostTask = client.PostAsync(_baseUrl + uri, scontent);
                response = myPostTask.Result;
                break;

            case "GET":
                var myGetTask = client.GetAsync(_baseUrl + uri);
                response = myGetTask.Result;
                break;

            case "PUT":
                var myPutTask = client.PutAsync(_baseUrl + uri, scontent);
                response = myPutTask.Result;
                break;

            case "DELETE":
                var myDeleteTask = client.DeleteAsync(_baseUrl + uri);
                response = myDeleteTask.Result;
                break;
            }

            if (response != null)
            {
                ParseHeaders(conversationContext, response.Headers);
                localVar.StatusCode = response.StatusCode;
                if (localVar.StatusCode == HttpStatusCode.OK)
                {
                    content = await response.Content.ReadAsStringAsync();
                }
            }

            if (content != null)
            {
                var jss = new JsonSerializerSettings();
                jss.Converters.Add(new StringEnumConverter());
                jss.MissingMemberHandling = MissingMemberHandling.Ignore;
                jss.FloatFormatHandling   = FloatFormatHandling.String;
                jss.NullValueHandling     = NullValueHandling.Ignore;
                jss.Error += DeserializationError;
                client.Dispose();

                try
                {
                    var json = JsonConvert.DeserializeObject <T>(content, jss);
                    localVar.Response = json;
                }
                catch (Exception ex)
                {
                }
            }
            return(localVar);
        }