-
Notifications
You must be signed in to change notification settings - Fork 53
Offer createKey() to register a new API key. #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rolftimmermans
wants to merge
24
commits into
master
Choose a base branch
from
create-key
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−23
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
78c31da
Offer createKey() to register a new API key.
rolftimmermans d8abbc0
Reuse anonymous client.
rolftimmermans e7d975e
Make method public.
rolftimmermans 94280d1
Use self. [ci skip]
rolftimmermans 769d98a
Make status public.
rolftimmermans 1befa82
Alternate approach for anonymous clients.
rolftimmermans c369f9f
Make options protected.
rolftimmermans 8c1a4f1
Merge branch 'master' into create-key
fc4cc1e
Merge proxy support.
rolftimmermans f12565c
Merge branch 'master' into create-key
rolftimmermans fe7e745
Merge branch 'master' into create-key
rolftimmermans 45e79fe
Merge retry logic.
rolftimmermans 7c76bcc
Merge checks for curl/openssl.
rolftimmermans ce0b3fe
Change ordering and naming.
rolftimmermans 1a927ac
Merge branch 'master' into create-key
mattijsvandruenen c6b617c
Use keys endpoint for key validation, and added retrieving remaining …
mattijsvandruenen b21ccb5
Use assertSame instead so that tests also succeed on PHP 7.2.
mattijsvandruenen cb76e97
Lets indeed not introduce extra alias getters for the new information…
mattijsvandruenen cd15c07
Merge branch 'master' into create-key
58652f9
Updated request func not to overwrite request body and created test.
yektaturan 7a005a9
Merge branch 'master' of github.com:wcreateweb/tinify-php into create…
tijmenbruggeman d71421e
Remove setup
tijmenbruggeman a2117ce
Remove status check
tijmenbruggeman 093e57b
Merge pull request #38 from wcreateweb/create-key
rkoopmans File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,18 +5,35 @@ | |
| const VERSION = "1.6.2"; | ||
|
|
||
| class Tinify { | ||
| const AUTHENTICATED = true; | ||
| const ANONYMOUS = false; | ||
|
|
||
| private static $key = NULL; | ||
| private static $appIdentifier = NULL; | ||
| private static $proxy = NULL; | ||
|
|
||
| private static $compressionCount = NULL; | ||
| private static $remainingCredits = NULL; | ||
| private static $payingState = NULL; | ||
| private static $emailAddress = NULL; | ||
|
|
||
| private static $client = NULL; | ||
|
|
||
| public static function setKey($key) { | ||
| self::$key = $key; | ||
| self::$client = NULL; | ||
| } | ||
|
|
||
| public static function getKey() { | ||
| return self::$key; | ||
| } | ||
|
|
||
| public static function createKey($email, $options) { | ||
| $body = array_merge(array("email" => $email), $options); | ||
| $response = self::getClient(self::ANONYMOUS)->request("post", "/keys", $body); | ||
| self::setKey($response->body->key); | ||
| } | ||
|
|
||
| public static function setAppIdentifier($appIdentifier) { | ||
| self::$appIdentifier = $appIdentifier; | ||
| self::$client = NULL; | ||
|
|
@@ -35,8 +52,32 @@ public static function setCompressionCount($compressionCount) { | |
| self::$compressionCount = $compressionCount; | ||
| } | ||
|
|
||
| public static function getClient() { | ||
| if (!self::$key) { | ||
| public static function getRemainingCredits() { | ||
| return self::$remainingCredits; | ||
| } | ||
|
|
||
| public static function setRemainingCredits($remainingCredits) { | ||
| self::$remainingCredits = $remainingCredits; | ||
| } | ||
|
|
||
| public static function getPayingState() { | ||
| return self::$payingState; | ||
| } | ||
|
|
||
| public static function setPayingState($payingState) { | ||
| self::$payingState = $payingState; | ||
| } | ||
|
|
||
| public static function getEmailAddress() { | ||
| return self::$emailAddress; | ||
| } | ||
|
|
||
| public static function setEmailAddress($emailAddress) { | ||
| self::$emailAddress = $emailAddress; | ||
| } | ||
|
|
||
| public static function getClient($mode = self::AUTHENTICATED) { | ||
| if ($mode == self::AUTHENTICATED && !self::$key) { | ||
| throw new AccountException("Provide an API key with Tinify\setKey(...)"); | ||
| } | ||
|
|
||
|
|
@@ -56,6 +97,14 @@ function setKey($key) { | |
| return Tinify::setKey($key); | ||
| } | ||
|
|
||
| function getKey() { | ||
| return Tinify::getKey(); | ||
| } | ||
|
|
||
| function createKey($email, $options) { | ||
| return Tinify::createKey($email, $options); | ||
| } | ||
|
|
||
| function setAppIdentifier($appIdentifier) { | ||
| return Tinify::setAppIdentifier($appIdentifier); | ||
| } | ||
|
|
@@ -72,6 +121,18 @@ function compressionCount() { | |
| return Tinify::getCompressionCount(); | ||
| } | ||
|
|
||
| function remainingCredits() { | ||
| return Tinify::getRemainingCredits(); | ||
| } | ||
|
|
||
| function payingState() { | ||
| return Tinify::getPayingState(); | ||
| } | ||
|
|
||
| function emailAddress() { | ||
| return Tinify::getEmailAddress(); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These methods are very strange. They looks similar. The duplicate should be removed. |
||
| function fromFile($path) { | ||
| return Source::fromFile($path); | ||
| } | ||
|
|
@@ -86,7 +147,8 @@ function fromUrl($string) { | |
|
|
||
| function validate() { | ||
| try { | ||
| Tinify::getClient()->request("post", "/shrink"); | ||
| Tinify::getClient()->request("get", "/keys/" . Tinify::getKey()); | ||
| return true; | ||
| } catch (AccountException $err) { | ||
| if ($err->status == 429) return true; | ||
| throw $err; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php | ||
|
|
||
| class TinifyExceptionTest extends TestCase { | ||
| public function testStatusShouldReturnStatusIfSet() { | ||
| $err = new Tinify\Exception("Message", "Error", 401); | ||
| $this->assertSame(401, $err->status); | ||
| } | ||
|
|
||
| public function testStatusShouldReturnNullIfUnset() { | ||
| $err = new Tinify\Exception("Message", "Error"); | ||
| $this->assertSame(null, $err->status); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should use
PHP_CodeSnifferorphp-cs-fixerto fixPSR-2coding style.