Merge pull request #1016 from mozilla/api-prefix

Add setApiUrlPrefix and use it in the android version.
This commit is contained in:
Danny Coates 2018-11-19 15:24:14 -08:00 committed by GitHub
commit eb1536878a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 14 deletions

View file

@ -23,6 +23,7 @@ import choo from 'choo';
import html from 'choo/html'; import html from 'choo/html';
import Raven from 'raven-js'; import Raven from 'raven-js';
import { setApiUrlPrefix } from '../app/api';
import assets from '../common/assets'; import assets from '../common/assets';
import Header from '../app/ui/header'; import Header from '../app/ui/header';
import locale from '../common/locales'; import locale from '../common/locales';
@ -38,6 +39,7 @@ import error from './pages/error';
if (navigator.userAgent === 'Send Android') { if (navigator.userAgent === 'Send Android') {
assets.setPrefix('/android_asset'); assets.setPrefix('/android_asset');
setApiUrlPrefix('https://send2.dev.lcip.org');
} }
const app = choo(); const app = choo();

View file

@ -8,6 +8,7 @@
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">

View file

@ -20,6 +20,15 @@ export function getFileProtocolWssUrl() {
return fileProtocolWssUrl; return fileProtocolWssUrl;
} }
let apiUrlPrefix = '';
export function getApiUrl(path) {
return apiUrlPrefix + path;
}
export function setApiUrlPrefix(prefix) {
apiUrlPrefix = prefix;
}
function post(obj, bearerToken) { function post(obj, bearerToken) {
const h = { const h = {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -62,13 +71,16 @@ async function fetchWithAuthAndRetry(url, params, keychain) {
} }
export async function del(id, owner_token) { export async function del(id, owner_token) {
const response = await fetch(`/api/delete/${id}`, post({ owner_token })); const response = await fetch(
getApiUrl(`/api/delete/${id}`),
post({ owner_token })
);
return response.ok; return response.ok;
} }
export async function setParams(id, owner_token, bearerToken, params) { export async function setParams(id, owner_token, bearerToken, params) {
const response = await fetch( const response = await fetch(
`/api/params/${id}`, getApiUrl(`/api/params/${id}`),
post( post(
{ {
owner_token, owner_token,
@ -81,7 +93,10 @@ export async function setParams(id, owner_token, bearerToken, params) {
} }
export async function fileInfo(id, owner_token) { export async function fileInfo(id, owner_token) {
const response = await fetch(`/api/info/${id}`, post({ owner_token })); const response = await fetch(
getApiUrl(`/api/info/${id}`),
post({ owner_token })
);
if (response.ok) { if (response.ok) {
const obj = await response.json(); const obj = await response.json();
@ -93,7 +108,7 @@ export async function fileInfo(id, owner_token) {
export async function metadata(id, keychain) { export async function metadata(id, keychain) {
const result = await fetchWithAuthAndRetry( const result = await fetchWithAuthAndRetry(
`/api/metadata/${id}`, getApiUrl(`/api/metadata/${id}`),
{ method: 'GET' }, { method: 'GET' },
keychain keychain
); );
@ -115,7 +130,7 @@ export async function metadata(id, keychain) {
export async function setPassword(id, owner_token, keychain) { export async function setPassword(id, owner_token, keychain) {
const auth = await keychain.authKeyB64(); const auth = await keychain.authKeyB64();
const response = await fetch( const response = await fetch(
`/api/password/${id}`, getApiUrl(`/api/password/${id}`),
post({ owner_token, auth }) post({ owner_token, auth })
); );
return response.ok; return response.ok;
@ -251,7 +266,7 @@ export function uploadWs(
async function downloadS(id, keychain, signal) { async function downloadS(id, keychain, signal) {
const auth = await keychain.authHeader(); const auth = await keychain.authHeader();
const response = await fetch(`/api/download/${id}`, { const response = await fetch(getApiUrl(`/api/download/${id}`), {
signal: signal, signal: signal,
method: 'GET', method: 'GET',
headers: { Authorization: auth } headers: { Authorization: auth }
@ -323,7 +338,7 @@ function download(id, keychain, onprogress, canceller) {
} }
}); });
const auth = await keychain.authHeader(); const auth = await keychain.authHeader();
xhr.open('get', `/api/download/blob/${id}`); xhr.open('get', getApiUrl(`/api/download/blob/${id}`));
xhr.setRequestHeader('Authorization', auth); xhr.setRequestHeader('Authorization', auth);
xhr.responseType = 'blob'; xhr.responseType = 'blob';
xhr.send(); xhr.send();
@ -358,7 +373,7 @@ export function downloadFile(id, keychain, onprogress) {
export async function getFileList(bearerToken) { export async function getFileList(bearerToken) {
const headers = new Headers({ Authorization: `Bearer ${bearerToken}` }); const headers = new Headers({ Authorization: `Bearer ${bearerToken}` });
const response = await fetch('/api/filelist', { headers }); const response = await fetch(getApiUrl('/api/filelist'), { headers });
if (response.ok) { if (response.ok) {
return response.body; // stream return response.body; // stream
} }
@ -367,7 +382,7 @@ export async function getFileList(bearerToken) {
export async function setFileList(bearerToken, data) { export async function setFileList(bearerToken, data) {
const headers = new Headers({ Authorization: `Bearer ${bearerToken}` }); const headers = new Headers({ Authorization: `Bearer ${bearerToken}` });
const response = await fetch('/api/filelist', { const response = await fetch(getApiUrl('/api/filelist'), {
headers, headers,
method: 'POST', method: 'POST',
body: data body: data

View file

@ -1,7 +1,7 @@
import Nanobus from 'nanobus'; import Nanobus from 'nanobus';
import Keychain from './keychain'; import Keychain from './keychain';
import { delay, bytes, streamToArrayBuffer } from './utils'; import { delay, bytes, streamToArrayBuffer } from './utils';
import { downloadFile, metadata } from './api'; import { downloadFile, metadata, getApiUrl } from './api';
import { blobStream } from './streams'; import { blobStream } from './streams';
import Zip from './zip'; import Zip from './zip';
@ -145,14 +145,18 @@ export default class FileReceiver extends Nanobus {
onprogress(0); onprogress(0);
if (noSave) { if (noSave) {
const res = await fetch(`/api/download/${this.fileInfo.id}`); const res = await fetch(getApiUrl(`/api/download/${this.fileInfo.id}`));
if (res.status !== 200) { if (res.status !== 200) {
throw new Error(res.status); throw new Error(res.status);
} }
} else { } else {
const downloadUrl = `${location.protocol}//${ const downloadPath = `/api/download/${this.fileInfo.id}`;
location.host let downloadUrl = getApiUrl(downloadPath);
}/api/download/${this.fileInfo.id}`; if (downloadUrl === downloadPath) {
downloadUrl = `${location.protocol}//${location.host}/api/download/${
this.fileInfo.id
}`;
}
const a = document.createElement('a'); const a = document.createElement('a');
a.href = downloadUrl; a.href = downloadUrl;
document.body.appendChild(a); document.body.appendChild(a);