"Effortless API Integration: SDK Generation as Best Practise", Nikita Galkin

fwdays 222 views 33 slides Oct 19, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

Many developers create a separate layer for working with APIs, but Nikita believes that this approach is inefficient — it is better to generate code from your API contract. The talk will cover the following topics: how does SDK differ from API, and why is SDK preferable for development? How can yo...


Slide Content

FRACTIONAL

AGENDA

Agenda 0
1.API Integration Pain
2.SDK vs API
3.Code generation for GraphQL API
4.Code generation for REST API
5.No API specifications ??????
6.One more reason to know React

API
INTEGRATION
PAIN

The pain 1 Interview question

Can you walk me through the best practices for
fetching and handling API data in a React
application? Specifically, what steps would you
take to ensure the solution is efficient,
maintainable, and user-friendly?

The pain 1 Typical Steps
1.?????? Create API Client
2.?????? Define Types for Input and Output Payload
3.?????? Validate Input and Output Data (e.g., using Zod or Joi)
4.?????? Transform Data if Required
5.⚙ Create the Hook with State Management
and Error Handling
6.⏹ Use AbortController for Cleanup
7.?????? Add Retry Logic for Network Errors
8.⏳ Add Loading Skeletons for UX Improvement
9.?????? Manage Cache for Performance Optimization

The pain 1 Typical solution
1.Fetch API
2.Axios (Axios-Retry)
3.RTK Query (Redux Toolkit Query)
4.React Query (TanStack Query)
5.Apollo Client

SDK vs API

Write code for
integration
Use SDK

SDK vs API 2 Use SDK

import Stripe from 'stripe';

const stripeClient = new Stripe('sk_test_12345', {
apiVersion: '2024-09-30.acacia',
maxNetworkRetries: 5,
});

const customers = await stripeClient.customers.list({
limit: 10
});

SDK vs API 2 API contract
API Contract (e.g., OpenAPI, RAML, Postman
Collections, GraphQL Schemas) describes:
●Endpoints
●Request/response models
●Authentication methods

SDK vs API 2 API contract
Company API Specification
SDK Languages
AWS
Smithy, OpenAPI JavaScript, Python, Java, Go,
Ruby, .NET, etc.
Azure
OpenAPI, OData Python, JavaScript, .NET, Go,
Java, etc.
Google Cloud
Discovery Document,
Protobuf, OpenAPI
Python, JavaScript, Java, Go,
Ruby, etc.
Stripe
OpenAPI JavaScript, Python, PHP,
Ruby, Java, .NET, Go

SDK vs API 2 SDK generation
1.Parse API Contract: Extract endpoints, models, and
data types.
2.Generate Code: Create SDK with models and API
wrappers.
3.Add Utilities: Authentication, retries, error handling,
logging, etc.
4.Documentation: Include inline API docs (e.g.,
JSDoc).

SDK vs API 2 Main ideas

●The API should have a contract defined as a
specification.
●Client integration code should be generated
from that specification.
●Code generation provides clear boundary
between integration code and application
code.

Code
generation for
GraphQL API

GraphQL 3 Codegen

Code
generation for
REST API

REST 4 Codegen
1.Redux toolkit Code Generation
https://redux-toolkit.js.org/rtk-query/usage/code-generation#openapi
2.React Query
https://tanstack.com/query/latest/docs/framework/react/community/community-projects#o
penapi-codegen
a.OpenAPI React Query codegen
https://openapi-react-query-codegen.vercel.app/

No API
specifications
??????

No specification 5 API Design First
1.Design-first means defining the API contract before
implementation.
2.Focuses on collaboration between teams (frontend,
backend, and stakeholders).
3.API contract serves as the single source of truth.

No specification 5 API Design First
•Faster Development: Parallel work for frontend and backend
teams.
•Clear Expectations: Ensures all stakeholders agree on API
behavior early.
•Improved Quality: Reduces misunderstandings and
mismatched expectations.
•Easier Testing: Enables API mocking and automated testing.
•Better Documentation: The API contract generates
documentation automatically.

No specification 5 API Design First
•Faster Development: Parallel work for frontend and backend
teams.
•Clear Expectations: Ensures all stakeholders agree on API
behavior early.
•Improved Quality: Reduces misunderstandings and
mismatched expectations.
•Easier Testing: Enables API mocking and automated testing.
•Better Documentation: The API contract generates
documentation automatically.

No specification 5 TypeSpec

No specification 4 TypeSpec

One more
reason to know
React

Alloy 6 TypeSpec

SDK vs API 6 Main ideas

●The API should have a contract defined as a
specification.
●Client integration code should be generated
from that specification.
●Code generation provides clear boundary
between integration code and application
code.

Time for your
questions!