Skip to content

Commit

Permalink
added unnamed data and param passing - used by CLB API
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Dec 5, 2016
1 parent e57f421 commit cc46b4d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ However, for now (and for backward compatibility reasons) the class name stays t
## Cloudflare API version 4

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

## Example code
Expand Down Expand Up @@ -167,7 +167,7 @@ if __name__ == '__main__':

## Providing Cloudflare Username and API Key

When you create a _CloudFlare_ class you can pass up to four paramaters.
When you create a **CloudFlare** class you can pass up to four paramaters.

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

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

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.
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.
For example: ```item==value```.

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

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

Data or parameters can be either named or unnamed.
It can not be both.
Named is the majority format; as described above.
Unnamed parameters simply don't have anything before the **=** sign, as in ```=value```.
This format is presently only used by the Cloudflare Load Balancer API calls.
For example:

```
cli4 --put ="[ 00000000000000000000000000000000 ]" /user/load_balancers/maps/:00000000000000000000000000000000/region/WNAM
```

### CLI output

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).
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).

### Simple CLI examples

Expand Down
24 changes: 18 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Cloudflare API version 4

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

Example code
------------
Expand Down Expand Up @@ -187,7 +187,8 @@ A more complex example follows.
Providing Cloudflare Username and API Key
-----------------------------------------

When you create a *CloudFlare* class you can pass up to four paramaters.
When you create a **CloudFlare** class you can pass up to four
paramaters.

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

If you need a numeric value passed then *==* can be used to force the
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. For
example: ``item==value``.

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

::

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

Data or parameters can be either named or unnamed. It can not be both.
Named is the majority format; as described above. Unnamed parameters
simply don't have anything before the **=** sign, as in ``=value``. This
format is presently only used by the Cloudflare Load Balancer API calls.
For example:

::

cli4 --put ="[ 00000000000000000000000000000000 ]" /user/load_balancers/maps/:00000000000000000000000000000000/region/WNAM

CLI output
~~~~~~~~~~

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).
readable). This is controled by the **--yaml** or **--json** flags (JSON
is the default).

Simple CLI examples
~~~~~~~~~~~~~~~~~~~
Expand Down
22 changes: 18 additions & 4 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def cli4(args):
+ '[-V|--version] [-h|--help] [-v|--verbose] [-q|--quiet] [-j|--json] [-y|--yaml] '
+ '[-r|--raw] '
+ '[-d|--dump] '
+ '[--get|--patch|--post|-put|--delete] '
+ '[--get|--patch|--post|--put|--delete] '
+ '[item=value ...] '
+ '/command...')

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

# next grab the params. These are in the form of tag=value
params = {}
params = None
while len(args) > 0 and '=' in args[0]:
tag_string, value_string = args.pop(0).split('=', 1)
if value_string == 'true':
Expand All @@ -213,8 +213,22 @@ def cli4(args):
exit('cli4: %s="%s" - can\'t parse json value' % (tag_string, value_string))
else:
value = value_string
tag = tag_string
params[tag] = value
if tag_string == '':
# There's no tag; it's just an unnamed list
if params == None:
params = []
try:
params.append(value)
except AttributeError:
exit('cli4: %s=%s - param error. Can\'t mix unnamed and named list' % (tag_string, value_string))
else:
if params == None:
params = {}
tag = tag_string
try:
params[tag] = value
except TypeError:
exit('cli4: %s=%s - param error. Can\'t mix unnamed and named list' % (tag_string, value_string))

if dump:
cf = CloudFlare.CloudFlare()
Expand Down

0 comments on commit cc46b4d

Please sign in to comment.