Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit cc46b4d

Browse files
committed
added unnamed data and param passing - used by CLB API
1 parent e57f421 commit cc46b4d

3 files changed

Lines changed: 52 additions & 15 deletions

File tree

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ However, for now (and for backward compatibility reasons) the class name stays t
3535
## Cloudflare API version 4
3636

3737
The Cloudflare API can be found [here](https://api.cloudflare.com/).
38-
Each API call is provided via a similarly named function within the _CloudFlare_ class.
38+
Each API call is provided via a similarly named function within the **CloudFlare** class.
3939
A full list is provided below.
4040

4141
## Example code
@@ -167,7 +167,7 @@ if __name__ == '__main__':
167167

168168
## Providing Cloudflare Username and API Key
169169

170-
When you create a _CloudFlare_ class you can pass up to four paramaters.
170+
When you create a **CloudFlare** class you can pass up to four paramaters.
171171

172172
* Account email
173173
* Account API key
@@ -412,10 +412,10 @@ For API calls that need to pass data or parameters there is various formats to u
412412

413413
The simplest form is ```item=value```. This passes the value as a string within the APIs JSON data.
414414

415-
If you need a numeric value passed then _==_ can be used to force the value to be treated as a numeric value within the APIs JSON data.
415+
If you need a numeric value passed then **==** can be used to force the value to be treated as a numeric value within the APIs JSON data.
416416
For example: ```item==value```.
417417

418-
if you need to pass a list of items; then _[]_ can be used. For example:
418+
if you need to pass a list of items; then **[]** can be used. For example:
419419

420420
```
421421
pool_id1="11111111111111111111111111111111"
@@ -424,9 +424,20 @@ pool_id3="33333333333333333333333333333333"
424424
cli4 --post global_pools="[ ${pool_id1}, ${pool_id2}, ${pool_id3} ]" region_pools="[ ]" /user/load_balancers/maps
425425
```
426426

427+
Data or parameters can be either named or unnamed.
428+
It can not be both.
429+
Named is the majority format; as described above.
430+
Unnamed parameters simply don't have anything before the **=** sign, as in ```=value```.
431+
This format is presently only used by the Cloudflare Load Balancer API calls.
432+
For example:
433+
434+
```
435+
cli4 --put ="[ 00000000000000000000000000000000 ]" /user/load_balancers/maps/:00000000000000000000000000000000/region/WNAM
436+
```
437+
427438
### CLI output
428439

429-
The output from the CLI command is in JSON or YAML format (and human readable). This is controled by the _--yaml_ or _--json_ flags (JSON is the default).
440+
The output from the CLI command is in JSON or YAML format (and human readable). This is controled by the **--yaml** or **--json** flags (JSON is the default).
430441

431442
### Simple CLI examples
432443

README.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Cloudflare API version 4
4545

4646
The Cloudflare API can be found `here <https://api.cloudflare.com/>`__.
4747
Each API call is provided via a similarly named function within the
48-
*CloudFlare* class. A full list is provided below.
48+
**CloudFlare** class. A full list is provided below.
4949

5050
Example code
5151
------------
@@ -187,7 +187,8 @@ A more complex example follows.
187187
Providing Cloudflare Username and API Key
188188
-----------------------------------------
189189

190-
When you create a *CloudFlare* class you can pass up to four paramaters.
190+
When you create a **CloudFlare** class you can pass up to four
191+
paramaters.
191192

192193
- Account email
193194
- Account API key
@@ -459,11 +460,12 @@ formats to use.
459460
The simplest form is ``item=value``. This passes the value as a string
460461
within the APIs JSON data.
461462

462-
If you need a numeric value passed then *==* can be used to force the
463+
If you need a numeric value passed then **==** can be used to force the
463464
value to be treated as a numeric value within the APIs JSON data. For
464465
example: ``item==value``.
465466

466-
if you need to pass a list of items; then *[]* can be used. For example:
467+
if you need to pass a list of items; then **[]** can be used. For
468+
example:
467469

468470
::
469471

@@ -472,12 +474,22 @@ if you need to pass a list of items; then *[]* can be used. For example:
472474
pool_id3="33333333333333333333333333333333"
473475
cli4 --post global_pools="[ ${pool_id1}, ${pool_id2}, ${pool_id3} ]" region_pools="[ ]" /user/load_balancers/maps
474476

477+
Data or parameters can be either named or unnamed. It can not be both.
478+
Named is the majority format; as described above. Unnamed parameters
479+
simply don't have anything before the **=** sign, as in ``=value``. This
480+
format is presently only used by the Cloudflare Load Balancer API calls.
481+
For example:
482+
483+
::
484+
485+
cli4 --put ="[ 00000000000000000000000000000000 ]" /user/load_balancers/maps/:00000000000000000000000000000000/region/WNAM
486+
475487
CLI output
476488
~~~~~~~~~~
477489

478490
The output from the CLI command is in JSON or YAML format (and human
479-
readable). This is controled by the *--yaml* or *--json* flags (JSON is
480-
the default).
491+
readable). This is controled by the **--yaml** or **--json** flags (JSON
492+
is the default).
481493

482494
Simple CLI examples
483495
~~~~~~~~~~~~~~~~~~~

cli4/cli4.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def cli4(args):
146146
+ '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] '
147147
+ '[-r|--raw] '
148148
+ '[-d|--dump] '
149-
+ '[--get|--patch|--post|-put|--delete] '
149+
+ '[--get|--patch|--post|--put|--delete] '
150150
+ '[item=value ...] '
151151
+ '/command...')
152152

@@ -193,7 +193,7 @@ def cli4(args):
193193
digits_only = re.compile('^[0-9]+$')
194194

195195
# next grab the params. These are in the form of tag=value
196-
params = {}
196+
params = None
197197
while len(args) > 0 and '=' in args[0]:
198198
tag_string, value_string = args.pop(0).split('=', 1)
199199
if value_string == 'true':
@@ -213,8 +213,22 @@ def cli4(args):
213213
exit('cli4: %s="%s" - can\'t parse json value' % (tag_string, value_string))
214214
else:
215215
value = value_string
216-
tag = tag_string
217-
params[tag] = value
216+
if tag_string == '':
217+
# There's no tag; it's just an unnamed list
218+
if params == None:
219+
params = []
220+
try:
221+
params.append(value)
222+
except AttributeError:
223+
exit('cli4: %s=%s - param error. Can\'t mix unnamed and named list' % (tag_string, value_string))
224+
else:
225+
if params == None:
226+
params = {}
227+
tag = tag_string
228+
try:
229+
params[tag] = value
230+
except TypeError:
231+
exit('cli4: %s=%s - param error. Can\'t mix unnamed and named list' % (tag_string, value_string))
218232

219233
if dump:
220234
cf = CloudFlare.CloudFlare()

0 commit comments

Comments
 (0)