HTTPie: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
(Created page with "==References== {| | valign="top" | * Homebrew * JQ Tool * LuaJIT * Wrk | valign="top" | | valign="top" | |- | colspan="3" | ---- |- | valign="top" | | vali...")
 
No edit summary
 
(28 intermediate revisions by the same user not shown)
Line 1: Line 1:
choco install httpie
brew  install httpie
snap  install httpie
<syntaxhighlight lang="bash">
curl -fsSL https://packages.httpie.io/deb/KEY.gpg\
| sudo tee /etc/apt/keyrings/httpie.asc >/dev/null
cat << SRC | sudo tee /etc/apt/sources.list.d/httpie.list >/dev/null
deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/httpie.asc] https://packages.httpie.io/deb ./
SRC
echo 'apt-get update;echo;apt list -a --upgradable;apt-get install -y httpie' | sudo bash
httpie --version
</syntaxhighlight>
==Playground==
{|
| valign="top" |
<syntaxhighlight lang="bash">
# post request body
time http 127.0.0.1:1983/api/v1/collections\
x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
Content-Type:application/json\
<<< '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'
</syntaxhighlight>
| valign="top" |
<syntaxhighlight lang="bash">
# post request body
time http 127.0.0.1:1983/api/v1/collections\
x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
Content-Type:application/json\
--raw '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'
</syntaxhighlight>
|-
| colspan="3" |
----
|-
| valign="top" |
<syntaxhighlight lang="bash">
# put request body
time http PUT 127.0.0.1:1983/api/v1/collections/1000\
x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
Content-Type:application/json\
<<< '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'
</syntaxhighlight>
| valign="top" |
<syntaxhighlight lang="bash">
# put request body
time http PUT 127.0.0.1:1983/api/v1/collections/1000\
x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
Content-Type:application/json\
--raw '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'
</syntaxhighlight>
|-
| colspan="3" |
----
|-
| valign="top" |
<syntaxhighlight lang="bash">
# show response body
time http 127.0.0.1:1983/api/v1/collections\
x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
Content-Type:application/json\
-b\
<<< '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'
</syntaxhighlight>
| valign="top" |
<syntaxhighlight lang="bash">
# show response body
time http 127.0.0.1:1983/api/v1/collections\
x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
Content-Type:application/json\
-b\
--raw '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'
</syntaxhighlight>
|-
| colspan="3" |
----
|-
| valign="top" |
time http 127.0.0.1:1983/api/v1/collections/gender/M
time http 127.0.0.1:1983/api/v1/collections/gender
time http 127.0.0.1:1983/api/v1/collections
time http 127.0.0.1:1983/api/v1/collections/-/ar_SA
time http 127.0.0.1:1983/api/v1/collections/-/bn_BD
time http 127.0.0.1:1983/api/v1/collections/-/en_US
http https://cdn.chorke.org/exec/cli/
http --version
http --help
| valign="top" |
|}
==Mock API==
<syntaxhighlight lang="bash">
python3 -m venv ~/.venv/mockapi --prompt="MockApi"
source ~/.venv/mockapi/bin/activate
pip install Flask
</syntaxhighlight>
<syntaxhighlight lang="python">
          rm rf ~/Documents/flask-playground/MockApi/*.py
cat << EOF | tee ~/Documents/flask-playground/MockApi/__main__.py >/dev/null
from flask import Flask, request, json
import time, os
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
def load_collections():
    jsonurl = os.path.join(basedir, 'collections-v1.json')
    return json.load(open(jsonurl))
@app.route("/api/v1/collections", methods=['GET'])
def find_all():
    time.sleep(1)
    return load_collections()
@app.route("/api/v1/collections/<string:group>", methods=['GET'])
def find_by_group(group):
    collections = load_collections()
    fetch_group = collections[group]
    return fetch_group
@app.route("/api/v1/collections/<string:group>/<string:code>", methods=['GET'])
def find_by_group_and_code(group, code):
    collections = load_collections()
    fetch_group = collections[group]
    fetch_monad = next(filter(lambda monad: (monad['code']==code), fetch_group), None)
    return fetch_monad
@app.route("/api/v1/collections/-/<string:locale>", methods=['GET'])
def find_by_locale(locale):
    collections = load_collections()
    group_names = list(collections.keys())
    locale_data = dict(map(lambda n:
        (n, list(map(lambda c: {
                'domain':c['domain'],
                'group' :c['group'], 'code':c['code'],
                'name'  :c['extendedProperties']['locale'][locale]
            },
            collections[n]))
        ), group_names))
    return locale_data
@app.route("/api/v1/collections", methods=['POST'])
def create():
    return request.json
@app.route("/api/v1/collections/<int:id>", methods=['PUT'])
def update_by_id(id):
    return request.json
@app.route("/api/v1/collections/<int:id>", methods=['DELETE'])
def delete_by_id(id):
    return request.json
if __name__ == "__main__":
    app.run(host='127.0.0.1', port=1983, debug=True)
EOF
          python ~/Documents/flask-playground/MockApi
</syntaxhighlight>
==Seed Data==
<syntaxhighlight lang="bash">
time cat <<< '{
  "gender":[
    {
      "domain":"chorke.org", "group":"gender", "code":"M", "name":"Male",
      "properties":{}, "extendedProperties":{
        "code":{
          "ISO":1,
          "SCTID":"446151000124109"
        },
        "locale":{
          "ar_SA":"ذكر",
          "en_US":"Male",
          "bn_BD":"পুরুষ",
          "he_IL":"זָכָר",
          "ms_MY":"Jantan"
        }
      }
    },
    {
      "domain":"chorke.org", "group":"gender", "code":"F", "name":"Female",
      "properties":{}, "extendedProperties":{
        "code":{
          "ISO":2,
          "SCTID":"446141000124107"
        },
        "locale":{
          "ar_SA":"أنثى",
          "en_US":"Female",
          "bn_BD":"মহিলা",
          "he_IL":"נְקֵבָה",
          "ms_MY":"Perempuan"
        }
      }
    },
    {
      "domain":"chorke.org", "group":"gender", "code":"U", "name":"Unknown",
      "properties":{}, "extendedProperties":{
        "code":{
          "ISO":0,
          "SCTID":"UNK"
        },
        "locale":{
          "ar_SA":"مجهول",
          "bn_BD":"অজানা",
          "en_US":"Unknown",
          "he_IL":"לא ידוע",
          "ms_MY":"Tidak Diketahui"
        }
      }
    },
    {
      "domain":"chorke.org", "group":"gender", "code":"N", "name":"Not Applicable",
      "properties":{}, "extendedProperties":{
        "code":{
          "ISO":9,
          "SCTID":"33791000087105"
        },
        "locale":{
          "ar_SA":"غير قابل للتطبيق",
          "bn_BD":"প্রযোজ্য নয়",
          "en_US":"Not Applicable",
          "he_IL":"לא ישים",
          "ms_MY":"Tidak Berkaitan"
        }
      }
    }
  ]
}'| tee ~/Documents/flask-playground/MockApi/collections-v1.json >/dev/null
</syntaxhighlight>
==References==
==References==
{|
{|
| valign="top" |
| valign="top" |
* [[Homebrew]]
* [https://httpie.io/docs/cli/what-parts-of-the-http-exchange-should-be-printed HTTPie » HTTP Exchange Print Format]
* [[JQ Tool]]
* [https://httpie.io/docs/cli/request-data-via---raw HTTPie » Request data via <code>--raw</code>]
* [[LuaJIT]]
* [https://httpie.io/docs/cli/raw-request-body HTTPie » Raw Request Body]
* [[Wrk]]
* [https://httpie.io/docs/cli/advanced-usage HTTPie » Advanced usage]
* [https://httpie.io/docs/cli/output-options HTTPie » Output options]
* [https://httpie.io/docs/cli/bearer-auth HTTPie » Bearer auth]
* [https://httpie.io/docs/cli/basic-auth HTTPie » Basic auth]
* [https://httpie.io/docs/cli/universal HTTPie » CLI docs]
* [https://httpie.io/docs/cli/netrc  HTTPie » NetRC]
* [https://httpie.io/ HTTPie]


| valign="top" |
| valign="top" |
 
* [https://httpie.io/docs/cli/extra-verbose-output HTTPie » Extra Verbose Output]
* [https://httpie.io/docs/cli/follow-location HTTPie » Follow <code>Location</code>]
* [https://httpie.io/docs/cli/file-upload-forms  HTTPie » File upload forms]
* [https://httpie.io/docs/cli/verbose-output HTTPie » Verbose Output]
* [https://httpie.io/docs/cli/regular-forms HTTPie » Regular forms]
* [https://httpie.io/docs/cli/quiet-output HTTPie » Quite Output]
| valign="top" |
| valign="top" |


Line 16: Line 306:
|-
|-
| valign="top" |
| valign="top" |
* [[TensorFlow]]
* [[Homebrew]]
* [[ZA Proxy]]
* [[Cypress]]
* [https://gatling.io/ Gatling]
* [[JQ Tool]]
* [[Locust]]
* [[JMeter]]
* [[LuaJIT]]
* [[Wrk]]


| valign="top" |
| valign="top" |
* [[Locale]]
* [[CURL]]
* [[Flask]]


| valign="top" |
| valign="top" |


|}
|}

Latest revision as of 03:25, 20 October 2024

choco install httpie
brew  install httpie
snap  install httpie
curl -fsSL https://packages.httpie.io/deb/KEY.gpg\
 | sudo tee /etc/apt/keyrings/httpie.asc >/dev/null

cat << SRC | sudo tee /etc/apt/sources.list.d/httpie.list >/dev/null
deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/httpie.asc] https://packages.httpie.io/deb ./
SRC

echo 'apt-get update;echo;apt list -a --upgradable;apt-get install -y httpie' | sudo bash
httpie --version

Playground

# post request body
time http 127.0.0.1:1983/api/v1/collections\
 x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
 Content-Type:application/json\
 <<< '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'
# post request body
time http 127.0.0.1:1983/api/v1/collections\
 x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
 Content-Type:application/json\
 --raw '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'

# put request body
time http PUT 127.0.0.1:1983/api/v1/collections/1000\
 x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
 Content-Type:application/json\
 <<< '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'
# put request body
time http PUT 127.0.0.1:1983/api/v1/collections/1000\
 x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
 Content-Type:application/json\
 --raw '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'

# show response body
time http 127.0.0.1:1983/api/v1/collections\
 x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
 Content-Type:application/json\
 -b\
 <<< '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'
# show response body
time http 127.0.0.1:1983/api/v1/collections\
 x-auth-key:aeda11fd-14ea-44d0-aff6-e94bf6b78c1b\
 Content-Type:application/json\
 -b\
 --raw '{
    "code": "M",
    "name": "Male",
    "group": "gender",
    "properties": {},
    "extendedProperties": {}
}'

time http 127.0.0.1:1983/api/v1/collections/gender/M
time http 127.0.0.1:1983/api/v1/collections/gender
time http 127.0.0.1:1983/api/v1/collections
time http 127.0.0.1:1983/api/v1/collections/-/ar_SA
time http 127.0.0.1:1983/api/v1/collections/-/bn_BD
time http 127.0.0.1:1983/api/v1/collections/-/en_US
http https://cdn.chorke.org/exec/cli/
http --version
http --help

Mock API

python3 -m venv ~/.venv/mockapi --prompt="MockApi"
source ~/.venv/mockapi/bin/activate
pip install Flask
           rm rf ~/Documents/flask-playground/MockApi/*.py
cat << EOF | tee ~/Documents/flask-playground/MockApi/__main__.py >/dev/null
from flask import Flask, request, json
import time, os

app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))

def load_collections():
    jsonurl = os.path.join(basedir, 'collections-v1.json')
    return json.load(open(jsonurl))

@app.route("/api/v1/collections", methods=['GET'])
def find_all():
    time.sleep(1)
    return load_collections()

@app.route("/api/v1/collections/<string:group>", methods=['GET'])
def find_by_group(group):
    collections = load_collections()
    fetch_group = collections[group]
    return fetch_group

@app.route("/api/v1/collections/<string:group>/<string:code>", methods=['GET'])
def find_by_group_and_code(group, code):
    collections = load_collections()
    fetch_group = collections[group]
    fetch_monad = next(filter(lambda monad: (monad['code']==code), fetch_group), None)
    return fetch_monad

@app.route("/api/v1/collections/-/<string:locale>", methods=['GET'])
def find_by_locale(locale):
    collections = load_collections()
    group_names = list(collections.keys())
    locale_data = dict(map(lambda n: 
        (n, list(map(lambda c: {
                'domain':c['domain'],
                'group' :c['group'], 'code':c['code'],
                'name'  :c['extendedProperties']['locale'][locale]
            },
            collections[n]))
        ), group_names))
    return locale_data

@app.route("/api/v1/collections", methods=['POST'])
def create():
    return request.json

@app.route("/api/v1/collections/<int:id>", methods=['PUT'])
def update_by_id(id):
    return request.json

@app.route("/api/v1/collections/<int:id>", methods=['DELETE'])
def delete_by_id(id):
    return request.json

if __name__ == "__main__":
    app.run(host='127.0.0.1', port=1983, debug=True)
EOF
          python ~/Documents/flask-playground/MockApi

Seed Data

time cat <<< '{
  "gender":[
    {
      "domain":"chorke.org", "group":"gender", "code":"M", "name":"Male",
      "properties":{}, "extendedProperties":{
        "code":{
          "ISO":1,
          "SCTID":"446151000124109"
        },
        "locale":{
          "ar_SA":"ذكر",
          "en_US":"Male",
          "bn_BD":"পুরুষ",
          "he_IL":"זָכָר",
          "ms_MY":"Jantan"
        }
      }
    },
    {
      "domain":"chorke.org", "group":"gender", "code":"F", "name":"Female",
      "properties":{}, "extendedProperties":{
        "code":{
          "ISO":2,
          "SCTID":"446141000124107"
        },
        "locale":{
          "ar_SA":"أنثى",
          "en_US":"Female",
          "bn_BD":"মহিলা",
          "he_IL":"נְקֵבָה",
          "ms_MY":"Perempuan"
        }
      }
    },
    {
      "domain":"chorke.org", "group":"gender", "code":"U", "name":"Unknown",
      "properties":{}, "extendedProperties":{
        "code":{
          "ISO":0,
          "SCTID":"UNK"
        },
        "locale":{
          "ar_SA":"مجهول",
          "bn_BD":"অজানা",
          "en_US":"Unknown",
          "he_IL":"לא ידוע",
          "ms_MY":"Tidak Diketahui"
        }
      }
    },
    {
      "domain":"chorke.org", "group":"gender", "code":"N", "name":"Not Applicable",
      "properties":{}, "extendedProperties":{
        "code":{
          "ISO":9,
          "SCTID":"33791000087105"
        },
        "locale":{
          "ar_SA":"غير قابل للتطبيق",
          "bn_BD":"প্রযোজ্য নয়",
          "en_US":"Not Applicable",
          "he_IL":"לא ישים",
          "ms_MY":"Tidak Berkaitan"
        }
      }
    }
  ]
}'| tee ~/Documents/flask-playground/MockApi/collections-v1.json >/dev/null

References