Concepts that programmers must understand: the difference between SDK and API

The difference between SDK and API

In C#, both SDK and API are tools for writing software and applications. SDK (Software Development Kit) is a set of tools and documentation to help developers create software applications. API (Application Programming Interface) is a set of rules, protocols and tools that define how applications communicate with each other.

The difference between SDK and API

The main difference between SDK and API is their function and purpose. An SDK is a complete toolkit that provides a set of tools and documentation to help developers create software applications. The SDK contains many APIs, but they are only part of the SDK. An API is a set of rules, protocols and tools that define how applications communicate with each other, it does not provide a complete toolkit.

Another major difference is the difficulty of use. SDKs are generally harder to use than APIs because they include more tools and documentation. However, they provide more flexibility and control because they allow developers to have full control over their applications.

When to use the SDK

When you need to create a complete application, the SDK is usually a better choice. It provides more flexibility and control, allowing you to fully control your application. Additionally, it usually includes more tools and documentation, making development more efficient.

When to use the API

APIs are often a better choice when you only need to access certain functionality of an existing application or service. It doesn't require you to create a complete application, it just requires you to use an API to interact with an existing application or service.

code example

Here's an example using the SDK and API:

Examples using the SDK

using System;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;

namespace SDKExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string subscriptionKey = "your-subscription-key";
            string endpoint = "your-endpoint";

            ComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey)) { Endpoint = endpoint };

            var result = client.RecognizeTextAsync("<https://example.com/image.jpg>").Result;

            foreach (var region in result.Regions)
            {
                foreach (var line in region.Lines)
                {
                    foreach (var word in line.Words)
                    {
                        Console.Write(word.Text + " ");
                    }
                    Console.WriteLine();
                }
            }
        }
    }
}

Example of using the API

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace APIExample
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string subscriptionKey = "your-subscription-key";
            string endpoint = "your-endpoint";
            string url = "<https://example.com/image.jpg>";

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);

            var response = await client.GetAsync($"{endpoint}/vision/v3.0/read/analyze?mode=Printed");
            var operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault();

            var result = await GetReadResult(operationLocation, client);

            foreach (var line in result.RecognitionResult.Lines)
            {
                foreach (var word in line.Words)
                {
                    Console.Write(word.Text + " ");
                }
                Console.WriteLine();
            }
        }

        static async Task<ReadOperationResult> GetReadResult(string operationLocation, HttpClient client)
        {
            ReadOperationResult result = null;
            do
            {
                var response = await client.GetAsync(operationLocation);
                result = await response.Content.ReadAsAsync<ReadOperationResult>();
                await Task.Delay(1000);
            } while (result.Status == OperationStatusCodes.Running || result.Status == OperationStatusCodes.NotStarted);

            return result;
        }
    }

    class ReadOperationResult
    {
        public string Status { get; set; }
        public RecognitionResult RecognitionResult { get; set; }
    }

    class RecognitionResult
    {
        public Line[] Lines { get; set; }
    }

    class Line
    {
        public Word[] Words { get; set; }
    }

    class Word
    {
        public string Text { get; set; }
    }
}

Guess you like

Origin blog.csdn.net/Documentlv/article/details/130290415