コード例 #1
0
 public UnauthorizedModel(IOptions <OrganizationOptions> options)
 {
     Options = options.Value;
 }
コード例 #2
0
        /// <summary>
        /// Updates an existing contact from the sevDesk API.
        /// </summary>
        /// <param name="id">The ID of the contact.</param>
        /// <param name="options">The options that contain the information about the contact.</param>
        /// <exception cref="InvalidOperationException">If the API could not be reached or the token is not valid, an <see cref="InvalidOperationException" /> is thrown.</exception>
        /// <returns>Returns the updated contact.</returns>
        public async Task <Contact> UpdateContactAsync(string id, ContactOptions options)
        {
            try
            {
                // Initializes the key value pairs
                List <KeyValuePair <string, string> > values = new List <KeyValuePair <string, string> >();
                if (!string.IsNullOrWhiteSpace(options.CustomerNumber))
                {
                    values.Add(new KeyValuePair <string, string>("customerNumber", options.CustomerNumber));
                }
                if (!string.IsNullOrWhiteSpace(options.Description))
                {
                    values.Add(new KeyValuePair <string, string>("description", options.Description));
                }
                if (!string.IsNullOrWhiteSpace(options.CategoryId))
                {
                    values.Add(new KeyValuePair <string, string>("category[id]", options.CategoryId));
                    values.Add(new KeyValuePair <string, string>("category[objectName]", "Category"));
                }

                // Adds the specific properties of a person
                PersonOptions personOptions = options as PersonOptions;
                if (personOptions != null)
                {
                    if (!string.IsNullOrWhiteSpace(personOptions.FirstName))
                    {
                        values.Add(new KeyValuePair <string, string>("surename", personOptions.FirstName));
                    }
                    if (!string.IsNullOrWhiteSpace(personOptions.LastName))
                    {
                        values.Add(new KeyValuePair <string, string>("familyname", personOptions.LastName));
                    }
                    if (!string.IsNullOrWhiteSpace(personOptions.AcademicTitle))
                    {
                        values.Add(new KeyValuePair <string, string>("academicTitle", personOptions.AcademicTitle));
                    }
                    if (!string.IsNullOrWhiteSpace(personOptions.Position))
                    {
                        values.Add(new KeyValuePair <string, string>("titel", personOptions.Position));
                    }
                    if (personOptions.Birthday != null)
                    {
                        values.Add(new KeyValuePair <string, string>("birthday", personOptions.Birthday?.ToString("yyyy-MM-ddT00:00:00")));
                    }
                    values.Add(new KeyValuePair <string, string>("gender", personOptions.Gender == Gender.Male ? "m" : "w"));
                    if (!string.IsNullOrWhiteSpace(personOptions.OrganizationId))
                    {
                        values.Add(new KeyValuePair <string, string>("parent[id]", personOptions.OrganizationId));
                        values.Add(new KeyValuePair <string, string>("parent[objectName]", "Contact"));
                    }
                }

                // Adds the specific properties of an organization
                OrganizationOptions organizationOptions = options as OrganizationOptions;
                if (organizationOptions != null)
                {
                    if (!string.IsNullOrWhiteSpace(organizationOptions.Name))
                    {
                        values.Add(new KeyValuePair <string, string>("name", organizationOptions.Name));
                    }
                }

                // Sends an HTTP request to endpoint
                HttpResponseMessage response = await httpClient.PutAsync($"{this.apiUri}/Contact/{id}?token={this.Token}", new FormUrlEncodedContent(values));

                // Returns the updated contact
                return(this.GetContact(JsonConvert.DeserializeObject <Result <Models.Contacts.Contact> >(await response.Content.ReadAsStringAsync()).objects));
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(e.Message, e);
            }
        }