国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

使用DTO在Laravel中簡化API響應(yīng)

創(chuàng)建自定義數(shù)據(jù)傳輸對象(DTO)的全面指南,以增強(qiáng)Laravel API集成的可讀性、效率和可測試性

Laravel DTO

介紹

有效處理API響應(yīng)對于集成第三方API非常重要。在之前的文章中,我討論了如何使用Http facade設(shè)置簡單的客戶端和請求類。如果你還沒有閱讀過這篇文章,我建議你去看一下。

在此基礎(chǔ)上,本文將為您詳細(xì)介紹如何創(chuàng)建自定義數(shù)據(jù)傳輸對象(DTO),以將數(shù)據(jù)映射到API響應(yīng)中。我將使用正在進(jìn)行的Google Books API集成場景作為實際示例,使事情更容易理解。

將響應(yīng)數(shù)據(jù)映射到DTO

首先,讓我們來看一下從Google Books API獲取搜索結(jié)果時的示例響應(yīng)。為此,我調(diào)用了之前創(chuàng)建的QueryBooksByTitle動作,并搜索書籍"The Ferryman":

$response = app(QueryBooksByTitle::class)("The Ferryman");
dump($response->json());

這將輸出以下JSON數(shù)據(jù),我只選擇了我想要追蹤的字段:

{
    "kind": "books#volumes",
    "totalItems": 367,
    "items": [
        {
            ...
        },
        {
            ...
        },
        {
            "kind": "books#volume",
            "id": "dO5-EAAAQBAJ",
            "volumeInfo": {
                "title": "The Ferryman",
                "subtitle": "A Novel",
                "authors": [
                    "Justin Cronin"
                ],
                "publisher": "Doubleday Canada",
                "publishedDate": "2023-05-02",
                "description": "..."
            },
            ...
        },
        ...
    ]
}

現(xiàn)在我們知道了響應(yīng)的格式,讓我們創(chuàng)建必要的DTO來映射數(shù)據(jù)。讓我們從BookListData開始,它可以是一個簡單的PHP類。

<?php
namespace App\DataTransferObjects;
use Illuminate\Contracts\Support\Arrayable;
/**
 * 存儲來自Google Books volumes API的頂層數(shù)據(jù)。
 */
readonly class BooksListData implements Arrayable
{
    public function __construct(
        public string $kind,
        public string $id,
        public int $totalItems,
    ) {
    }
    /**
     * 從數(shù)據(jù)數(shù)組創(chuàng)建類的新實例。
     */
    public static function fromArray(array $data): BooksListData
    {
        return new self(
            data_get($data, 'kind'),
            data_get($data, 'id'),
            data_get($data, 'totalItems'),
        );
    }
    /**
     * 實現(xiàn)Laravel的Arrayable接口,允許將對象序列化為數(shù)組。
     */
    public function toArray(): array
    {
        return [
            'kind' => $this->kind,
            'items' => $this->items,
            'totalItems' => $this->totalItems,
        ];
    }
}

創(chuàng)建完DTO后,我們可以更新之前文章中創(chuàng)建的QueryBooksByTitle動作。

<?php
namespace App\Actions;
use App\DataTransferObjects\BooksListData;
use App\Support\ApiRequest;
use App\Support\GoogleBooksApiClient;
use Illuminate\Http\Client\Response;
/**
 * QueryBooksByTitle類是一個用于從Google Books API查詢書籍的動作。
 * 它提供了一個__invoke方法,接受一個標(biāo)題并返回API的響應(yīng)。
 */
class QueryBooksByTitle
{
    /**
     * 根據(jù)標(biāo)題從Google Books API查詢書籍,并返回BookListData。
     * 該方法創(chuàng)建一個GoogleBooksApiClient和一個ApiRequest,
     * 使用給定的標(biāo)題作為'q'查詢參數(shù)和'books'作為'printType'查詢參數(shù),
     * 并使用客戶端發(fā)送請求,然后返回書籍列表數(shù)據(jù)。
     */
    public function __invoke(string $title): BooksListData
    {
        $client = app(GoogleBooksApiClient::class);
        $request = ApiRequest::get('volumes')
            ->setQuery('q', 'intitle:'.$title)
            ->setQuery('printType', 'books');
        $response = $client->send($request);
        return BooksListData::fromArray($response->json());
    }
}

