实习记录 02
命名规范
对于命名来说: 我对一个数据进行清空的操作(置为0)。
本质是根据id 对一个字段进行修改为0的操作,但若起名叫 updateById 还是不能够一眼理解它意思的。
所以,应该见名知意的去命名:清空数据:clearData
跳出两次循环 并继续
foreach($arr as $value) {
foreach($value as $item) {
continue 2; // 跳出当前的所有循环
}
}
对接平台接口 (以Amazon为例)
首先是 去到API文档中 阅读接口的要求,参数规范。
接口阅读方法:
找到所需接口的部分 然后 会有个:POST /messaging/v1/orders/{amazonOrderId}/messages/confirmDeliveryDetails
可以知道 需要使用 POST方式提交。 可以知道地址目录。
由
Description
Sends a message to a buyer to arrange a delivery or to confirm contact information for making a delivery.
可知,此接口作用为,发送给买家信息的。
由:
Parameters
Type |Name |Description |Schema
Path | amazonOrderId required An Amazon order identifier. This specifies the order for which a message is sent. string // 路径需要用到 amazonOrderId 为string 类型:string
Query |marketplaceIds required A marketplace identifier. This specifies the marketplace in which the order was placed. Only one marketplace can be specified.Max count : 1 < string
array // 参数需要用到市场 marketplaceIds 是一个数组形式的参数 里面包含市场id :< string array
Body |body
required The request schema for the createConfirmDeliveryDetails operation. CreateConfirmDeliveryDetailsRequest
body 给出的参数定义是: 一个text 然后就是传递一个文本:
Name Description Schema
text
optional The text to be sent to the buyer. Only links related to order delivery are allowed. Do not include HTML or email addresses. The text must be written in the buyer's language of preference, which can be retrieved from the GetAttributes operation.
minLength : 1
maxLength : 2000 string
继续阅读返回结果部分:
Name Description Schema
errors
optional A list of error responses returned when a request is unsuccessful. ErrorList
可知 只有当不成功时,会返回errors,所以我们对于接口的判断,可以通过是否存在errors去进行判断。
```
// 对接亚马逊接口成功
if (!isset($response['errors'])) { }
```
若要了解errors的返回值,可继续看文档:
Error
Error response returned when the request is unsuccessful.
Name Description Schema
code
required An error code that identifies the type of error that occurred. string
message
required A message that describes the error condition. string
details
optional Additional details that can help the caller understand or fix the issue. string
使用CI框架,在对接口的封装的基础上调用接口:
1. 根据
MESSAGING API
Messaging API v1 reference
Messaging API v1 model
Messaging API v1 Use Case Guide
文档目录,规定了,在library目录下的service中,应该新建一个Messaging类 用来调用所需要对接的亚马逊接口:
所有的类都继承自 Service 所以可以调用$this->call方法
```
<?php
namespace library\amazonSdk\service;
use library\amazonSdk\Service;
use Swagger\Client\ObjectSerializer;
class Messaging extends Service
{
public function confirmDeliveryDetails($amazonOrderId, array $marketplaceIds, string $bodyText)
{
$query = [];
$query['marketplaceIds'] = ObjectSerializer::serializeCollection($marketplaceIds, 'csv', true);
return $this->client->call("/messaging/v1/orders/{$amazonOrderId}/messages/confirmDeliveryDetails", 'POST', ['text' => $bodyText], $query); // 规定使用的为POST方法,以及接口所要使用的数据格式
}
}
```
然后 在另外一个service中 去处理接口所需的参数: 然后 传参调用
```
public function doRequestReview(string $amazonOrderId, int $shopId, array $marketplaceIds, string $bodyText)
{
// 接口请求
list($config, $awsConfig, $orderToken) = $this->getAmazonOrderConfigs($shopId);
$client = new Messaging($config, $awsConfig, $orderToken);
return $client->confirmDeliveryDetails($amazonOrderId, $marketplaceIds, $bodyText); // 调用接口方法,传参
}
```
本作品采用《CC 协议》,转载必须注明作者和本文链接
亚马逊的接口好像大部分都有限流的, 需要注意...
命名几乎是伴随程序员职业生涯的课题,一般取决于具体代码风格(code style)规范和个人经验。当然作为非英语母语者我现在会尝试问 copilot 如何命名: