Skip to content

Commit

Permalink
Fix not working api examples.
Browse files Browse the repository at this point in the history
- support token and basic authentication in packages api
  • Loading branch information
vtsykun committed Nov 12, 2019
1 parent 55a17e4 commit 2cc0a12
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 86 deletions.
2 changes: 1 addition & 1 deletion app/config/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ security:

firewalls:
packages:
pattern: (^(/packages.json$|/p/|/zipball/|/feeds/.+(\.rss|\.atom)|/downloads/|/api/))+
pattern: (^(/packages.json$|/p/|/zipball/|/feeds/.+(\.rss|\.atom)|/packages/[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+?\.json|/packages/list\.json|/downloads/|/api/))+
api_basic: true

main:
Expand Down
19 changes: 18 additions & 1 deletion src/Packagist/WebBundle/Controller/ApiDocController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Packagist\WebBundle\Controller;

use Packagist\WebBundle\Entity\Version;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

Expand All @@ -26,6 +27,22 @@ class ApiDocController extends Controller
*/
public function indexAction()
{
return array();
$qb = $this->getDoctrine()->getRepository(Version::class)
->createQueryBuilder('v');

$qb->select(['v.name'])
->groupBy('v.name')
->orderBy('COUNT(v.id)', 'DESC')
->setMaxResults(1);

try {
$examplePackage = $qb->getQuery()->getSingleScalarResult();
} catch (\Exception $exception) {
$examplePackage = 'monolog/monolog';
}

return [
'examplePackage' => $examplePackage
];
}
}
95 changes: 11 additions & 84 deletions src/Packagist/WebBundle/Resources/views/ApiDoc/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
</li>
<li><a href="#get-package-data">{{ 'api_doc.get_package_data'|trans }}</a></li>
</ul>

{% set apiToken = app.user.username ~ ':' ~ app.user.apiToken %}

<section class="col-d-12">
<h3 id="list-packages">{{ 'api_doc.listing_names'|trans }}</h3>
<h4 id="list-packages-all">{{ 'api_doc.all_packages'|trans }}</h4>
<pre>
GET https://{{ packagist_host }}/packages/list.json
GET https://{{ packagist_host }}/packages/list.json?token={{ apiToken }}
<code>
{
"packageNames": [
Expand All @@ -35,11 +35,11 @@ GET https://{{ packagist_host }}/packages/list.json
]
}
</code></pre>
<p>Working example: <code><a href="https://{{ packagist_host }}/packages/list.json">https://{{ packagist_host }}/packages/list.json</a></code></p>
<p>Working example: <code><a href="https://{{ packagist_host }}/packages/list.json?token={{ apiToken }}">https://{{ packagist_host }}/packages/list.json?token={{ apiToken }}</a></code></p>

<h4 id="list-packages-by-organization">{{ 'api_doc.list_by_organization'|trans }}</h4>
<pre>
GET https://{{ packagist_host }}/packages/list.json?vendor=[vendor]
GET https://{{ packagist_host }}/packages/list.json?vendor=[vendor]&token={{ apiToken }}
<code>
{
"packageNames": [
Expand All @@ -48,11 +48,11 @@ GET https://{{ packagist_host }}/packages/list.json?vendor=[vendor]
]
}
</code></pre>
<p>Working example: <code><a href="https://{{ packagist_host }}/packages/list.json?vendor=composer">https://{{ packagist_host }}/packages/list.json?vendor=composer</a></code></p>
<p>Working example: <code><a href="https://{{ packagist_host }}/packages/list.json?vendor=composer&token={{ apiToken }}">https://{{ packagist_host }}/packages/list.json?vendor=composer&token={{ apiToken }}</a></code></p>

<h4 id="list-packages-by-type">{{ 'api_doc.list_by_type'|trans }}</h4>
<pre>
GET https://{{ packagist_host }}/packages/list.json?type=[type]
GET https://{{ packagist_host }}/packages/list.json?type=[type]&token={{ apiToken }}
<code>
{
"packageNames": [
Expand All @@ -61,83 +61,11 @@ GET https://{{ packagist_host }}/packages/list.json?type=[type]
]
}
</code></pre>
<p>Working example: <code><a href="https://{{ packagist_host }}/packages/list.json?type=composer-plugin">https://{{ packagist_host }}/packages/list.json?type=composer-plugin</a></code></p>
<p>Working example: <code><a href="https://{{ packagist_host }}/packages/list.json?type=composer-plugin&token={{ apiToken }}">https://{{ packagist_host }}/packages/list.json?type=composer-plugin&token={{ apiToken }}</a></code></p>

</section>


<section class="col-d-12">
<h3 id="search-packages">{{ 'api_doc.searching'|trans }}</h3>