Test the Response Data

我們可以創(chuàng)建一個測試來確保在調(diào)用該動作時返回BooksListData對象:

<?php
use App\Actions\QueryBooksByTitle;
use App\DataTransferObjects\BooksListData;
it('fetches books by title', function () {
    $title = 'The Lord of the Rings';
    $response = resolve(QueryBooksByTitle::class)($title);
    expect($response)->toBeInstanceOf(BooksListData::class);
});

你可能沒有注意到,但上面的測試存在一個問題。我們正在訪問Google Books API。這對于不經(jīng)常運(yùn)行的集成測試可能沒問題,但在我們的Laravel測試中,應(yīng)該修復(fù)這個問題。我們可以利用Http facade的功能來解決這個問題,因為我們的Client類是使用該facade構(gòu)建的。

防止測試中的HTTP請求

我喜歡做的第一步是確保我的測試沒有進(jìn)行我沒有預(yù)期的外部HTTP請求。我們可以將`Http::preventStrayRequests();`添加到Pest.php文件中。然后,在使用Http facade發(fā)出請求的任何測試中,除非我們模擬請求,否則會引發(fā)異常。

<?php
use Illuminate\Foundation\Testing\TestCase;
use Illuminate\Support\Facades\Http;
use Tests\CreatesApplication;
uses(
    TestCase::class,
    CreatesApplication::class,
)
    ->beforeEach(function () {
        Http::preventStrayRequests();
    })
    ->in('Feature');

如果再次運(yùn)行QueryBooksByTitle測試,現(xiàn)在會出現(xiàn)一個失敗的測試,顯示以下錯誤信息:

RuntimeException: Attempted request to [https://www.googleapis.com/books/v1/volumes?key=XXXXXXXXXXXXX&q=intitle%3AThe%20Lord%20of%20the%20Rings&printType=books] without a matching fake.

現(xiàn)在,讓我們使用Http facade來偽造響應(yīng)。

<?php
use App\Actions\QueryBooksByTitle;
use App\DataTransferObjects\BooksListData;
use Illuminate\Support\Facades\Http;
it('fetches books by title', function () {
    $title = fake()->sentence();
    // 從Google Books API生成一個假響應(yīng)。
    $responseData = [
        'kind' => 'books#volumes',
        'totalItems' => 1,
        'items' => [
            [
                'id' => fake()->uuid,
                'volumeInfo' => [
                    'title' => $title,
                    'subtitle' => fake()->sentence(),
                    'authors' => [fake()->name],
                    'publisher' => fake()->company(),
                    'publishedDate' => fake()->date(),
                    'description' => fake()->paragraphs(asText: true),
                    'pageCount' => fake()->numberBetween(100, 500),
                    'categories' => [fake()->word],
                    'imageLinks' => [
                        'thumbnail' => fake()->url(),
                    ],
                ],
            ],
        ],
    ];
    // 當(dāng)客戶端向Google Books API發(fā)送請求時,返回假響應(yīng)。
    Http::fake(['https://www.googleapis.com/books/v1/*' => Http::response(
        body: $responseData,
        status: 200
    )]);
    $response = resolve(QueryBooksByTitle::class)($title);
    expect($response)->toBeInstanceOf(BooksListData::class);
    expect($response->items[0]['volumeInfo']['title'])->toBe($title);
});

現(xiàn)在運(yùn)行測試,不再出現(xiàn)RuntimeException,因為我們使用Http::fake()方法偽造了請求。Http::fake()方法非常靈活,可以接受一個包含不同URL的項目數(shù)組。根據(jù)您的應(yīng)用程序,您可以只使用'*'而不是完整的URL,甚至可以更具體地包括查詢參數(shù)或其他動態(tài)URL數(shù)據(jù)。如果需要,甚至可以偽造請求序列。有關(guān)更多信息,請參閱Laravel文檔。

這個測試效果很好,但仍然有一些改進(jìn)的空間。

擴(kuò)展數(shù)據(jù)傳輸對象(DTO)

首先,讓我們再次看一下響應(yīng)數(shù)據(jù)。將頂層的響應(yīng)映射到BooksListData對象中是不錯的,但使用items[0]['volumeInfo']['title']并不方便開發(fā)人員,并且IDE無法提供任何類型的自動完成。為了解決這個問題,我們需要創(chuàng)建更多的DTOs。通常最容易從需要映射的最低級別的項開始。在這種情況下,需要映射響應(yīng)中的imageLinks數(shù)據(jù)。查看來自Google Books的響應(yīng),似乎該數(shù)據(jù)可能包含縮略圖和小縮略圖屬性。我們將創(chuàng)建一個ImageLinksData對象來映射這部分?jǐn)?shù)據(jù)。

