Skip to content

Commit

Permalink
Merge pull request #3 from roboflow-ai/fallback-to-default-workspace-…
Browse files Browse the repository at this point in the history
…api-key-for-public-project-get

fallback to authed workspace api key for getting public projects
  • Loading branch information
hansent authored Nov 29, 2022
2 parents aa4d789 + 19b87c6 commit cad0939
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
12 changes: 6 additions & 6 deletions cli/commands/inference.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const axios = require("axios");
const fs = require("fs");

const api = require("../../api.js");
const { getApiKeyWorWorkspace } = require("../core.js");
const { getApiKeyForWorkspace } = require("../core.js");

async function infer(args, options) {
const workspaceUrl = options.workspace;
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);

// console.log("infer", args, options);

Expand Down Expand Up @@ -49,7 +49,7 @@ async function infer(args, options) {

async function detectObject(args, options) {
const workspaceUrl = options.workspace;
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);

const modelUrl = options.model;
const file = args;
Expand All @@ -63,7 +63,7 @@ async function detectObject(args, options) {

async function classify(args, options) {
const workspaceUrl = options.workspace;
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);

const modelUrl = options.model;
const file = args;
Expand All @@ -74,7 +74,7 @@ async function classify(args, options) {

async function instanceSegmentation(args, options) {
const workspaceUrl = options.workspace;
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);

const modelUrl = options.model;
const file = args;
Expand All @@ -85,7 +85,7 @@ async function instanceSegmentation(args, options) {

async function semanticSegmentation(args, options) {
const workspaceUrl = options.workspace;
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);

const modelUrl = options.model;
const file = args;
Expand Down
15 changes: 10 additions & 5 deletions cli/commands/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { link } = require("../utils");
const config = require("../../config");

const { getWorkspace, getProject } = require("../../api.js");
const { getApiKeyWorWorkspace } = require("../core.js");
const { hasApiKeyForWorkspace, getApiKeyForWorkspace } = require("../core.js");

function printProject(p) {
const app_url = config.get("RF_APP_URL");
Expand All @@ -22,7 +22,7 @@ function printProject(p) {

async function listProjects(options) {
const workspaceUrl = options.workspace;
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);

const workspaceData = await getWorkspace(workspaceUrl, apiKey);

Expand All @@ -45,9 +45,14 @@ async function projectDetails(projectId, options) {
[workspaceUrl, projectUrl] = projectId.split("/");
}

console.log(workspaceUrl, projectUrl);

const apiKey = getApiKeyWorWorkspace(workspaceUrl);
let apiKey;
if (hasApiKeyForWorkspace(workspaceUrl)) {
apiKey = getApiKeyForWorkspace(workspaceUrl);
} else {
//fallback to default workspace or the one sepcified via --workspace if the one in
// the project id is not one we have an api key for (e.g. for public projects on universe)
apiKey = getApiKeyForWorkspace(options.workspace);
}

const result = await getProject(workspaceUrl, projectUrl, apiKey);
console.log(result);
Expand Down
6 changes: 3 additions & 3 deletions cli/commands/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const pLimit = require("p-limit");

const annotation = require("@roboflow/annotation");

const { selectProjectFromWorkspace, getApiKeyWorWorkspace } = require("../core.js");
const { selectProjectFromWorkspace, getApiKeyForWorkspace } = require("../core.js");

const { parseFolder } = require("../datasetParser");

Expand Down Expand Up @@ -44,7 +44,7 @@ async function uploadSimple(f, projectUrl, apiKey, extraOption) {

async function uploadImage(args, options) {
const workspaceUrl = options.workspace;
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);
let projectUrl = options.project;

if (!projectUrl) {
Expand Down Expand Up @@ -129,7 +129,7 @@ async function uploadParsedDatasetImage(

async function importDataset(datasetFolder, options) {
const workspaceUrl = options.workspace;
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);
let projectUrl = options.project;

if (!projectUrl) {
Expand Down
4 changes: 2 additions & 2 deletions cli/commands/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const chalk = require("chalk");
const { link } = require("../utils");

const { getWorkspace } = require("../../api.js");
const { getApiKeyWorWorkspace, selectWorkspace } = require("../core.js");
const { getApiKeyForWorkspace, selectWorkspace } = require("../core.js");

require("util").inspect.defaultOptions.depth = null;

Expand Down Expand Up @@ -54,7 +54,7 @@ async function listWorkspaces() {

async function workspaceDetails(options) {
const workspaceUrl = options.workspace;
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);

const workspaceData = await getWorkspace(workspaceUrl, apiKey);
console.log(workspaceData);
Expand Down
27 changes: 23 additions & 4 deletions cli/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@ const config = require("../config.js");

const { getWorkspace } = require("../api.js");

function getApiKeyWorWorkspace(workspaceId) {
function hasApiKeyForWorkspace(workspaceId) {
const workspaces = config.get("workspaces");

if (!workspaces) {
return false;
}

const workspaceConf = Object.values(workspaces).find(
(ws) => ws.url == workspaceId || ws.id == workspaceId
);

if (workspaceConf && workspaceConf.apiKey) {
return true;
}

return false;
}

function getApiKeyForWorkspace(workspaceId) {
const workspaces = config.get("workspaces");

if (!workspaces) {
Expand Down Expand Up @@ -41,7 +59,7 @@ async function selectWorkspace() {

if (Object.keys(workspaces).length == 1) {
console.log(
"nothign to select from, only 1 default workspace authorized:",
"Nothing to select from, only 1 default workspace authorized:",
chalk.green(Object.values(workspaces)[0].url)
);
return Object.values(workspaces)[0].url;
Expand Down Expand Up @@ -69,7 +87,7 @@ async function selectWorkspace() {
}

async function selectProjectFromWorkspace(workspaceUrl) {
const apiKey = getApiKeyWorWorkspace(workspaceUrl);
const apiKey = getApiKeyForWorkspace(workspaceUrl);
const workspaceData = await getWorkspace(workspaceUrl, apiKey);
const projects = workspaceData.workspace?.projects;

Expand Down Expand Up @@ -109,7 +127,8 @@ async function selectProjectFromWorkspace(workspaceUrl) {
}

module.exports = {
getApiKeyWorWorkspace,
hasApiKeyForWorkspace,
getApiKeyForWorkspace,
selectWorkspace,
selectProjectFromWorkspace
};

0 comments on commit cad0939

Please sign in to comment.