Showing posts with label api. Show all posts
Showing posts with label api. Show all posts

Saturday, March 12, 2022

Scala : HTTP Client using Sttp

 HTTP Client using Sttp


HttpClient in spark-shell REPL

  • open link https://mvnrepository.com/artifact/com.softwaremill.sttp.client3/core

  • open compatible scalalink

  • click on Maven tab

  • copy groupId:artifactId:version ie., com.softwaremill.sttp.client3:core_2.12:3.7.4

  • create packagename as below

  • Open terminal and run below commands :

    $spark-shell --packages com.softwaremill.sttp.client3:core_2.12:3.7.4
    
    scala> import sttp.client3._
    import sttp.client3._
    
    scala> implicit val backend = HttpURLConnectionBackend()
    backent: sttp.client.SttpBackend[sttp.client.Identity,Nothing,sttp.client.NothingT] = sttp.client.FollowRedirectsBackend@50bac431
    
    scala> val r = basicRequest.get(uri"https://pokeapi.co/api/v2/pokemon/ditto").send(backend)
    r: sttp.client.Identity[sttp.client.Response[Either[String,String]]] = Response(Right({"ab....
    
    scala> println(r)
    Response(Right({"abilities":[{"ability":{"name":"limber
    
// Ref -https://sttp.softwaremill.com/en/latest/examples.html
/*
libraryDependencies += "com.softwaremill.sttp.client3" %% "core" % "3.5.1"
Bazel Dependencies :
com.softwaremill.sttp.client3.core
com.softwaremill.sttp.client3.model
com.softwaremill.sttp.client3.shared
*/
import sttp.client3._
object SttpClient extends App{
val url= "http://0.0.0.0:9999/test/1"
val headers:Map[String,String]=Map("Content-type"->"application/json")
val payload:String="""{"name":"deepsk"}"""
post(url,headers,payload)
get(url,headers)
def get(url:String,headers:Map[String,String]):Unit={
val backend = HttpURLConnectionBackend()
val response = basicRequest.headers(headers) .get(uri"${url}").send(backend)
println(response.body)
}
def post(url:String,headers:Map[String,String],payload:String):Unit={
val backend = HttpURLConnectionBackend()
val response = basicRequest.body(payload).headers(headers) .post(uri"${url}").send(backend)
println(response.body)
}
}

Saturday, March 13, 2021

FastApi : Create Production ready Api

 FastApi : Create Production ready Api (faster than Flask)


https://fastapi.tiangolo.com/

Features

1. Asynchronous

2. High Perfromance

3. Less Code

4. Data Type  and Data Models auto Conversions

5. Auto Documentation

    - swagger (/docs)

    - ReDoc   (/redoc)


Pre-Req:

Install and activate virtual environment to be safe.

Steps:

  1. pip install fastapi
  2. pip install hypercorn #server
  3. touch main.py
  4. copy paste below code
  5. hypercorn main:app --reload


from fastapi import FastAPI
from pydantic import BaseModel #model
# import requests

app = FastAPI()
db = []

class Person(BaseModel):
name: str

@app.get('/')
def index():return {'person' : 'name'}

@app.get('/persons')
def get_persons():
results = []
for person in db:results.append(person)
return results

@app.get('/persons/{person_id}')
def get_person(person_id: int):return db[person_id-1]

@app.post('/persons')
def create_person(person: Person):
db.append(person)
return db[-1]

@app.delete('/persons/{person_id}')
def delete_person(person_id: int):
db.pop(person_id-1)
return {}