namespace App\DataTransferObjects;
use Illuminate\Contracts\Support\Arrayable;
readonly class ImageLinksData implements Arrayable
{
    public function __construct(
        public ?string $thumbnail = null,
        public ?string $smallThumbnail = null,
    ) {
    }
    public static function fromArray(array $data): self
    {
        return new self(
            thumbnail: data_get($data, 'thumbnail'),
            smallThumbnail: data_get($data, 'smallThumbnail'),
        );
    }
    public function toArray(): array
    {
        return [
            'thumbnail' => $this->thumbnail,
            'smallThumbnail' => $this->smallThumbnail,
        ];
    }
}
#從那里,往上走一級,我們有VolumeInfoData對象。
namespace App\DataTransferObjects;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
readonly class VolumeInfoData implements Arrayable
{
    public function __construct(
        public string $title,
        public string $subtitle,
        // 使用集合而不是數(shù)組是個人偏好。
        // 這使得處理數(shù)據(jù)稍微更容易一些。
        /** @var Collection<int, string> */
        public Collection $authors,
        public string $publisher,
        public string $publishedDate,
        public string $description,
        public int $pageCount,
        /** @var Collection<int, string> */
        public Collection $categories,
        // 圖片鏈接由ImageLinksData對象映射。
        public ImageLinksData $imageLinks,
    ) {
    }
    public static function fromArray(array $data): self
    {
        return new self(
            title: data_get($data, 'title'),
            subtitle: data_get($data, 'subtitle'),
            // 從數(shù)據(jù)數(shù)組創(chuàng)建集合。
            authors: collect(data_get($data, 'authors')),
            publisher: data_get($data, 'publisher'),
            publishedDate: data_get($data, 'publishedDate'),
            description: data_get($data, 'description'),
            pageCount: data_get($data, 'pageCount'),
            // 從數(shù)據(jù)數(shù)組創(chuàng)建集合。
            categories: collect(data_get($data, 'categories')),
            // 將圖片鏈接映射到ImageLinksData對象。
            imageLinks: ImageLinksData::fromArray(data_get($data, 'imageLinks')),
        );
    }
    public function toArray(): array
    {
        return [
            'title' => $this->title,
            'subtitle' => $this->subtitle,
            // 將集合轉(zhuǎn)換為數(shù)組,因為它們實現(xiàn)了可數(shù)組化接口。
            'authors' => $this->authors->toArray(),
            'publisher' => $this->publisher,
            'publishedDate' => $this->publishedDate,
            'description' => $this->description,
            'pageCount' => $this->pageCount,
            'categories' => $this->categories->toArray(),
            // 由于我們使用了可數(shù)組化接口,我們可以直接調(diào)用imageLinks對象的toArray方法。
            'imageLinks' => $this->imageLinks->toArray(),
        ];
    }
}

請注意,我使用了Laravel的集合而不是數(shù)組。我更喜歡使用集合,因此每當(dāng)響應(yīng)中有數(shù)組時,我都會映射到集合。另外,由于VolumeInfoData包含imageLinks屬性,我們可以使用ImageLinksData對象進(jìn)行映射。

再往上走一級,我們有一個項的列表,所以我們可以創(chuàng)建ItemData對象。

