{"id":22513363,"url":"https://github.com/fatjohn/unicorntoolkit","last_synced_at":"2025-08-03T16:30:55.063Z","repository":{"id":49247071,"uuid":"63009894","full_name":"FatJohn/UnicornToolkit","owner":"FatJohn","description":"This library is mainly to provide a simple way connect http API","archived":false,"fork":false,"pushed_at":"2024-10-03T13:53:30.000Z","size":10336,"stargazers_count":6,"open_issues_count":9,"forks_count":1,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-12-06T23:24:59.830Z","etag":null,"topics":["csharp","dotnet-standard","http","uwp","windows","windows-10","windows10"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FatJohn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-07-10T17:16:58.000Z","updated_at":"2024-10-03T13:53:35.000Z","dependencies_parsed_at":"2023-01-25T14:01:09.107Z","dependency_job_id":null,"html_url":"https://github.com/FatJohn/UnicornToolkit","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FatJohn%2FUnicornToolkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FatJohn%2FUnicornToolkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FatJohn%2FUnicornToolkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FatJohn%2FUnicornToolkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FatJohn","download_url":"https://codeload.github.com/FatJohn/UnicornToolkit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228552373,"owners_count":17935803,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet-standard","http","uwp","windows","windows-10","windows10"],"created_at":"2024-12-07T03:11:44.548Z","updated_at":"2024-12-07T03:11:45.461Z","avatar_url":"https://github.com/FatJohn.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# UnicornToolkit  [![NuGet Version](https://img.shields.io/nuget/v/UnicornToolkit.svg?style=flat)](https://www.nuget.org/packages/UnicornToolkit/)\n\nInstall from NuGet\n\u003e PM\u003e Install-Package UnicornToolkit\n\n##### This library is mainly to provide a simple way connect http service.\n\u003e Other classes were create during my developing or take from other open source project.\n\nOK~ Let me use an example to explain the idea.\n\nIf you are trying connect a http login service.\n- API host at : http://myUrl.personal/v1/login\n- one GET parameter : date (in format yyyy/MM/dd)\n- two POST parameter : userid, passowrd\n- result in JSON format as below\n\n```json\n{\n  \"status\": 1,\n  \"memberType\": \"Premium\",\n  \"message\" : \"Login Success!\",\n}\n```\n\n\nIn my thoughts, a service is combined in three parts\n1. Data \n2. Parameter\n3. Parser. \n\nSo I made BaseHttpService combined with theese three generic parts. Here're steps to create a client service.\n\n#### 1 : Create a Data class\n```csharp\npublic enum LoginStatus\n{\n    Fail,\n    Success,\n}\n\npublic enum MemberType\n{\n    [EnumProperty(\"premium\")]\n    Premium,\n    [EnumProperty(\"normal\")]\n    Normal,\n}\n\npublic class LoginResult\n{\n    [JsonProperty(\"status\")]\n    [JsonConverter(typeof(IntToEnumJsonConverter))]\n    public LoginStatus IsSuccess { get; set; }\n\n    [JsonProperty(\"memberType\")]\n    [JsonConverter(typeof(EnumKeyJsonConverter))]\n    public MemberType MemberType { get; set; }\n\n    [JsonProperty(\"message\")]\n    public string Message { get; set; }\n}\n```\n\n\n#### 2 : Create a Parameter class\n```csharp\npublic class DateTimeToLoginDateServiceConverter : IServiceParameterConveter\n{\n    public string Convert(object value)\n    {\n        var date = (DateTime)value;\n        return date.ToString(\"yyyy/MM/dd\");\n    }\n}\n\npublic class LoginParameter : HttpServiceParameter\n{\n    [HttpProperty(\"date\", HttpPropertyFor.GET, typeof(DateTimeToLoginDateServiceConverter)]\n    public DateTime Date { get; set; }\n\n    [HttpProperty(\"userid\", HttpPropertyFor.POST)]\n    public string UserId { get; set; }\n\n    [HttpProperty(\"password\", HttpPropertyFor.POST)]\n    public string Password { get; set; }\n}\n```\n\nThere are 4 types of attributes that you can add on your property.\n- **HttpIgnoreAttribute** : When sending http request won't treat this property as any part of query string or body. This is can be used in places which you want user to fill something is part of your dynamic request uri. And you can use it in the override **CreateRequestUri** method in your service.\n- **HttpMultiPartPropertyAttribute** : see this property as part of http Multi-Part content.\n- **HttpRawByteArrayPropertyAttribute** : this is used for your own custom byte content.\n- **HttpRawStringPropertyAttribute** : this is used for you own string content.\n\nYou can set HttpMethod, Content-Type by setting **HttpMethod** and **HttpContentType** property.\n  \u003e Default value of HttpMethod is NotSpecific. My generator will check your attribute to determine whether it should use GET or POST. If you change **HttpMethod** property to other value, generator will use your value.\n\nAlso there's a **Options** property which you can set like if enable cache, retry, even by pass auto generate request url.\n\nThere're some built-in ServiceParameterParser. Such as **DateTimeToUnixSecondsParameterConverter, EnumKeyParameterConverter, EnumToIntParameterConverter, TimeSpanParameterConverter**.\n\n#### 3 : Create Service class\n```csharp\npublic class LoginService : BaseHttpService\u003cLoginResult, LoginParameter, JsonParser\u003cLoginResult\u003e\u003e\n{\n    protected override string CreateRequestUri(LoginParameter parameter)\n    {\n        return \"http://myUrl.personal/v1/login\";\n    }\n}\n```\n\n#### 4 : Invoke Service\n```csharp\nvar parameter = new LoginParameter\n{\n    Date = DateTime.Now,\n    UserId = \"myUserId\",\n    Password = \"myPassword\",\n};\n\nvar service = new LoginService();\nvar result = await service.InvokeAsync(parameter);\n```\n\nAnd its done. Now LoginService will pack in correct GET url and POST content to send a request.\nThen you get the result you declared.\n\n***\n\n#### Custom Parser\nOf course you can create your custom Parser since there're lots of situation. Just like our built in JsonParser.\n```csharp\npublic class CustomParser : BaseParser\u003cLoginResult, HttpResponseMessage\u003e\n{\n    public override Task\u003cParseResult\u003cLoginResult\u003e\u003e Parse(HttpResponseMessage source)\n    {\n        // implement your parse logic here\n    }\n}\n```\n\nBecause we're all http api. so the second generic parameter of BaseParser should be HttpResponseMessage.\n\n*which namespace of HttpResponseMessage depends on which platform you are developing. \nThe simplest way to decide UWP(WinRT): Windows.Web.Http, Other : System.Net.Http*\n\n****\n\n#### Log\nIf you want to log what servcie sent. You can implement your own logger and set it to PlatformService.Logger at the begging of your application.\n```csharp\n// YourPlatformLogger must inherit from ILogService\nPlatformService.Logger = YourPlatformLogger;\n// Or you can use built-in Logger\nPlatformService.Logger = new NullLogService\n```\n\n\u003e **NullLogService** ouput log to output window by using **System.Diagnostics.Debug.WriteLine** method.\n***\n\n#### PreFlow\nIf you look at BaseHttpService, you'll find it has a PreProcessFlow property. It's a collection. These preflow will Pre-Process your parameter in the order you add your preflow classes to the collection. After preflow then parameter will be packed and send.\n\nPreflow class must inherit from IHttpPreFlow\n```csharp\npublic interface IHttpPreFlow\u003cTParameter\u003e\n    where TParameter : HttpServiceParameter\n{\n    Task Process(TParameter parameter);\n}\n```\n\n#### Other Useful classes\n- UnixDateTimeConverter\n- EnumHelper\n- Convert2\n- AsyncLock, AsyncAutoResetEvent, AsyncBarrier, AsyncCountdownEvent, AsyncReaderWriterLock, AsyncSemaphore","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffatjohn%2Funicorntoolkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffatjohn%2Funicorntoolkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffatjohn%2Funicorntoolkit/lists"}