Example #1
0
        protected virtual void ProcessRecordViaRest()
        {
            var client = new JsonServiceClient(Remote);

            if (Credential != null)
            {
                client.SetCredentials(Credential.UserName, Credential.Password.ToUnsecureString());
            }
            object[] response = null;

            try {
                // try connecting where the URL is the base URL
                response = client.Send <object[]>((this as T));

                if (!response.IsNullOrEmpty())
                {
                    foreach (var ob in response)
                    {
                        WriteObject(ob);
                    }
                }
            } catch (WebServiceException wse) {
                throw new Exception("Invalid Remote Service");
            }
        }
Example #2
0
        private JsonServiceClient CreateSiteClient(SiteInfo siteInfo)
        {
            var client      = new JsonServiceClient(siteInfo.BaseUrl);
            var siteSession = SessionBag.Get <SiteSession>();

            if (siteSession != null)
            {
                if (siteSession.BearerToken != null)
                {
                    client.BearerToken = siteSession.BearerToken;
                }
                else if (siteSession.SessionId != null)
                {
                    client.RequestFilter = req =>
                                           req.Headers["X-" + Keywords.SessionId] = siteSession.SessionId;
                }
                else if (siteSession.UserName != null && siteSession.Password != null)
                {
                    client.SetCredentials(siteSession.UserName, siteSession.Password);
                }
                else if (siteSession.AuthSecret != null)
                {
                    client.RequestFilter = req =>
                                           req.Headers[HttpHeaders.XParamOverridePrefix + Keywords.AuthSecret] = siteSession.AuthSecret;
                }
            }
            return(client);
        }
Example #3
0
        public static JsonServiceClient GetClient()
        {
            var client = new JsonServiceClient(ServiceStarter.ServiceUrl);

            client.SetCredentials("*****@*****.**", "blub");
            client.AlwaysSendBasicAuthHeader = true;

            return(client);
        }
Example #4
0
        public static IServiceClient CreateDefaultRestClient(string subDomain, string username, string password)
        {
            var client = new JsonServiceClient(string.Format("https://{0}.moneybird.nl/api/v1.0", subDomain))
            {
                AlwaysSendBasicAuthHeader = true,
            };

            client.SetCredentials(username, password);

            return(client);
        }
Example #5
0
        static void Main(string[] args)
        {
            // The host ASP.NET MVC is working OK! When I visit this URL I see the metadata page.
            const string BaseUrl = "http://localhost:1337/";

            var restClient = new JsonServiceClient(BaseUrl);

            restClient.SetCredentials("juser", "password");
            restClient.AlwaysSendBasicAuthHeader = true;

            Console.WriteLine("Enter name:");
            string name = Console.ReadLine();
            while (!string.IsNullOrEmpty(name)) {
                ServiceStackService.Program.HelloResponse response = restClient.Get<ServiceStackService.Program.HelloResponse>("/hello/" + name);
                //ServiceStackService.Program.HelloResponse response = restClient.Get<ServiceStackService.Program.HelloResponse>("/hello/bhavesh");
                Console.WriteLine(response.Result);
                name = Console.ReadLine();
            }
        }
Example #6
0
        private static void Main()
        {
            var serverUrl = "http://localhost:57441/";

            var client = new JsonServiceClient(serverUrl);

            client.SetCredentials("cyberzed", "cyberzed");

            var isDatabaseSetupForDemo = client.Get(new Payload());

            Console.WriteLine("IsDatabaseSetupForDemo: {0}", isDatabaseSetupForDemo);

            var candies = client.Get(new CandyRequest());

            var bagOfCandy = new BagOfCandy {Name = "Mix bag"};

            bagOfCandy.Add(candies.Skip(1).Take(1).Single(), 10.1f);
            bagOfCandy.Add(candies.Skip(2).Take(1).Single(), 5.1f);

            client.Post(bagOfCandy);
        }