namespace App\DataTransferObjects;
use Illuminate\Contracts\Support\Arrayable;
readonly class ItemData implements Arrayable
{
    public function __construct(
        public string $id,
        public VolumeInfoData $volumeInfo,
    ) {
    }

    public static function fromArray(array $data): self
    {
        return new self(
            id: data_get($data, 'id'),
            volumeInfo: VolumeInfoData::fromArray(data_get($data, 'volumeInfo')),
        );
    }

    public function toArray(): array
    {
        return [
            'id' => $this->id,
            'volumeInfo' => $this->volumeInfo->toArray(),
        ];
    }
}

最后,我們需要回到原始的BooksListData對象,而不是映射數(shù)據(jù)數(shù)組,我們想要映射一個ItemData對象的集合。

namespace App\DataTransferObjects;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Collection;
/**
 * 存儲來自Google Books volumes API的頂級數(shù)據(jù)。
 */
readonly class BooksListData implements Arrayable
{
    public function __construct(
        public string $kind,
        /** @var Collection<int, ItemData> */
        public Collection $items,
        public int $totalItems,
    ) {
    }
    /**
     * 從數(shù)據(jù)數(shù)組創(chuàng)建類的新實例。
     */
    public static function fromArray(array $data): BooksListData
    {
        return new self(
            data_get($data, 'kind'),
            // 將項映射到ItemData對象的集合。
            collect(data_get($data, 'items', []))->map(fn (array $item) => ItemData::fromArray($item)),
            data_get($data, 'totalItems'),
        );
    }
    /**
     * 實現(xiàn)Laravel的Arrayable接口,允許將對象序列化為數(shù)組。
     */
    public function toArray(): array
    {
        return [
            'kind' => $this->kind,
            'items' => $this->items->toArray(),
            'totalItems' => $this->totalItems,
        ];
    }
}

有了所有新創(chuàng)建的DTO,讓我們回到測試并進(jìn)行更新。

測試完整的數(shù)據(jù)傳輸對象(DTO)


<?php
use App\Actions\QueryBooksByTitle;
use App\DataTransferObjects\BooksListData;
use App\DataTransferObjects\ImageLinksData;
use App\DataTransferObjects\ItemData;
use App\DataTransferObjects\VolumeInfoData;
use Illuminate\Support\Facades\Http;
it('按標(biāo)題獲取圖書', function () {
    $title = fake()->sentence();
    // 從Google Books API生成一個假響應(yīng)。
    $responseData = [
        'kind' => 'books#volumes',
        'totalItems' => 1,
        'items' => [
            [
                'id' => fake()->uuid,
                'volumeInfo' => [
                    'title' => $title,
                    'subtitle' => fake()->sentence(),
                    'authors' => [fake()->name],
                    'publisher' => fake()->company(),
                    'publishedDate' => fake()->date(),
                    'description' => fake()->paragraphs(asText: true),
                    'pageCount' => fake()->numberBetween(100, 500),
                    'categories' => [fake()->word],
                    'imageLinks' => [
                        'thumbnail' => fake()->url(),
                    ],
                ],
            ],
        ],
    ];
    // 當(dāng)客戶端向Google Books API發(fā)送請求時,返回假響應(yīng)。
    Http::fake(['https://www.googleapis.com/books/v1/*' => Http::response(
        body: $responseData,
        status: 200
    )]);
    $response = resolve(QueryBooksByTitle::class)($title);
    expect($response)->toBeInstanceOf(BooksListData::class)
        ->and($response->items->first())->toBeInstanceOf(ItemData::class)
        ->and($response->items->first()->volumeInfo)->toBeInstanceOf(VolumeInfoData::class)
        ->imageLinks->toBeInstanceOf(ImageLinksData::class)
        ->title->toBe($title);
});

現(xiàn)在我們的期望中可以看到,響應(yīng)正在映射所有不同的DTO,并正確設(shè)置標(biāo)題。

通過使操作返回DTO而不是默認(rèn)的Illuminate/Http/Client/Response,我們現(xiàn)在對API響應(yīng)具有類型安全性,并在編輯器中獲得更好的自動完成,這極大地提高了開發(fā)人員的體驗。

創(chuàng)建測試響應(yīng)輔助函數(shù)

另一個我喜歡做的測試技巧是創(chuàng)建類似于響應(yīng)工廠的東西。在每個可能需要查詢圖書的單個測試中模擬響應(yīng)是耗時的,因此我更喜歡創(chuàng)建一個簡單的trait來幫助我更快地模擬響應(yīng)。

