Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

googleApi.native.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // @flow
  2. import {
  3. GoogleSignin
  4. } from 'react-native-google-signin';
  5. import {
  6. API_URL_BROADCAST_STREAMS,
  7. API_URL_LIVE_BROADCASTS
  8. } from './constants';
  9. /**
  10. * Class to encapsulate Google API functionalities and provide a similar
  11. * interface to what WEB has. The methods are different, but the point is that
  12. * the export object is similar so no need for different export logic.
  13. *
  14. * For more detailed documentation of the {@code GoogleSignin} API, please visit
  15. * https://github.com/react-native-community/react-native-google-signin.
  16. */
  17. class GoogleApi {
  18. /**
  19. * Wraps the {@code GoogleSignin.configure} method.
  20. *
  21. * @param {Object} config - The config object to be passed to
  22. * {@code GoogleSignin.configure}.
  23. * @returns {void}
  24. */
  25. configure(config: Object) {
  26. GoogleSignin.configure(config);
  27. }
  28. /**
  29. * Retrieves the available YouTube streams the user can use for live
  30. * streaming.
  31. *
  32. * @param {string} accessToken - The Google auth token.
  33. * @returns {Promise}
  34. */
  35. getYouTubeLiveStreams(accessToken: string): Promise<*> {
  36. return new Promise((resolve, reject) => {
  37. // Fetching the list of available broadcasts first.
  38. this._fetchGoogleEndpoint(accessToken,
  39. API_URL_LIVE_BROADCASTS)
  40. .then(broadcasts => {
  41. // Then fetching all the available live streams that the
  42. // user has access to with the broadcasts we retreived
  43. // earlier.
  44. this._getLiveStreamsForBroadcasts(
  45. accessToken, broadcasts).then(resolve, reject);
  46. }, reject);
  47. });
  48. }
  49. /**
  50. * Wraps the {@code GoogleSignin.hasPlayServices} method.
  51. *
  52. * @returns {Promise<*>}
  53. */
  54. hasPlayServices() {
  55. return GoogleSignin.hasPlayServices();
  56. }
  57. /**
  58. * Wraps the {@code GoogleSignin.signIn} method.
  59. *
  60. * @returns {Promise<*>}
  61. */
  62. signIn() {
  63. return GoogleSignin.signIn();
  64. }
  65. /**
  66. * Wraps the {@code GoogleSignin.signInSilently} method.
  67. *
  68. * @returns {Promise<*>}
  69. */
  70. signInSilently() {
  71. return GoogleSignin.signInSilently();
  72. }
  73. /**
  74. * Wraps the {@code GoogleSignin.signOut} method.
  75. *
  76. * @returns {Promise<*>}
  77. */
  78. signOut() {
  79. return GoogleSignin.signOut();
  80. }
  81. /**
  82. * Helper method to fetch a Google API endpoint in a generic way.
  83. *
  84. * @private
  85. * @param {string} accessToken - The access token used for the API call.
  86. * @param {string} endpoint - The endpoint to fetch, including the URL
  87. * params if needed.
  88. * @returns {Promise}
  89. */
  90. _fetchGoogleEndpoint(accessToken, endpoint): Promise<*> {
  91. return new Promise((resolve, reject) => {
  92. const headers = {
  93. Authorization: `Bearer ${accessToken}`
  94. };
  95. fetch(endpoint, {
  96. headers
  97. }).then(response => response.json())
  98. .then(responseJSON => {
  99. if (responseJSON.error) {
  100. reject(responseJSON.error.message);
  101. } else {
  102. resolve(responseJSON.items || []);
  103. }
  104. }, reject);
  105. });
  106. }
  107. /**
  108. * Retrieves the available YouTube streams that are available for the
  109. * provided broadcast IDs.
  110. *
  111. * @private
  112. * @param {string} accessToken - The Google access token.
  113. * @param {Array<Object>} broadcasts - The list of broadcasts that we want
  114. * to retreive streams for.
  115. * @returns {Promise}
  116. */
  117. _getLiveStreamsForBroadcasts(accessToken, broadcasts): Promise<*> {
  118. return new Promise((resolve, reject) => {
  119. const ids = [];
  120. for (const broadcast of broadcasts) {
  121. broadcast.contentDetails
  122. && broadcast.contentDetails.boundStreamId
  123. && ids.push(broadcast.contentDetails.boundStreamId);
  124. }
  125. this._fetchGoogleEndpoint(
  126. accessToken,
  127. `${API_URL_BROADCAST_STREAMS}${ids.join(',')}`)
  128. .then(streams => {
  129. const keys = [];
  130. // We construct an array of keys bind with the broadcast
  131. // name for a nice display.
  132. for (const stream of streams) {
  133. const key = stream.cdn.ingestionInfo.streamName;
  134. let title;
  135. // Finding title from the broadcast with the same
  136. // channelId. If not found (unknown scenario), we use
  137. // the key as title again.
  138. for (const broadcast of broadcasts) {
  139. if (broadcast.snippet.channelId
  140. === stream.snippet.channelId) {
  141. title = broadcast.snippet.title;
  142. }
  143. }
  144. keys.push({
  145. key,
  146. title: title || key
  147. });
  148. }
  149. resolve(keys);
  150. }, reject);
  151. });
  152. }
  153. }
  154. export default new GoogleApi();