flutter api_我如何在Flutter项目中组织API文件

flutter api

This explanation will be pretty long and complex, so if you’d want to directly see to the codebase, you can see it here:

该解释将非常冗长且复杂,因此,如果您想直接查看代码库,可以在这里查看:

Almost every app that is available in the market uses and sends data to external source (a.k.a. Internet). Although some app relies on local database to save and fetch its data to display to the user. This is fairly easy in mobile programming because this is a pretty basic topic to mobile programmers around the globe, usually we learned this upon learning the mobile programming framework itself.

市场上几乎所有可用的应用程序都使用数据并将数据发送到外部源(也称为Internet)。 尽管某些应用程序依赖于本地数据库来保存和获取其数据以显示给用户。 在移动编程中,这相当容易,因为对于全球的移动程序员来说,这是一个相当基本的话题,通常我们是在学习移动编程框架本身后才学到这一点的。

For small project probably it’s not that big of a deal because the API calls used in the application is small. But what if the application used and sends data from the internet pretty extensively? Of course that raises a problem, especially if the codebase is handled by multiple programmers. We’ve had to have a standardization in order to make things clear and in the correct order as more and more programmer handles and writes code to the codebase.

对于小型项目而言,这没什么大不了的,因为应用程序中使用的API调用很小。 但是,如果应用程序大量使用并从Internet发送数据该怎么办? 当然,这会带来一个问题,尤其是在代码库由多个程序员处理的情况下。 随着越来越多的程序员处理代码并将代码写入代码库,我们必须进行标准化以使事情变得清晰和正确。

So, what should we take into consideration when designing an API layer for Flutter app? I tried to list down all things that need to be considered before designing an API layer for Flutter

因此,在为Flutter应用程序设计API层时应该考虑什么? 我试图列出在为Flutter设计API层之前需要考虑的所有事项

  • API layer must give proper abstraction to the UI layer.

    API层必须为UI层提供适当的抽象。
  • API layer must convert all API responses into a plain dart object, so UI can directly consume it.

    API层必须将所有API响应转换为普通的dart对象,以便UI可以直接使用它。
  • API layer must be able to manage the session by itself, so the UI part should not be responsible to manage session.

    API层必须能够自己管理会话,因此UI部分不应负责管理会话。

After taking those things into consideration, this is the shape of the API layer that I mostly used on my professional projects.

考虑了这些因素之后,这就是我在专业项目中最常使用的API层的形状。

First, let’s start with the components that will be used by the API layer, I’ll illustrate this with a simple diagram

首先,让我们从API层将要使用的组件开始,我将用一个简单的图来说明这一点

Image for post

So what are those components means?

那么这些成分意味着什么?

  • Let’s start with the Internet.

    让我们从互联网开始。

This is well the “Internet” or the external data that we need to get from outside the device. This can be any means of communication, REST API, Websockets, File Download/Upload, anything.

这正是我们需要从设备外部获取的“ Internet”或外部数据。 这可以是任何通信方式,REST API,Websocket,文件下载/上传等等。

  • Next, we start with the AuthApiClient. What is this?

    接下来,我们从AuthApiClient开始。 这是什么?

Most of modern apps will need the user to login/register to the app’s system (backend), which means most of the API calls will need to be authenticated by the system. A simple example is like this: imagine you’re making an e-commerce app, you don’t want to see user A buying and selling stuff as someone else (user B) right?

大多数现代应用程序将需要用户登录/注册到应用程序的系统(后端),这意味着大多数API调用都需要由系统进行身份验证。 一个简单的例子是这样的:假设您正在开发一个电子商务应用程序,您不想看到用户A与其他人(用户B)一起买卖东西对吗?

So this AuthApiClient will handle all the communication regarding user Authentication and Authorization to other components that needs it. For example, if we need the user information on some page, this is the component that you’re looking for.

因此,此AuthApiClient将处理与用户身份验证和授权到需要它的其他组件的所有通信。 例如,如果我们需要某个页面上的用户信息,那么这就是您要查找的组件。

For more information regarding Backend authentication, you can read here https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2

有关后端身份验证的更多信息,您可以在这里阅读https://www.digitalocean.com/community/tutorials/an-introduction-to-oauth-2

  • Next up, we got the ApiClient.

    接下来,我们得到了ApiClient。

So what is the difference between the ApiClient and AuthApiClient? Ok, so basically they are the “same” things and can be used for almost same things. But the responsibility is the main difference between ApiClient and AuthApiClient.

那么ApiClient和AuthApiClient有什么区别? 好的,所以基本上它们是“相同”的东西,可以用于几乎相同的东西。 但是责任是ApiClient和AuthApiClient之间的主要区别。

ApiClient will be responsible for more practical use-case on user’s point of view, for instance when we have to

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值