Peyman.JsonSimpleAPI 1.0.0

dotnet add package Peyman.JsonSimpleAPI --version 1.0.0                
NuGet\Install-Package Peyman.JsonSimpleAPI -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Peyman.JsonSimpleAPI" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Peyman.JsonSimpleAPI --version 1.0.0                
#r "nuget: Peyman.JsonSimpleAPI, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Peyman.JsonSimpleAPI as a Cake Addin
#addin nuget:?package=Peyman.JsonSimpleAPI&version=1.0.0

// Install Peyman.JsonSimpleAPI as a Cake Tool
#tool nuget:?package=Peyman.JsonSimpleAPI&version=1.0.0                

JsonSimpleAPI

This repo makes simple any API JSON request and response. simply make a new Request(); then specify class will back via request and send any type of data. this library serializes any classes or data types to JSON then deserialize any classes or data types for you back. You don't have to worry about the implementation.

Example

    var api_request = new Request(URL, "Authorization", $"Basic blahblahapikey==");
    var result = api.Send<MyClassResult>(MyClassDataToSend);
    if (result != null)
    {
            print(result.Message);
    }

MyClassResult could be any data type like int or string or any custom class
MyClassDataToSend could be any data type like int or string or any custom class

Example 2

  int age = 33;
  var api_request = new Request("www.age-to-year-api.com", "Authorization", $"Basic blahblahapikey==");
  var yearIWasBorn = api.Send<DateTime>(age); // "1988"

How it Works

  public class Request
  {
      public string URL { get; set; }
      public string Method { get; set; } = "POST";
      public string AuthorizationKey { get; set; } = "Authorization";
      public string AuthorizationValue { get; set; } = "Basic blahblahblah==";
      public string ContentType { get; set; } = "application/json";

      public Request(string uRL,  string authorizationKey, string authorizationValue)
      {
          URL = uRL;
          AuthorizationKey = authorizationKey;
          AuthorizationValue = authorizationValue;
      }

      public T Send<T>(object data_to_send)
      {
          var httpWebRequest = (HttpWebRequest)WebRequest.Create(this.URL);
          httpWebRequest.ContentType = this.ContentType;
          httpWebRequest.Method = this.Method;
          httpWebRequest.Headers.Add(this.AuthorizationKey, this.AuthorizationValue);

          using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
          {
              string json = JsonConvert.SerializeObject(data_to_send);
              streamWriter.Write(json);
          }

          try
          {
              using (var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse())
              {
                  using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                  {
                      var result = streamReader.ReadToEnd();

                      var json_result = JsonConvert.DeserializeObject<List<T>>(result);
                      if (json_result.Count() > 0)
                      {

                          return json_result.FirstOrDefault();

                      }
                  }
              }

              return default;

          }
          catch (WebException ex)
          {
              if (ex.Status == WebExceptionStatus.ProtocolError && ex.Response != null)
              {
                  var resp = (HttpWebResponse)ex.Response;
                  if (resp.StatusCode == HttpStatusCode.NotFound)
                  {
                      return default;
                  }
              }
          }

          return default;


      }

  }
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 224 5/2/2022