Skip to content

Commit fefe329

Browse files
committed
Add a showContents() method to Contents API
1 parent 10a728f commit fefe329

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/Github/Api/Repository/Contents.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,33 @@ public function archive($username, $repository, $format, $reference = null)
7272
'ref' => $reference
7373
));
7474
}
75+
76+
/**
77+
* Get the contents of a file in a repository
78+
*
79+
* @param string $username the user who owns the repository
80+
* @param string $repository the name of the repository
81+
* @param string $path path to file
82+
* @param string $reference reference to a branch or commit
83+
*
84+
* @return string content of file
85+
*/
86+
public function showContents($username, $repository, $path, $reference = null)
87+
{
88+
$file = $this->show($username, $repository, $path, $reference);
89+
90+
if (!isset($file['type']) || 'file' !== $file['type']) {
91+
throw new \Exception(sprintf('Path "%s" is not a file.', $path));
92+
}
93+
94+
if (!isset($file['content'])) {
95+
throw new \Exception(sprintf('Unable to access "content" for file "%s" (possible keys: "%s").', $path, implode(', ', array_keys($file))));
96+
}
97+
98+
if (!isset($file['encoding']) || 'base64' !== $file['encoding']) {
99+
throw new \Exception(sprintf('Encoding of file "%s" is not supported.', $path));
100+
}
101+
102+
return base64_decode($file['content']);
103+
}
75104
}

0 commit comments

Comments
 (0)