private static RetsPostObjectResponse Execute(ProgramOptions options, RetsSession session, byte[] fileContents) { RetsPostObjectResponse response = null; ConsoleHelper.WriteInfo("Executing action {0} {1} {2} for ID {3}...", options.Action, options.Resource, options.Type, options.ResourceId); try { string fileName = null; if (!string.IsNullOrWhiteSpace(options.File)) { var fileInfo = new FileInfo(options.File); fileName = fileInfo.Name; } if (options.Action.Equals("Add", StringComparison.InvariantCultureIgnoreCase)) response = session.PostObject(RetsPostObjectParams.ActionAdd(options.ContentType, options.Type, options.Resource, options.ResourceId, options.ObjectId, fileName, fileContents, description: options.Description, label: options.Label, accessibility: options.Accessibility, documentTypeId: options.DocumentTypeId, delegateId: options.DelegateId, delegateHash: options.DelegateHash, delegatePassword: options.DelegatePassword)); else if (options.Action.Equals("Replace", StringComparison.InvariantCultureIgnoreCase)) response = session.PostObject(RetsPostObjectParams.ActionReplace(options.ContentType, options.Type, options.Resource, options.ResourceId, options.ObjectId ?? -1, fileName, fileContents, description: options.Description, label: options.Label, accessibility: options.Accessibility, documentTypeId: options.DocumentTypeId, delegateId: options.DelegateId, delegateHash: options.DelegateHash, delegatePassword: options.DelegatePassword)); else if (options.Action.Equals("Delete", StringComparison.InvariantCultureIgnoreCase)) response = session.PostObject(RetsPostObjectParams.ActionDelete(options.ContentType, options.Type, options.Resource, options.ResourceId, options.ObjectId, options.DelegateId, options.DelegateHash, options.DelegatePassword)); else response = session.PostObject(new RetsPostObjectParams { ContentType = options.ContentType, Accessibility = options.Accessibility, ContentDescription = options.Description, ContentLabel = options.Label, FileContents = fileContents, ObjectId = options.ObjectId, Resource = options.Resource, ResourceId = options.ResourceId, Type = options.Type, UpdateAction = options.Action, DelegateId = options.DelegateId, DelegateHash = options.DelegateHash, DelegatePassword = options.DelegatePassword, DocumentTypeId = options.DocumentTypeId, }); } catch (Exception ex) { ConsoleHelper.WriteFail(ex.Message); Environment.Exit(ExitCode.Fail); } return response; }
private static void EnsureArgumentsValid(ProgramOptions options, IModelBindingDefinition<ProgramOptions> configuration) { try { Guard.That(options.Port).IsTrue(p => p > 0, "Port must be greater than 0."); // Replace requires an object ID if (options.Action.Equals("Replace", StringComparison.InvariantCultureIgnoreCase)) Guard.That(options.ObjectId).IsTrue(o => o != null, "Object ID must be supplied when action is Replace."); // Add and Replace require a file to upload and a description for documents if (options.Action.Equals("Add", StringComparison.InvariantCultureIgnoreCase) || options.Action.Equals("Replace", StringComparison.InvariantCultureIgnoreCase)) { Guard.That(options.File).IsTrue(f => !string.IsNullOrWhiteSpace(f), "File must be provided when action is Add or Replace."); var fileInfo = new FileInfo(options.File); if (!fileInfo.Exists) throw new ArgumentException("Invalid file specified."); if (options.Type.Equals("Document", StringComparison.InvariantCultureIgnoreCase)) Guard.That(options.Description).IsTrue(d => !string.IsNullOrWhiteSpace(d), "Description must be provided when type is Document and action is Add or Replace."); } if (!string.IsNullOrWhiteSpace(options.DelegateId)) { Guard.That(options.DelegateHash).IsTrue(dh => !string.IsNullOrWhiteSpace(dh), "Delegate user hash is required when /did parameter is used."); Guard.That(options.DelegatePassword).IsTrue(dp => !string.IsNullOrWhiteSpace(dp), "Delegate authorization password is required when /did parameter is used."); } } catch (Exception ex) { ConsoleHelper.WriteFail(ex.Message); OutputHelp(configuration); Environment.Exit(ExitCode.BadArguments); } }
private static RetsLoginResponse Login(ProgramOptions options, RetsSession session) { ConsoleHelper.WriteInfo("Logging in to {0}...", options.LoginUrl); return session.Login(); }