private void UserControl_Initialized(object sender, EventArgs e) { string baseAddres = String.Empty; using (SqliteWrapper sqliteWrapper = new SqliteWrapper(true)) { try { if (sqliteWrapper.ExecuteSelect <WebApiModel>().Count() == 0) { WebApiModel webApiModel = new WebApiModel() { base_addres = "http://localhost:1000/" }; sqliteWrapper.ExecuteInsert(webApiModel); sqliteWrapper.Commit(); } baseAddres = sqliteWrapper.ExecuteSelect <WebApiModel>().FirstOrDefault().base_addres; } catch (Exception ex) { sqliteWrapper.RollBack(); } } BaseAddressTextBox.Text = baseAddres; }
public void CreateCommand(WebApiModel cmd) { if (cmd == null) { throw new ArgumentNullException(nameof(cmd)); } _context.WebApiTable.Add(cmd); }
// PUT /api/lists/{lid}/notes/{nid} public void Put(string lid, string nid, WebApiModel.Note note) { try { var userId = (Request.GetUserPrincipal().Identity as UserIdentity).UserId; var noteData = new Note() { Name = note.title }; _manager.UpdateNote(userId, lid, nid, noteData); } catch (ObjectNotFoundException) { throw new HttpResponseException(HttpStatusCode.NotFound); } }
public HttpResponseMessage GetJson(HttpRequestMessage request) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); var model = new WebApiModel() { ipcontent = GetClientIp(request) }; var result = JsonHelper.SerializeObject(model); response.Content = new StringContent(result, Encoding.Unicode, "application/json"); return(response); }
// POST /api/lists/{lid}/notes public HttpResponseMessage Post(string lid, WebApiModel.Note note) { var userId = (Request.GetUserPrincipal().Identity as UserIdentity).UserId; var noteData = new Note() { Name = note.title }; _manager.CreateNote(new User { UniqueId = userId }, lid, string.IsNullOrEmpty(note.listCreatorId) ? userId : note.listCreatorId, noteData); var response = new HttpResponseMessage<WebApiModel.Note>(note) { StatusCode = HttpStatusCode.Created }; response.Headers.Location = new Uri(Request.RequestUri, string.Format("/api/lists/{0}/notes/{1}", lid, noteData.Id)); return response; }
// PUT /api/lists/{id} public void Put(string id, WebApiModel.TaskList list) { try { var userId = (Request.GetUserPrincipal().Identity as UserIdentity).UserId; var listData = new TaskList() { Name = list.name }; _manager.UpdateTaskList(userId, id, string.IsNullOrEmpty(list.creatorId)? userId : list.creatorId, listData); } catch (ObjectNotFoundException) { throw new HttpResponseException(HttpStatusCode.NotFound); } }
// POST /api/lists public HttpResponseMessage Post(WebApiModel.TaskList list) { var userId = (Request.GetUserPrincipal().Identity as UserIdentity).UserId; var listData = new TaskList() { Name = list.name }; _manager.CreateTaskList(new User { UniqueId = userId }, listData); list.id = listData.Id; var response = new HttpResponseMessage<WebApiModel.TaskList>(list) { StatusCode = HttpStatusCode.Created }; response.Headers.Location = new Uri(Request.RequestUri, "/api/lists/" + listData.Id + "/" + userId); return response; }
public WebApiModel GetWebApiSchema(Assembly assembly) { var controllers = assembly.GetTypes() .Where(type => type != typeof(ApiController) && !type.Name.Equals("WebApiSchemaController") && typeof(ApiController).IsAssignableFrom(type)).ToList(); var result = new WebApiModel { Controllers = new List <Controller>() }; foreach (var controller in controllers) { var controllerModel = new Controller { Name = controller.Name, Methods = new List <Method>() }; foreach ( var methodInfo in controller.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) .Where(x => !x.IsSpecialName) .ToList()) { var methodModel = new Method { Name = methodInfo.Name }; methodModel.Parameters = GetMethodParameters(methodInfo.GetParameters()); var returnType = methodInfo.ReturnType; var methodReturnType = GetMethodReturnType(returnType); methodModel.ReturnType = methodReturnType; if (methodReturnType != MethodReturnType.Void) { var primitiveType = GetPrimitiveTypeByMethodReturnType(methodReturnType, returnType); methodModel.PrimitiveType = primitiveType; if (primitiveType == null) { if (methodReturnType == MethodReturnType.TaskT || methodReturnType == MethodReturnType.IEnumerableT) { var taskTType = returnType.GenericTypeArguments.First(); if (taskTType.IsPrimitiveType()) { methodModel.PrimitiveType = GetPrimitiveType(taskTType.Name); } else if (taskTType.IsHttpResult()) { methodModel.PrimitiveType = PrimitiveType.String; } else { methodModel.ComplexType = GetComplexType(taskTType); } } else { methodModel.ComplexType = GetComplexType(returnType); } } } controllerModel.Methods.Add(methodModel); } result.Controllers.Add(controllerModel); } return(result); }
public void GenerateWebApiActions() { if (_options.GenerateServiceStackRequests) { throw new Exception("WebApi actions are not supported for ServiceStack APIs at the moment! Please submit a Pull Request!"); } var model = new WebApiModel(); foreach (var assemblyName in _options.Assemblies) { var assembly = Assembly.LoadFrom(assemblyName); var controllers = assembly.GetTypes().Where(_configuration.ControllerPredicate).OrderBy(t => t.Name); foreach (var controller in controllers) { var actions = controller.GetMethods() .Where(_configuration.ActionsPredicate) .Where(m => m.DeclaringType == controller) .OrderBy(m => m.Name); if (!actions.Any()) { continue; } var controllerName = controller.Name.Replace("Controller", ""); var controllerModel = new Controller(); controllerModel.ServerType = controller; controllerModel.TypeScriptName = controllerName; model.Controllers.Add(controllerModel); // TODO: WebAPI supports multiple actions with the same name but different parameters - this doesn't! foreach (var action in actions) { if (NotAnAction(action)) { continue; } var httpMethod = GetHttpMethod(action); var returnType = TypeScriptDefinitionsGenerator.Common.TypeConverter.GetTypeScriptName(action.ReturnType); var actionParameters = _configuration.GetActionParameters(action); var dataParameter = actionParameters.FirstOrDefault(a => !a.FromUri && !a.RouteProperty); var dataParameterName = dataParameter == null ? "null" : dataParameter.Name; var url = _configuration.UrlGenerator.GetUrl(action); var actionModel = new Action(); actionModel.Verbs = new[] { httpMethod }; actionModel.Url = url; actionModel.TypeScriptName = action.Name.ToCamelCase(); actionModel.ActionParameters = actionParameters; actionModel.DataParameterName = dataParameterName; actionModel.ReturnType = returnType; controllerModel.Actions.Add(actionModel); } } } SetupHelpers(); SetupTemplates("WebApi_jQuery"); var result = Handlebars.Compile("{{> main.hbs }}")(model); File.WriteAllText(Path.Combine(_options.OutputFilePath, _options.ActionsOutputFileName ?? "actions.ts"), result); if (!_options.SuppressDefaultServiceCaller) { // Write the default service caller using (var stream = typeof(MainGenerator).Assembly.GetManifestResourceStream(typeof(MainGenerator).Namespace + ".Resources.ServiceCaller.ts")) using (var reader = new StreamReader(stream)) { File.WriteAllText(Path.Combine(_options.OutputFilePath, "servicecaller.ts"), reader.ReadToEnd()); } } }