Coder Social home page Coder Social logo

maciejowski2006 / sapi Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 766 KB

Simple API server written in C#

Home Page: https://www.nuget.org/packages/SAPI

License: MIT License

C# 100.00%
csharp csharp-library dotnet http-api http-server http-server-library http-rest-api rest-api

sapi's Introduction

SAPI - Simple API

Nuget NuGet GitHub

SAPI is a library for creating APIs with C#. It's simple by design and allows for a lot of flexibility.

Installation

Add as dependency in NuGet

Install-Package SAPI -ProjectName <project>

In your preferred IDE: SAPI in rider's NuGet PM

By downloading and referencing the DLL (and its dependencies) in your project.

Usage

For detailed explanation You can also see docs

// Program.cs
using SAPI;
using SAPI.Endpoints;
using Project.Endpoints;

public static void Main(string[] args)
{
    // Init SAPI
    Server sapi = new();
    sapi.Start();
}
// Endpoints/Ping.cs
using System.Net;
using SAPI;

namespace Project.Endpoints
{
    public class Ping : Endpoint
    {
        public override string url { get; } = "ping";

        private override void Get(ref Packet packet)
        {
            Console.WriteLine("Ping!");
            
            Error.Page(HttpStatus.EnhanceYourCalm, ref packet);
        }
    }
}

sapi's People

Contributors

dependabot[bot] avatar maciejowski2006 avatar michal-2137 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

michal-2137

sapi's Issues

Cannot refresh browser tab while on page with large file

Describe the bug
You cannot refresh the page when you enter the endpoint with file download. The bug is only present with large files.

To Reproduce
Steps to reproduce the behavior:

  1. Create an endpoint that is holding a file
  2. Add a large file(> 500MB) to the specified directory
  3. Try refreshing a page

Expected behavior
The page should refresh.

Exceptions in endpoints should result in an HTTP response status 500 Internal Server Error

Currently, when an endpoint method throws an exception, a request doesn't finish, and times out. Such thing should be caught and result in a 500 Internal Server Error.

image

Example code that throws an exception:

using SAPI;

namespace ConsoleApp12.Endpoints;

public class GetServers : Endpoint
{
    public override string url { get; } = "get-servers";

    protected override void Get(ref Packet packet)
    {
        int.Parse("hello");
    }
}

Cannot dowload large files

Describe the bug
Cannot download large files. The server throws 500.

To Reproduce
Steps to reproduce the behavior:

  1. Create an endpoint that is holding a file
  2. Add a large file(> 500MB) to the specified directory
  3. Open DevTools and navigate to the URL

Expected behavior
You should be able to download the file(and not get the HttpStatus 500)

(ref) Packet struct is breaking responses

ref Packet is breaking the logic of responses. You cannot attach new headers, nor add new cookies - the changes you make are not reflected on the HttpListenerResponse.

Crash when file directory does not exist

Describe the bug
Server crashes when trying to host file on a directory that does not exist.

To Reproduce
Steps to reproduce the behavior:

  1. Create an endpoint that is holding a file/directory, and point it to a non-existent directory
  2. Try opening a page

Expected behavior
The server should not crash

Undefined methods result in HTTP code 200

When sending an HTTP request with a method that wasn't defined on an endpoint, SAPI responds with HTTP code 200 and empty body (except POST, which returns 400 Bad Request), instead of 405 Method Not Allowed.


using ConsoleApp12.Services;
using SAPI;
using SAPI.API.Utilities;

namespace ConsoleApp12.Endpoints;

public class GetServers : Endpoint
{
    public override string url { get; } = "get-servers";

    protected override void Get(ref Packet packet)
    {
        var servers = Database.GetServers();
        Json.Response(servers, ref packet);
    }
}

image
image
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.