<p>Search results are paginated and you can change the pagination step by using the per_page parameter. For example <code>https://{{ packagist_host }}/search.json?q=[query]&amp;per_page=5</code></p>

<h4 id="search-packages-by-name">{{ 'api_doc.search_by_name'|trans }}</h4>
<pre>
GET https://{{ packagist_host }}/search.json?q=[query]
<code>
{
"results" : [
{
"name": "[vendor]/[package]",
"description": "[description]",
"url": "https://{{ packagist_host }}/packages/[vendor]/[package]",
"repository": [repository url],
"downloads": [number of downloads],
"favers": [number of favers]
},
...
],
"total": [number of results],
"next": "https://{{ packagist_host }}/search.json?q=[query]&amp;page=[next page number]"
}
</code></pre>
<p>Working example: <code><a href="https://{{ packagist_host }}/search.json?q=monolog">https://{{ packagist_host }}/search.json?q=monolog</a></code></p>

<h4 id="search-packages-by-tag">{{ 'api_doc.search_by_tag'|trans }}</h4>
<pre>
GET https://{{ packagist_host }}/search.json?tags=[tag]
<code>
{
"results": [
{
"name": "[vendor]/[package]",
"description": "[description]",
"url": "https://{{ packagist_host }}/packages/[vendor]/[package]",
"repository": "[repository url]",
"downloads": [number of downloads],
"favers": [number of favers]
}
...
],
"total": [numbers of results]
}
</code></pre>
<p>Working example: <code><a href="https://{{ packagist_host }}/search.json?q=monolog&amp;tags=psr-3">https://{{ packagist_host }}/search.json?q=monolog&amp;tags=psr-3</a></code></p>

<h4 id="search-packages-by-type">{{ 'api_doc.search_by_type'|trans }}</h4>
<pre>
GET https://{{ packagist_host }}/search.json?q=[query]&amp;type=symfony-bundle
<code>
{
"results" : [
{
"name": "[vendor]/[package]",
"description": "[description]",
"url": "https://{{ packagist_host }}/packages/[vendor]/[package]",
"repository": [repository url],
"downloads": [number of downloads],
"favers": [number of favers]
},
...
],
"total": [number of results],
"next": "https://{{ packagist_host }}/search.json?q=[query]&amp;page=[next page number]"
}
</code></pre>
<p>Working example: <code><a href="https://{{ packagist_host }}/search.json?q=monolog&amp;type=symfony-bundle">https://{{ packagist_host }}/search.json?q=monolog&amp;type=symfony-bundle</a></code></p>
</section>


<section class="col-d-12">
<h3 id="get-package-data">{{ 'api_doc.get_package_data'|trans }}</h3>

Expand All @@ -156,7 +84,7 @@ GET https://{{ packagist_host }}/search.json?q=[query]&amp;type=symfony-bundle
</p>

<pre>
GET https://{{ packagist_host }}/p/[vendor]/[package].json
GET https://{{ packagist_host }}/p/[vendor]/[package].json?token={{ apiToken }}
<code>
{
"packages": {
Expand All @@ -174,14 +102,14 @@ GET https://{{ packagist_host }}/p/[vendor]/[package].json
}
}
</code></pre>
<p>Working example: <code><a href="https://{{ packagist_host }}/p/monolog/monolog.json">https://{{ packagist_host }}/p/monolog/monolog.json</a></code></p>
<p>Working example: <code><a href="https://{{ packagist_host }}/p/{{ examplePackage }}.json?token={{ apiToken }}">https://{{ packagist_host }}/p/{{ examplePackage }}.json?token={{ apiToken }}</a></code></p>

<h4 id="get-package-by-name">Using the API</h4>

<p>The JSON API for packages gives you all the infos we have including downloads, dependents count, github info, etc. However it is generated dynamically so for performance reason we cache the responses for twelve hours. As such if the static file endpoint described above is enough please use it instead.</p>

<pre>
GET https://{{ packagist_host }}/packages/[vendor]/[package].json
GET https://{{ packagist_host }}/packages/[vendor]/[package].json?token={{ apiToken }}
<code>
{
"package": {
Expand All @@ -201,9 +129,8 @@ GET https://{{ packagist_host }}/packages/[vendor]/[package].json
}
}
</code></pre>
<p>Working example: <code><a href="https://{{ packagist_host }}/packages/monolog/monolog.json">https://{{ packagist_host }}/packages/monolog/monolog.json</a></code></p>
<p>Working example: <code><a href="https://{{ packagist_host }}/packages/{{ examplePackage }}.json?token={{ apiToken }}">https://{{ packagist_host }}/packages/{{ examplePackage }}.json?token={{ apiToken }}</a></code></p>

</section>


{% endblock %}

0 comments on commit 2cc0a12

Please sign in to comment.