<?php
namespace Tests\Helpers;
use Illuminate\Support\Facades\Http;
trait GoogleBooksApiResponseHelpers
{
    /**
     * 為按標(biāo)題查詢圖書生成一個假響應(yīng)。
     */
    private function fakeQueryBooksByTitleResponse(array $items = [], int $status = 200, bool $raw = false): void
    {
        // 如果raw為true,則直接返回items數(shù)組。否則,從Google Books API返回一個假響應(yīng)。
        $data = $raw ? $items : [
            'kind' => 'books#volumes',
            'totalItems' => count($items),
            'items' => array_map(fn (array $item) => $this->createItem($item), $items),
        ];
        Http::fake(['https://www.googleapis.com/books/v1/*' => Http::response(
            body: $data,
            status: $status
        )]);
    }
    // 創(chuàng)建一個假的項目數(shù)組。
    private function createItem(array $data = []): array
    {
        return [
            'id' => data_get($data, 'id', '123'),
            'volumeInfo' => $this->createVolumeInfo(data_get($data, 'volumeInfo', [])),
        ];
    }
    // 創(chuàng)建一個假的卷信息數(shù)組。
    private function createVolumeInfo(array $data = []): array
    {
        return [
            'title' => data_get($data, 'title', fake()->sentence),
            'subtitle' => data_get($data, 'subtitle', '圖書副標(biāo)題'),
            'authors' => data_get($data, 'authors', ['作者1', '作者2']),
            'publisher' => data_get($data, 'publisher', '出版商'),
            'publishedDate' => data_get($data, 'publishedDate', '2021-01-01'),
            'description' => data_get($data, 'description', '圖書描述'),
            'pageCount' => data_get($data, 'pageCount', 123),
            'categories' => data_get($data, 'categories', ['類別1', '類別2']),
            'imageLinks' => data_get($data, 'imageLinks', ['thumbnail' => 'https://example.com/image.jpg']),
        ];
    }
}

在Pest測試中使用該trait,我們只需要使用uses方法。

uses(GoogleBooksApiResponseHelpers::class);

有了這個trait,我們現(xiàn)在可以輕松地添加其他測試,而無需在每個測試中編寫所有的模擬數(shù)據(jù)。

<?php
use App\Actions\QueryBooksByTitle;
use App\DataTransferObjects\BooksListData;
use App\DataTransferObjects\ImageLinksData;
use App\DataTransferObjects\ItemData;
use App\DataTransferObjects\VolumeInfoData;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;
use Tests\Helpers\GoogleBooksApiResponseHelpers;
uses(GoogleBooksApiResponseHelpers::class);
it('按標(biāo)題獲取圖書', function () {
    $title = fake()->sentence();
    // 從Google Books API生成一個假響應(yīng)。
    $this->fakeQueryBooksByTitleResponse([['volumeInfo' => ['title' => $title]]]);
    $response = resolve(QueryBooksByTitle::class)($title);
    expect($response)->toBeInstanceOf(BooksListData::class)
        ->and($response->items->first())->toBeInstanceOf(ItemData::class)
        ->and($response->items->first()->volumeInfo)->toBeInstanceOf(VolumeInfoData::class)
        ->imageLinks->toBeInstanceOf(ImageLinksData::class)
        ->title->toBe($title);
});
it('將標(biāo)題作為查詢參數(shù)傳遞', function () {
    $title = fake()->sentence();
    // 從Google Books API生成一個假響應(yīng)。
    $this->fakeQueryBooksByTitleResponse([['volumeInfo' => ['title' => $title]]]);
    resolve(QueryBooksByTitle::class)($title);
    Http::assertSent(function (Illuminate\Http\Client\Request $request) use ($title) {
        expect($request)
            ->method()->toBe('GET')
            ->data()->toHaveKey('q', 'intitle:'.$title);
    return true;
});
});
it('獲取多本圖書的列表', function () {
    // 從Google Books API生成一個假響應(yīng)。
    $this->fakeQueryBooksByTitleResponse([
    $this->createItem(),
    $this->createItem(),
    $this->createItem(),
    ]);
    $response = resolve(QueryBooksByTitle::class)('Fake Title');
    expect($response->items)->toHaveCount(3);
});
it('拋出異常', function () {
    // 從Google Books API生成一個假響應(yīng)。
    $this->fakeQueryBooksByTitleResponse([
        $this->createItem(),
    ], 400);
    resolve(QueryBooksByTitle::class)('Fake Title');
})->throws(RequestException::class);

