定制 remp/crm-api-module 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

remp/crm-api-module

Composer 安装命令:

composer require remp/crm-api-module

包简介

CRM Api Module

README 文档

README

This documentation serves as a complement to CRM Skeleton documentation related to creating and registering API handlers.

It describes API handlers provided by this module for others to use.

Enabling module

Make sure you add extension to your app/config/config.neon file.

extensions:
	- Crm\ApiModule\DI\ApiModuleExtension

Configuration

Preflight requests

Module allows you to configure preflight request handling. Add following snippet to your config.neon file:

services:
	# ...
	apiHeadersConfig:
		setup:
			- setAllowedOrigins('*')
			- setAllowedHeaders('Content-Type', 'Authorization', 'X-Requested-With')
			- setAllowedHttpMethods('*')
			- setAllowedCredentials(true)
			- setAccessControlMaxAge(600) # seconds

You can configure allowed origins by explicitly stating them or by using wildcards. Following configurations are valid:

  • setAllowedOrigins("*"). Matches everything
  • setAllowedOrigins("foo.bar", "*.foo.bar"). Matches foo.bar and all of its subdomains.
  • setAllowedOrigins("foo.bar", "1.foo.bar"). Matches foo.bar, 1.foo.bar, but nothing else (nor any other subdomain).

API requests logging

You can globally enable or disable API logging in the CRM admin - config section. If the logging is enabled, you can further configure which specific paths should be logged or not.

We recommend using configuration of blacklist over whitelist. Otherwise you might encounter a scenario, where logs from one of our APIs might be missing when you need them.

To enable blacklist, add following snippet to your config.neon file:

services:
	# ...
	apiLoggerConfig:
		setup:
			- setPathBlacklist([
				Crm\ApiModule\Models\Api\LoggerEndpointIdentifier('1', 'foo', 'bar'),
			])

The LoggerEndpointIdentifier requires three parameters: version, package and apiCall - same parameters which you use when you register a new API handler. The snippet above will not log requests to the /api/v1/foo/bar endpoint.

You can also use wildcards where necessary:

  • Crm\ApiModule\Models\Api\LoggerEndpointIdentifier('*', 'foo', 'bar') will match all requests going to /api/*/foo/bar.
  • Crm\ApiModule\Models\Api\LoggerEndpointIdentifier('1', '*', '*') will match all requests going to /api/v3/*/*.

Blacklist and whitelist cannot be combined, the latter configured wins.

By default the API logger redacts standard fields found in GET parameters, POST parameters and JSON payload parameters: password, token and auth. If you need to extend this and redact other parameters, you can extend the redaction:

	# ...
	apiLoggerConfig:
		setup:
			- addRedactedFields(['internal_token'])

Data retention configuration

You can configure time before which application:cleanup deletes old repository data and column which it uses by using (in your project configuration file):

apiLogsRepository:
	setup:
		- setRetentionThreshold('-2 months', 'created_at')

Database tables migration

Because of need of changing primary keys (int -> bigint), in tables that contain lots of data (or have risk of overflowing primary key if its int), we had to create migration process. Since some tables are very exposed and cannot be locked for more than a couple of seconds, we decided to create new tables, migrate the data manually and keep the old and new table in sync while migrating.

This migration process is necessary only for installations after specific version for specific table, and is two steps process.

Api logs migration (installed before version 2.5.0)

Consists of api_logs table migration.

Steps:

  1. Run phinx migrations command phinx:migrate, which creates new table api_logs_v2 (in case there is no data in table, migration just changes type of primary key and next steps are not needed).
  2. Run command api:convert_api_logs_to_bigint, which copies data from old table to new (e.g. api_logs to api_logs_v2). Command will after successful migration atomically rename tables (e.g. api_logs -> api_logs_old and api_logs_v2 -> api_logs), so when the migration ends only new tables are used.

It's recommended to run application:bigint_migration_cleanup api_logs command, at least 2 weeks (to preserve backup data, if some issue emerges) after successful migration to drop left-over tables.

API documentation

All examples use http://crm.press as a base domain. Please change the host to the one you use before executing the examples.

All examples use XXX as a default value for authorization token, please replace it with the real tokens:

  • API tokens. Standard API keys for server-server communication. It identifies the calling application as a whole. They can be generated in CRM Admin (/api/api-tokens-admin/) and each API key has to be whitelisted to access specific API endpoints. By default the API key has access to no endpoint.
  • User tokens. Generated for each user during the login process, token identify single user when communicating between different parts of the system. The token can be read:
    • From n_token cookie if the user was logged in via CRM.
    • From the response of /api/v1/users/login endpoint - you're free to store the response into your own cookie/local storage/session.

API responses can contain following HTTP codes:

Value Description
200 OK Successful response, default value
400 Bad Request Invalid request (missing required parameters)
403 Forbidden The authorization failed (provided token was not valid)
404 Not found Referenced resource wasn't found

If possible, the response includes application/json encoded payload with message explaining the error further.

GET /api/v1/token/check

API servers for checking the validity of provided Bearer token within header.

Headers:
Name Value Required
Authorization Bearer XXX yes
Example:
curl -v –X GET http://crm.press/api/v1/token/check \ 
-H "Content-Type:application/json" \
-H "Authorization: Bearer XXX"

Response:

{
    "status": "ok"
}

remp/crm-api-module 适用场景与选型建议

remp/crm-api-module 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53.01k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 remp/crm-api-module 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 remp/crm-api-module 我们能提供哪些服务?
定制开发 / 二次开发

基于 remp/crm-api-module 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 53.01k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 5
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-02