Skip to content

Commit

Permalink
Merge pull request #71 from gofynd/partner-client
Browse files Browse the repository at this point in the history
Getting partner client for offline token support added.
  • Loading branch information
brijeshgajjarfynd authored Nov 20, 2024
2 parents 0c0bc75 + e4de87c commit 352fc34
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.7.12] - 2024-11-18
### Changed
- Added offline token support for partner clients.

## [v0.7.11] - 2024-09-23
### Changed
- Kafka config response key change in api.
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ function backgroundHandler(companyId) {
}
```


#### How to call partner apis in background tasks?

Background tasks running under some consumer or webhook or under any queue can get partner client via method `getPartnerClient`. It will return instance of `PartnerClient` as well.

> Here FdkClient `access_mode` should be **offline**. Cause such client can only access PartnerClient in background task.
```javascript
function backgroundHandler(organizationId) {
try {
const partnerClient = await fdkExtension.getPartnerClient(organizationId);
let data = await partnerClient.webhook.responseTimeSummary({
extensionId: '<EXTENSION_ID>',
startDate: '<START_DATE>',
endDate: '<END_DATE>'
});
// Some business logic here
res.json({ success: true });
} catch (err) {
console.error(err);
res.status(404).json({ success: false });
}
}
```

#### How to call partner apis?

To call partner api you need to have instance of `PartnerClient`. Instance holds methods for SDK classes. All routes registered under `partnerApiRoutes` express router will have `partnerClient` under request object which is instance of `PartnerClient`.
Expand Down
2 changes: 1 addition & 1 deletion express/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class Extension {
partnerConfig.oauthClient.setToken(session);
partnerConfig.oauthClient.token_expires_at = session.access_token_validity;

if (session.access_token_validity && session.refresh_token) {
if (!session.access_token_validity || session.refresh_token) {
let ac_nr_expired = ((session.access_token_validity - new Date().getTime()) / 1000) <= 120;
if (ac_nr_expired) {
logger.debug(`Renewing access token for organization ${organizationId} with platform config ${logger.safeStringify(partnerConfig)}`);
Expand Down
14 changes: 14 additions & 0 deletions express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ function setupFdk(data, syncInitialization) {
return applicationClient;
}

async function getPartnerClient(organizationId) {
let client = null;
if (!extension.isOnlineAccessMode()) {
let sid = Session.generateSessionId(false, {
cluster: extension.cluster,
id: organizationId
});
let session = await SessionStorage.getSession(sid);
client = await extension.getPartnerClient(organizationId, session);
}
return client;
}

const configInstance = {
fdkHandler: router,
extension: extension,
Expand All @@ -53,6 +66,7 @@ function setupFdk(data, syncInitialization) {
webhookRegistry: extension.webhookRegistry,
applicationProxyRoutes: applicationProxyRoutes,
getPlatformClient: getPlatformClient,
getPartnerClient: getPartnerClient,
getApplicationClient: getApplicationClient
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
},
"name": "@gofynd/fdk-extension-javascript",
"description": "FDK Extension Helper Library",
"version": "0.7.11",
"version": "0.7.12",
"main": "index.js",
"directories": {
"example": "examples"
Expand Down

0 comments on commit 352fc34

Please sign in to comment.