Skip to content

Commit

Permalink
[RNMobile] Fetch VideoPress token (#29756)
Browse files Browse the repository at this point in the history
* Add native version of `getMediaToken`

* Fetch token upon block mount

* Add changelog

* Fix lint issue

* Revert code removal after merge with `trunk`
  • Loading branch information
fluiddot authored Apr 5, 2023
1 parent 0184dbe commit 9608824
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

VideoPress block: Add native version of `getMediaToken` function to fetch the VideoPress token.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { View } from 'react-native';
/**
* Internal dependencies
*/
import getMediaToken from '../../../lib/get-media-token/index.native';
import { buildVideoPressURL, getVideoPressUrl } from '../../../lib/url';
import { usePreview } from '../../hooks/use-preview';
import isLocalFile from '../../utils/is-local-file.native';
Expand Down Expand Up @@ -76,6 +77,21 @@ export default function VideoPressEdit( {
isReplacing: false,
prevAttrs: {},
} );
const [ , setToken ] = useState< string >();

// Fetch token for a VideoPress GUID
useEffect( () => {
if ( guid ) {
getMediaToken( 'playback', { guid } )
.then( tokenData => {
setToken( tokenData.token );
} )
.catch( error => {
// eslint-disable-next-line no-console
console.error( "Can't obtain the token:", error );
} );
}
}, [ guid ] );

const [ showReplaceControl, setShowReplaceControl ] = useState( true );

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* WordPress dependencies
*/
import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import { MEDIA_TOKEN_SCOPES } from './types';
/**
* Types
*/
import type { MediaTokenScopeProps, MediaTokenProps, GetMediaTokenArgsProps } from './types';

type Response = {
metadata_token: string;
blog_id: string;
};

/**
* Request media token data hiting WPCOM API.
*
* @param {MediaTokenScopeProps} scope - The scope of the token to request.
* @param {GetMediaTokenArgsProps} args - function arguments.
* @returns {MediaTokenProps} Media token data.
*/
const requestMediaToken = function (
scope: MediaTokenScopeProps,
args: GetMediaTokenArgsProps = {}
): Promise< MediaTokenProps > {
const { guid } = args;
return new Promise( function ( resolve, reject ) {
if ( ! MEDIA_TOKEN_SCOPES.includes( scope ) ) {
return reject( 'Invalid scope' );
}

const fetchParams: { path: string; body: object } = { path: '', body: {} };

switch ( scope ) {
case 'upload':
return reject( '"upload" scope is not supported.' );

case 'upload-jwt':
return reject( '"upload-jwt" scope is not supported.' );

case 'playback':
fetchParams.path = `/wpcom/v2/media/videopress-playback-jwt/${ guid }`;
fetchParams.body = {};
break;
}

apiFetch< Response >( {
path: fetchParams.path,
method: 'POST',
body: fetchParams.body,
} )
.then( response => {
resolve( { token: response.metadata_token } );
} )
.catch( error => {
console.warn( 'Token is not achievable', error ); // eslint-disable-line no-console
resolve( { token: null } );
} );
} );
};

/**
* Return media token data from fetch request.
*
* NOTE: In the native version, the token is not persisted.
*
* @param {MediaTokenScopeProps} scope - The scope of the token to request.
* @param {GetMediaTokenArgsProps} args - function arguments.
* @returns {MediaTokenProps} Media token data.
*/
async function getMediaToken(
scope: MediaTokenScopeProps,
args: GetMediaTokenArgsProps = {}
): Promise< MediaTokenProps > {
const { flushToken } = args;

if ( flushToken ) {
// eslint-disable-next-line no-console
console.warn( 'Token is not persisted in the native version.' );
}

return await requestMediaToken( scope, args );
}

export default getMediaToken;

0 comments on commit 9608824

Please sign in to comment.