Spotify.NET/assets/js/755cfc2e.8a798d94.js
2022-11-18 10:28:22 +00:00

1 line
8.4 KiB
JavaScript

"use strict";(self.webpackChunkspotify_api_docs=self.webpackChunkspotify_api_docs||[]).push([[5003],{3905:function(e,t,n){n.d(t,{Zo:function(){return c},kt:function(){return g}});var a=n(7294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function i(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?i(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):i(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function l(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},i=Object.keys(e);for(a=0;a<i.length;a++)n=i[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(a=0;a<i.length;a++)n=i[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var s=a.createContext({}),p=function(e){var t=a.useContext(s),n=t;return e&&(n="function"==typeof e?e(t):o(o({},t),e)),n},c=function(e){var t=p(e.components);return a.createElement(s.Provider,{value:t},e.children)},u={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},d=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,i=e.originalType,s=e.parentName,c=l(e,["components","mdxType","originalType","parentName"]),d=p(n),g=r,m=d["".concat(s,".").concat(g)]||d[g]||u[g]||i;return n?a.createElement(m,o(o({ref:t},c),{},{components:n})):a.createElement(m,o({ref:t},c))}));function g(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var i=n.length,o=new Array(i);o[0]=d;var l={};for(var s in t)hasOwnProperty.call(t,s)&&(l[s]=t[s]);l.originalType=e,l.mdxType="string"==typeof e?e:r,o[1]=l;for(var p=2;p<i;p++)o[p]=n[p];return a.createElement.apply(null,o)}return a.createElement.apply(null,n)}d.displayName="MDXCreateElement"},210:function(e,t,n){n.r(t),n.d(t,{assets:function(){return c},contentTitle:function(){return s},default:function(){return g},frontMatter:function(){return l},metadata:function(){return p},toc:function(){return u}});var a=n(7462),r=n(3366),i=(n(7294),n(3905)),o=["components"],l={id:"pagination",title:"Pagination"},s=void 0,p={unversionedId:"pagination",id:"pagination",title:"Pagination",description:"When working with Spotify responses, you will often encounter the Paging type.",source:"@site/docs/pagination.md",sourceDirName:".",slug:"/pagination",permalink:"/SpotifyAPI-NET/docs/pagination",draft:!1,editUrl:"https://github.com/JohnnyCrazy/SpotifyAPI-NET/edit/master/SpotifyAPI.Docs/docs/pagination.md",tags:[],version:"current",lastUpdatedBy:"dependabot[bot]",lastUpdatedAt:1668767198,formattedLastUpdatedAt:"11/18/2022",frontMatter:{id:"pagination",title:"Pagination"},sidebar:"docs",previous:{title:"Proxy",permalink:"/SpotifyAPI-NET/docs/proxy"},next:{title:"Retry Handling",permalink:"/SpotifyAPI-NET/docs/retry_handling"}},c={},u=[{value:"PaginateAll",id:"paginateall",level:2},{value:"Paginate",id:"paginate",level:2},{value:"Paginators",id:"paginators",level:2}],d={toc:u};function g(e){var t=e.components,n=(0,r.Z)(e,o);return(0,i.kt)("wrapper",(0,a.Z)({},d,n,{components:t,mdxType:"MDXLayout"}),(0,i.kt)("p",null,"When working with Spotify responses, you will often encounter the ",(0,i.kt)("inlineCode",{parentName:"p"},"Paging<T>")," type."),(0,i.kt)("blockquote",null,(0,i.kt)("p",{parentName:"blockquote"},"The offset-based paging object is a container for a set of objects. It contains a key called Items (whose value is an array of the requested objects) along with other keys like Previous, Next and Limit which can be useful in future calls.")),(0,i.kt)("p",null,"It allows you to only receive a subset of all available data and dynamically check if more requests are required. The library supports ",(0,i.kt)("inlineCode",{parentName:"p"},"Paging<T>")," responses in two ways:"),(0,i.kt)("h2",{id:"paginateall"},"PaginateAll"),(0,i.kt)("p",null,(0,i.kt)("inlineCode",{parentName:"p"},"PaginateAll")," will query all remaining elements based on a first page and return all of them in an ",(0,i.kt)("inlineCode",{parentName:"p"},"IList"),". This method should not be used for huge amounts of pages (e.g ",(0,i.kt)("inlineCode",{parentName:"p"},"Search")," Endpoint), since it stores every response in memory."),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-csharp"},"// we need the first page\nvar page = await spotify.Playlists.CurrentUsers();\n\n// allPages will include the first page retrived before\nvar allPages = await spotify.PaginateAll(page);\n")),(0,i.kt)("h2",{id:"paginate"},"Paginate"),(0,i.kt)("div",{className:"admonition admonition-info alert alert--info"},(0,i.kt)("div",{parentName:"div",className:"admonition-heading"},(0,i.kt)("h5",{parentName:"div"},(0,i.kt)("span",{parentName:"h5",className:"admonition-icon"},(0,i.kt)("svg",{parentName:"span",xmlns:"http://www.w3.org/2000/svg",width:"14",height:"16",viewBox:"0 0 14 16"},(0,i.kt)("path",{parentName:"svg",fillRule:"evenodd",d:"M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"}))),".NET Standard >= 2.1 required")),(0,i.kt)("div",{parentName:"div",className:"admonition-content"})),(0,i.kt)("p",null,(0,i.kt)("inlineCode",{parentName:"p"},"Paginate")," is based on ",(0,i.kt)("inlineCode",{parentName:"p"},"IAsyncEnumerable")," and streams pages instead of returning them all in one list. This allows it to break the fetching early and keep only 1 page in memory at a time. This method should always be preferred to ",(0,i.kt)("inlineCode",{parentName:"p"},"PaginateAll"),"."),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-csharp"},'// we need the first page\nvar page = await spotify.Playlists.CurrentUsers();\n\nawait foreach(var item in spotify.Paginate(page))\n{\n Console.WriteLine(item.Name);\n // you can use "break" here!\n}\n')),(0,i.kt)("p",null,"Some endpoints have nested and/or multiple paginations objects. When requesting the next page, it will not return the actual paging object but rather the root level endpoint object. A good example is the ",(0,i.kt)("inlineCode",{parentName:"p"},"Search")," endpoint, which contains up to 5 Paging objects. Requesting the next page of the nested ",(0,i.kt)("inlineCode",{parentName:"p"},"Artists")," paging object will return another ",(0,i.kt)("inlineCode",{parentName:"p"},"Search")," response, instead of just ",(0,i.kt)("inlineCode",{parentName:"p"},"Artists"),". You will need to supply a mapper function to the ",(0,i.kt)("inlineCode",{parentName:"p"},"Paginate")," call, which returns the correct paging object:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-csharp"},'var search = await spotify.Search.Item(new SearchRequest(\n SearchRequest.Types.All, "Jake"\n));\n\nawait foreach(var item in spotify.Paginate(search.Albums, (s) => s.Albums))\n{\n Console.WriteLine(item.Name);\n // you can use "break" here!\n}\n')),(0,i.kt)("h2",{id:"paginators"},"Paginators"),(0,i.kt)("p",null,"Via the interface ",(0,i.kt)("a",{parentName:"p",href:"https://github.com/JohnnyCrazy/SpotifyAPI-NET/blob/master/SpotifyAPI.Web/Clients/Interfaces/IPaginator.cs"},(0,i.kt)("inlineCode",{parentName:"a"},"IPaginator")),", it can be configured how pages are fetched. It can be configured on a global level:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-csharp"},"var config = SpotifyClientConfig\n .CreateDefault()\n .WithPaginator(new YourCustomPaginator());\n")),(0,i.kt)("p",null,"or on method level:"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-csharp"},'await foreach(var item in spotify.Paginate(page, new YourCustomPaginator()))\n{\n Console.WriteLine(item.Name);\n // you can use "break" here!\n}\n')),(0,i.kt)("p",null,"By default, ",(0,i.kt)("a",{parentName:"p",href:"https://github.com/JohnnyCrazy/SpotifyAPI-NET/blob/master/SpotifyAPI.Web/Clients/SimplePaginator.cs"},(0,i.kt)("inlineCode",{parentName:"a"},"SimplePaginator"))," is used. It fetches pages without any delay."))}g.isMDXComponent=!0}}]);