Example #7
0
        static void Main(string[] args)
        {
            // The host ASP.NET MVC is working OK! When I visit this URL I see the metadata page.
            const string BaseUrl = "http://localhost:1337/";

            var restClient = new JsonServiceClient(BaseUrl);

            restClient.SetCredentials("juser", "password");
            restClient.AlwaysSendBasicAuthHeader = true;

            Console.WriteLine("Enter name:");
            string name = Console.ReadLine();

            while (!string.IsNullOrEmpty(name))
            {
                ServiceStackService.Program.HelloResponse response = restClient.Get <ServiceStackService.Program.HelloResponse>("/hello/" + name);
                //ServiceStackService.Program.HelloResponse response = restClient.Get<ServiceStackService.Program.HelloResponse>("/hello/bhavesh");
                Console.WriteLine(response.Result);
                name = Console.ReadLine();
            }
        }
Example #8
0
        /// <summary>
        /// All the admin related tasks are not available via just an API key and user, this mimicks logging in a normal user
        /// If the user is admin, admin methods are available.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pass"></param>
        public void Login(string userName, string pass)
        {
            //client.Post <LoginAuthResponse>("/session", new LoginAuth { username = userName, password = pass });
            var csrfWebresponse = client.Get <GetCsrfTokenResponse>(new GetCsrfToken());

            client.Headers.Add("X-CSRF-Token", csrfWebresponse.csrf);
            csrf = csrfWebresponse.csrf;
            client.Headers.Add("X-Request-With", "XMLHttpRequest");
            client.SetCredentials(userName, pass);
            client.GetUrl("/session").PostToUrl(new LoginAuth {
                username = userName, password = pass
            }, "*/*",
                                                webReq =>
            {
                webReq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                webReq.Headers.Add("X-CSRF-Token", csrfWebresponse.csrf);
                webReq.Headers.Add("X-Request-With", "XMLHttpRequest");
                webReq.CookieContainer = client.CookieContainer;
            },
                                                webRes =>
            {
                client.CookieContainer.Add(webRes.Cookies);
            });
        }
Example #9
0
        protected virtual void ProcessRecordViaRest()
        {
            var client = new JsonServiceClient(ServiceUrl);

            if (Credential != null)
            {
                client.SetCredentials(Credential.UserName, Credential.Password.ToUnsecureString());
            }
            PowershellReponse response = null;

            try {
                // try connecting where the URL is the base URL
                response = client.Send <PowershellReponse>((this as T));

                if (response != null)
                {
                    if (!response.Warnings.IsNullOrEmpty())
                    {
                        foreach (var warning in response.Warnings)
                        {
                            WriteWarning(warning);
                        }
                    }

                    if (!response.Error.IsNullOrEmpty())
                    {
                        foreach (var error in response.Error.TakeAllBut(1))
                        {
                            WriteError(new ErrorRecord(new Exception("{0} - {1}".format(error.ExceptionType, error.ExceptionMessage)), error.Message, error.Category, null));
                        }
                    }

                    if (!response.Output.IsNullOrEmpty())
                    {
                        foreach (var ob in response.Output)
                        {
                            WriteObject(ob);
                        }
                    }

                    if (response.LastIsTerminatingError)
                    {
                        var error = response.Error.Last();
                        ThrowTerminatingError(new ErrorRecord(new Exception("{0} - {1}".format(error.ExceptionType, error.ExceptionMessage)), error.Message, error.Category, null));
                    }
                }
            } catch (WebServiceException wse) {
                switch (wse.StatusCode)
                {
                case 401:
                    if (Credential == null)
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception("Not Authenticated: you must supply credentials to access the remote service", wse), "", ErrorCategory.PermissionDenied, null));
                        return;
                    }
                    else
                    {
                        ThrowTerminatingError(new ErrorRecord(new Exception("Invalid Authentication: the given credentials are not valid with the remote service", wse), "", ErrorCategory.PermissionDenied, null));
                        return;
                    }

                case 403:
                    ThrowTerminatingError(new ErrorRecord(new Exception("Not Authorized: You are not authorized to access that remote service", wse), "", ErrorCategory.PermissionDenied, null));
                    return;

                case 404:
                    ThrowTerminatingError(new ErrorRecord(new Exception("Unknown Service: no remote service for {0} found at {1}".format(GetType().Name, ServiceStack.Text.StringExtensions.WithTrailingSlash(client.SyncReplyBaseUri) + GetType().Name), wse), "", ErrorCategory.ResourceUnavailable, null));
                    return;
                }

                ThrowTerminatingError(new ErrorRecord(new Exception("Unable to call remote cmdlet -- error: {0}".format(wse.Message), wse), "", ErrorCategory.NotSpecified, null));
            }
        }