Skip to content

Commit 8afce13

Browse files
authored
Merge pull request #4 from preprio/develop
Update PHP Client (for PHP 7<)
2 parents 17aca6c + 796e94e commit 8afce13

File tree

3 files changed

+33
-100
lines changed

3 files changed

+33
-100
lines changed

README.md

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# Getting started with PHP
1+
# PHP + Prepr SDK
22

3-
This package is a wrapper for the <a href="https://prepr.dev">Prepr</a> REST API.
3+
This package is an SDK for the REST API.
44

55
## Basics
66
The SDK on [GitHub](https://github.com/preprio/php-sdk)
7-
Minimal PHP version: `> 5.6.4`
8-
Requires `GuzzleHttp 7.0.X`, `Murmurhash 2.0.X`
7+
Minimal PHP version: `> 5.6.4`
8+
Requires `GuzzleHttp 7.0.X`
9+
10+
For Laravel projects we recommend using the Laravel providers for [REST](https://github.com/preprio/laravel-rest-sdk) or [GraphQL](https://github.com/preprio/laravel-graphql-sdk).
911

1012
## Installation
1113

@@ -17,7 +19,7 @@ composer require preprio/php-sdk
1719

1820
## Making your first request
1921

20-
Let's start with getting all publications from your Prepr Environment.
22+
Let's start with getting some content items from your Prepr Environment.
2123

2224
```php
2325
<?php
@@ -27,7 +29,7 @@ use Preprio\Prepr;
2729
$apiRequest = new Prepr('{ACCESS_TOKEN}');
2830

2931
$apiRequest
30-
->path('publications')
32+
->path('content_items')
3133
->query([
3234
'fields' => 'items'
3335
])
@@ -40,7 +42,7 @@ if($apiRequest->getStatusCode() == 200) {
4042
```
4143

4244

43-
To get a single publication, pass the Id to the request.
45+
To get a single content item, pass the ID to the request.
4446

4547
```php
4648
<?php
@@ -50,7 +52,7 @@ use Preprio\Prepr;
5052
$apiRequest = new Prepr('{ACCESS_TOKEN}');
5153

5254
$apiRequest
53-
->path('publications/{id}', [
55+
->path('content_items/{id}', [
5456
'id' => '1236f0b1-b26d-4dde-b835-9e4e441a6d09'
5557
])
5658
->query([
@@ -64,61 +66,13 @@ if($apiRequest->getStatusCode() == 200) {
6466
}
6567
```
6668

67-
68-
## A/B testing with Optimize
69-
70-
To enable A/B testing you can pass a User ID to provide a consistent result.
71-
The A/B testing feature requires the use of the cached CDN API.
72-
73-
To switch off A/B testing, pass NULL to the UserId param.
74-
75-
```php
76-
$apiRequest = new Prepr('{ACCESS_TOKEN}', '{{YourCustomUserId}}');
77-
```
78-
79-
or per request
80-
81-
```php
82-
$apiRequest
83-
->path('publications/{id}',[
84-
'id' => 1
85-
]),
86-
->query([
87-
'fields' => 'example'
88-
])
89-
->userId(
90-
'{{YourCustomUserId}}'
91-
)
92-
->get();
93-
94-
if($apiRequest->getStatusCode() == 200) {
95-
96-
print_r($apiRequest->getResponse());
97-
}
98-
```
99-
100-
For more information check the [Optimize documentation](/docs/optimize/v1/introduction).
101-
102-
## Using the CDN
103-
104-
To use Prepr in production we advise you to use the API CDN for a fast response.
105-
Add the API CDN Url to the init of the SDK.
106-
107-
```php
108-
<?php
109-
110-
use Preprio\Prepr;
111-
112-
$apiRequest = new Prepr('{ACCESS_TOKEN}', null, 'https://cdn.prepr.io/');
113-
```
114-
11569
### Auto paging results
11670

11771
To get all resources for an endpoint, you can use the auto paging feature.
11872

11973
```php
12074
$apiRequest
121-
->path('publications')
75+
->path('content_items')
12276
->query([
12377
'limit' => 200 // optional
12478
])
@@ -138,7 +92,7 @@ The authorization can also be set for one specific request `->url('url')->author
13892

13993
```php
14094
$apiRequest
141-
->path('tags')
95+
->path('content_items')
14296
->params([
14397
'body' => 'Example'
14498
])
@@ -154,7 +108,7 @@ if($apiRequest->getStatusCode() == 201) {
154108

155109
```php
156110
$apiRequest
157-
->path('tags')
111+
->path('content_items')
158112
->params([
159113
'body' => 'Example'
160114
])
@@ -170,8 +124,8 @@ if($apiRequest->getStatusCode() == 200) {
170124

171125
```php
172126
$apiRequest
173-
->path('tags/{id}',[
174-
'id' => 1
127+
->path('content_items/{id}',[
128+
'id' => "398402d-dd-asd-ads3343dad"
175129
])
176130
->delete();
177131

@@ -189,7 +143,7 @@ $apiRequest
189143
->params([
190144
'body' => 'Example',
191145
])
192-
->file('/path/to/file.txt') // For laravel storage: storage_path('app/file.ext')
146+
->file('/path/to/file.txt')
193147
->post();
194148

195149
if($apiRequest->getStatusCode() == 200) {

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "preprio/php-sdk",
3-
"description": "Prepr API wrapper",
4-
"keywords": ["laravel", "prepr"],
3+
"description": "Prepr PHP SDK",
4+
"keywords": ["php", "prepr"],
55
"license": "MIT",
66
"authors": [
77
{
@@ -12,8 +12,7 @@
1212
"minimum-stability": "dev",
1313
"require": {
1414
"php": ">=5.6.4",
15-
"guzzlehttp/guzzle": "^7.0.1",
16-
"lastguest/murmurhash": "^2.0.0"
15+
"guzzlehttp/guzzle": "^7.0.1"
1716
},
1817
"autoload": {
1918
"psr-4": {

src/Prepr.php

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
namespace Preprio;
44

55
use GuzzleHttp\Client;
6-
use Cache;
7-
use Artisan;
8-
use lastguest\Murmur;
9-
use Session;
106

117
class Prepr
128
{
@@ -22,18 +18,14 @@ class Prepr
2218
protected $authorization;
2319
protected $file = null;
2420
protected $statusCode;
25-
protected $userId;
21+
protected $client;
2622

2723
private $chunkSize = 26214400;
2824

29-
public function __construct($authorization = null, $userId = null, $baseUrl = 'https://api.eu1.prepr.io/')
25+
public function __construct($authorization = null, $baseUrl = 'https://cdn.prepr.io/')
3026
{
3127
$this->baseUrl = $baseUrl;
3228
$this->authorization = $authorization;
33-
34-
if($userId) {
35-
$this->userId = $this->hashUserId($userId);
36-
}
3729
}
3830

3931
protected function client()
@@ -44,7 +36,6 @@ protected function client()
4436
'Accept' => 'application/json',
4537
'Content-Type' => 'application/json',
4638
'Authorization' => $this->authorization,
47-
'Prepr-ABTesting' => $this->userId
4839
]
4940
]);
5041
}
@@ -70,22 +61,23 @@ protected function request($options = [])
7061
$this->rawResponse = $this->request->getBody()->getContents();
7162
$this->response = json_decode($this->rawResponse, true);
7263

73-
// Files larger then 25 MB (upload chunked)
74-
if ($this->data_get($this->file, 'chunks') > 1 && ($this->getStatusCode() === 201 || $this->getStatusCode() === 200)) {
64+
// Files larger than 25 MB (upload chunked)
65+
if ($this->data_get($this->file, 'chunks') > 1 &&
66+
($this->getStatusCode() === 201 || $this->getStatusCode() === 200)) {
7567
return $this->processFileUpload();
7668
}
7769

7870
return $this;
7971
}
8072

81-
public function authorization($authorization)
73+
public function authorization(string $authorization)
8274
{
8375
$this->authorization = $authorization;
8476

8577
return $this;
8678
}
8779

88-
public function url($url)
80+
public function url(string $url)
8981
{
9082
$this->baseUrl = $url;
9183

@@ -168,10 +160,11 @@ public function getStatusCode()
168160
if($this->statusCode) {
169161
return $this->statusCode;
170162
}
163+
171164
return $this->request->getStatusCode();
172165
}
173166

174-
public function file($filepath)
167+
public function file(string $filepath)
175168
{
176169
$fileSize = filesize($filepath);
177170
$file = fopen($filepath, 'r');
@@ -186,13 +179,13 @@ public function file($filepath)
186179

187180
if ($this->file) {
188181

189-
// Files larger then 25 MB (upload chunked)
182+
// Files larger than 25 MB (upload chunked)
190183
if ($this->data_get($this->file, 'chunks') > 1) {
191184

192185
$this->params['upload_phase'] = 'start';
193186
$this->params['file_size'] = $this->data_get($this->file, 'size');
194187

195-
// Files smaller then 25 MB (upload directly)
188+
// Files smaller than 25 MB (upload directly)
196189
} else {
197190
$this->params['source'] = $this->data_get($this->file, 'file');
198191
}
@@ -293,26 +286,13 @@ public function autoPaging()
293286
'items' => $arrayItems,
294287
'total' => count($arrayItems)
295288
];
296-
$this->statusCode = 200;
297-
298-
return $this;
299-
}
300-
301-
public function hashUserId($userId)
302-
{
303-
$hashValue = Murmur::hash3_int($userId, 1);
304-
$ratio = $hashValue / pow(2, 32);
305-
return intval($ratio*10000);
306-
}
307289

308-
public function userId($userId)
309-
{
310-
$this->userId = $this->hashUserId($userId);
290+
$this->statusCode = 200;
311291

312292
return $this;
313293
}
314294

315-
public function nestedArrayToMultipart($array)
295+
public function nestedArrayToMultipart(array $array)
316296
{
317297
$flatten = function ($array, $original_key = '') use (&$flatten) {
318298
$output = [];
@@ -347,7 +327,7 @@ public function nestedArrayToMultipart($array)
347327
return $multipart;
348328
}
349329

350-
public function data_get($array, $variable)
330+
public function data_get(array $array, string $variable)
351331
{
352332
if(isset($array[$variable])) {
353333
return $array[$variable];

0 commit comments

Comments
 (0)