通過這樣做,我們現(xiàn)在有了更干凈的測試,并且我們的API響應(yīng)被映射到DTO中。對于更多的優(yōu)化,您可以考慮使用Spatie提供的Laravel Data包來創(chuàng)建DTO,它可以幫助減少一些創(chuàng)建fromArray和toArray方法的模板代碼。

總結(jié)

在這篇文章中,你學(xué)習(xí)了如何通過使用數(shù)據(jù)傳輸對象(DTO)來簡化 Laravel 中開發(fā)和測試 API 集成的過程。

我們探討了使用 DTO 的好處,以及如何創(chuàng)建 DTO、將 API 響應(yīng)映射到 DTO,并開發(fā)測試響應(yīng)輔助函數(shù)。這不僅提高了代碼的可讀性,還促進(jìn)了更加類型安全、高效和可測試的開發(fā)流程。

這些技術(shù)不僅適用于 Laravel 的 API 集成,也適用于任何類型的 API 集成。然而,如果你希望了解更高級的解決方案,我推薦看一下 Saloon PHP 庫。文章來源地址http://www.zghlxwxcb.cn/article/694.html

到此這篇關(guān)于使用DTO在Laravel中簡化API響應(yīng)的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

原文地址:http://www.zghlxwxcb.cn/article/694.html

