Follow

Create user example

Description

This sample shows how to create a new company user of the type/role 'Driver'.
It is assumed that a valid api key is provided into the function.
With successful operation the user is created and annotated with the unique user identificator (uid). This uid can further be used with e.g. job assignment.
Please note that in this example we try to create an active user, however the result might be creating an inactive user in case the license limit is reached.

C#.NET

        ...
        LoginUser driver = new LoginUser("Joe Smith", "jsmith@virtual.com", "joesecret");
        int uid = CreateActiveDriver(apikey, driver);
        ...

        public class LoginUser
        {
            public string Email { get; set; }
            public string Name { get; set; }
            public string Password { get; set; }
            public LoginUser(string name, string email, string password)
            {
                this.Name = name;
                this.Email = email;
                this.Password = password;
            }
        }

        public class FullUser
        {
            public int Id { get; set; }
            public string Email { get; set; }
            public string Name { get; set; }
            public string PhoneNumber { get; set; }
            public string Notes { get; set; }
            public bool ActiveLicense { get; set; }
            public string UserRole { get; set; }
            public string Password { get; set; }
        }

        protected int CreateActiveDriver(string apikey, LoginUser driver )
        {
            FullUser user = new FullUser();
            user.Id = 0;
            user.Name = driver.Name;
            user.Email = driver.Email;
            user.Password = driver.Password;
            user.PhoneNumber = "";
            user.Notes = "please set up your profile";
            user.ActiveLicense = true;
            user.UserRole = "Driver";

            var cli = new RestClient("https://api.jobdispatch.sygic.com/api/v1");
            var request = new RestRequest("users", Method.POST);
            request.AddHeader("X-api_key", apikey);
            request.RequestFormat = DataFormat.Json;
            request.AddBody(user);
            int uid = 0;
            try
            {
                var response = (RestResponse)cli.Execute(request);
                var jsonConverter = new RestSharp.Deserializers.JsonDeserializer();
                FullUser resultUser = jsonConverter.Deserialize<FullUser>(response);
                if (resultUser != null)
                {
                    uid = resultUser.Id;
                }
            }
            catch (Exception e)
            {}

            return uid;
        }


Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments