Skip to content

Commit

Permalink
chnage variable name and flow changes for extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratik Patel committed May 25, 2021
1 parent d8b77f0 commit b61d977
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 44 deletions.
23 changes: 7 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ FDK Extension Helper Library
const redis = new Redis();

let extensionHandler = {
setup: async function(data) {
console.log("called setup callback");
},

install: async function(data) {
console.log("called install callback");
},
Expand All @@ -37,22 +33,17 @@ FDK Extension Helper Library
let baseUrl = "https://test.extension.com";


let FDKClient = setupFdk({
name: "Transformer",
let fdkClient = setupFdk({
api_key: "<API_KEY>",
api_secret: "<API_SECRET>",
base_url: baseUrl,
logo : {
small: `${baseUrl}\icon.png`,
large: `${baseUrl}\icon.png`,
},
scopes: ["company/products"],
prefix_path: "/callback",
callbacks: extensionHandler,
contact_email: "xyz@gmail.com",
developed_by_name: "Fynd",
webhooks: [],
storage: new RedisStorage(redis)
storage: new RedisStorage(redis),
access_mode: "offline",
cluster: "https://api.fyndx0.de" // this is optional by default it points to prod.
});
app.use(FDKClient.fdkHandler);
app.use(fdkClient.fdkHandler);

app.listen(8080);
```
31 changes: 7 additions & 24 deletions examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ console.log(baseUrl);

const redis = new Redis();

let FDKExtension = setupFdk({
let fdkExtension = setupFdk({
api_key: "000001",
api_secret: "tetsjskdjalsjdl",
base_url: baseUrl,
Expand All @@ -34,14 +34,14 @@ let FDKExtension = setupFdk({
cluster: "https://api.fyndx0.de" // this is optional by default it points to prod.
});

app.use(FDKExtension.fdkHandler);
app.use(fdkExtension.fdkHandler);
app.use('/_healthz', (req, res, next) => {
res.json({
"ok": "ok"
});
});

FDKExtension.apiRoutes.get("/test/routes", async (req, res, next) => {
fdkExtension.apiRoutes.get("/test/routes", async (req, res, next) => {
try {
let data = await req.platformClient.lead.getTickets();
res.json(data);
Expand All @@ -52,7 +52,7 @@ FDKExtension.apiRoutes.get("/test/routes", async (req, res, next) => {

});

FDKExtension.applicationProxyRoutes.get("/1234", async (req, res, next) => {
fdkExtension.applicationProxyRoutes.get("/1234", async (req, res, next) => {
try {
let data = await req.platformClient.lead.getTickets();
res.json(data);
Expand All @@ -63,33 +63,16 @@ FDKExtension.applicationProxyRoutes.get("/1234", async (req, res, next) => {

});

app.use(FDKExtension.applicationProxyRoutes);
app.use(FDKExtension.apiRoutes);

// FDKExtension.apiRoutes.post("/:application_id", async (req, res, next) => {
// try {
// const { application_id } = req.params;
// const { platformClient } = req;
// const platformApiUrl = `https://api.fyndx0.de/service/platform/partners/v1.0/company/${req.fdkSession.company_id}/application/${application_id}/proxy/${platformClient.config.apiKey}`;
// response = await APIClient.execute(platformClient.config, 'post', platformApiUrl, {}, {
// attached_path: "chatbot-test",
// proxy_url: `https://chatbots.extensions.fyndx0.de/proxy/application`
// });
// return res.json(response);
// }
// catch(err) {
// next(err);
// }
// });

app.use(fdkExtension.applicationProxyRoutes);
app.use(fdkExtension.apiRoutes);

// sample webhook endpoint
const webhookRouter = express.Router({ mergeParams: true });
webhookRouter.get("/webhook", async (req, res, next) => {
// fetch company id from query params
let companyId = req.query.companyId;
let cluster = "https://api.fyndx0.de"; // either take it from some env variables like "https://api.fyndx0.de"
let client = await FDKExtension.getPlatformClient(cluster, companyId);
let client = await fdkExtension.getPlatformClient(cluster, companyId);
res.json({"success": true});
});

Expand Down
2 changes: 1 addition & 1 deletion express/api_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function setupProxyRoutes() {

apiRoutes.use(sessionMiddleware(true), async (req, res, next) => {
try {
const client = extension.getPlatformClient(req.fdkSession.company_id, req.fdkSession);
const client = await extension.getPlatformClient(req.fdkSession.company_id, req.fdkSession);
req.platformClient = client;
req.extension = extension;
next();
Expand Down
3 changes: 2 additions & 1 deletion express/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ class Extension {
return platformConfig;
}

getPlatformClient(companyId, session) {
async getPlatformClient(companyId, session) {
let platformConfig = this.getPlatformConfig(companyId);
platformConfig.oauthClient.setToken(session);
await platformConfig.oauthClient.renewAccessToken();
return new PlatformClient(platformConfig);
}
}
Expand Down
2 changes: 1 addition & 1 deletion express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function setupFdk(data) {
companyId: companyId
});
let session = await SessionStorage.getSession(sid);
client = extension.getPlatformClient(companyId, session);
client = await extension.getPlatformClient(companyId, session);
}
return client;
}
Expand Down
2 changes: 1 addition & 1 deletion express/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function setupRoutes(ext) {
companyId: company_id
});
let session = await SessionStorage.getSession(sid);
const client = ext.getPlatformClient(company_id, session);
const client = await ext.getPlatformClient(company_id, session);
req.platformClient = client;
}
req.extension = ext;
Expand Down

0 comments on commit b61d977

Please sign in to comment.