如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請聯(lián)系站長進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • HTTP簡化版 API使用

    HTTP簡化版 API使用

    1.1、獲取當(dāng)前IP(限制 1200次 /小時) 用瀏覽器訪問?http://ip.hahado.cn/simple/current-ip?username=usernamepassword=password?URL上面加上用戶名和密碼 \\\"ip\\\": 字段是當(dāng)前的外網(wǎng)IP (\\\"ip\\\":\\\"null\\\" 正在切換中,暫時沒有IP) \\\"ttl\\\": 字段是ip可以使用的剩余時間(秒) 1.2、手動切換IP(限制 180次 /小時,間隔

    2023年04月09日
    瀏覽(25)
  • 如何使用Laravel的HTTP客戶端與外部API交互

    如何使用Laravel的HTTP客戶端與外部API交互

    Laravel使API交互對新的和有經(jīng)驗的Web開發(fā)人員來說都是輕而易舉的。Larvel的HTTP客戶端是建立在PHP的Guzzle HTTP客戶端之上,讓開發(fā)者在進(jìn)行HTTP請求時有更順暢的體驗。它的主要功能包括認(rèn)證, 路由, 和有效的對象關(guān)系映射(ORM). 本文將探討如何使用Laravel的HTTP客戶端來進(jìn)行請求, 調(diào)

    2024年01月21日
    瀏覽(45)
  • larvel 中的api.php_Laravel 開發(fā) API

    Laravel10中提示了Target *classController does not exist,為什么呢? 原因是:laravel8開始寫法變了。換成了新的寫法了 解決方法一: 在路由數(shù)組加入 AppHttpControllers 即可。 再次訪問URL,搞定。 解決方法二: 打開 appProvidersRouteServiceProvider.php 修改,添加一個namespace變量

    2024年02月06日
    瀏覽(34)
  • 使用Azure Data Factory REST API和HDInsight Spark進(jìn)行簡化數(shù)據(jù)處理

    在這篇文章中,我們將探討如何利用Azure Data Factory和HDInsight Spark創(chuàng)建一個強(qiáng)大的數(shù)據(jù)處理管道。 在當(dāng)今數(shù)據(jù)驅(qū)動的世界中,組織經(jīng)常面臨著高效可靠地處理和分析大量數(shù)據(jù)的挑戰(zhàn)。Azure Data Factory是一種基于云的數(shù)據(jù)集成服務(wù),結(jié)合HDInsight Spark,一種快速可擴(kuò)展的大數(shù)據(jù)處理框

    2024年02月10日
    瀏覽(36)
  • 【API接口工具】postman-請求響應(yīng)使用詳解

    【API接口工具】postman-請求響應(yīng)使用詳解

    Postman 可以輕松創(chuàng)建和發(fā)送 API 請求。向端點發(fā)送請求、從數(shù)據(jù)源檢索數(shù)據(jù)或測試 API 的功能。您無需在終端中輸入命令或編寫任何代碼。創(chuàng)建一個新請求并選擇Send,API 響應(yīng)出現(xiàn)在 Postman 中。 定義的 API 請求 API 為一個應(yīng)用程序訪問另一個應(yīng)用程序的功能提供了一種結(jié)構(gòu)化的方

    2024年02月03日
    瀏覽(27)
  • 使用 Python 流式傳輸來自 OpenAI API 的響應(yīng):分步指南

    使用 Python 流式傳輸來自 OpenAI API 的響應(yīng):分步指南

    OpenAI API 提供了大量可用于執(zhí)行各種 NLP 任務(wù)的尖端 AI 模型。但是,在某些情況下,僅向 OpenAI 發(fā)出 API 請求可能還不夠,例如需要實時更新時。這就是服務(wù)器發(fā)送事件 (SSE) 發(fā)揮作用的地方。 SSE 是一種簡單有效的技術(shù),用于將數(shù)據(jù)從服務(wù)器實時流式傳輸?shù)娇蛻舳恕?如何在 W

    2023年04月19日
    瀏覽(38)
  • 使用 Python 集成 ChatGPT API

    使用 Python 集成 ChatGPT API

    目錄 一、安裝 ChatGPT API 二、創(chuàng)建 Python 程序 三、調(diào)用 ChatGPT API 四、使用上下文進(jìn)行對話 五、自定義模型 六、總結(jié) 隨著人工智能技術(shù)的不斷發(fā)展,自然語言處理技術(shù)也越來越成熟。ChatGPT 是一種基于深度學(xué)習(xí)的自然語言生成技術(shù),可以用于構(gòu)建智能對話系統(tǒng)。ChatGPT API 是

    2024年02月04日
    瀏覽(29)
  • SpringBoot 如何使用 TestRestTemplate 進(jìn)行 RESTful API 集成測試

    SpringBoot 如何使用 TestRestTemplate 進(jìn)行 RESTful API 集成測試

    在使用 SpringBoot 開發(fā) RESTful API 的過程中,我們需要進(jìn)行集成測試,以確保 API 的正確性和可用性。而 TestRestTemplate 是 Spring Framework 提供的一個工具類,可以用來進(jìn)行 RESTful API 的集成測試。在本文中,我們將介紹如何使用 TestRestTemplate 進(jìn)行 RESTful API 集成測試。 TestRestTemplate 是

    2024年02月13日
    瀏覽(26)
  • “利用Python使用API進(jìn)行數(shù)據(jù)集成和自動化開發(fā)的指南“

    標(biāo)題:利用Python使用API進(jìn)行數(shù)據(jù)集成和自動化開發(fā)的指南 摘要:本文將為讀者提供一個詳細(xì)而全面的指南,教您如何使用Python編程語言來利用API進(jìn)行數(shù)據(jù)集成和自動化開發(fā)。我們將介紹API的基本概念,探討Python中常用的API庫和工具,以及演示如何通過編寫Python代碼來調(diào)用和處

    2024年02月13日
    瀏覽(25)
  • Minio入門系列【5】JAVA集成Minio之存儲桶操作API使用詳解

    Minio入門系列【5】JAVA集成Minio之存儲桶操作API使用詳解

    官方文檔:https://min.io/docs/minio/kubernetes/upstream/index.html?ref=docs-redirect SDK:https://github.com/minio/minio-java Minio 提供了多種語言的SDK,比如java、go、python等。JAVA開發(fā)平臺可以選擇JS和java SDK,也就是前端和后端都可以直接集成minio。 每個OSS的用戶都會用到上傳服務(wù)。Web端常見的上傳

    2024年02月05日
    瀏覽(28)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包