Code Monkey home page Code Monkey logo

bookapi's Introduction

BookAPI

BookAPI kullanıcıların kütüphane üzerinde CRUD işlemlerini yapabilmesini sağlar.

Dependencies: Python 3.8.0, Pip, Virtualenv

Create virtual environment

git clone https://github.com/mertbilgic/BookAPI.git
cd BookAPI
virtualenv venv

Install depedencies

$ source venv/bin/activate
$ pip install -r requirements.txt

Start

$ python manage.py runserver
Hello World

API ımızı test ederek başlayalım.Terminali açalım ve aşağıdaki komutu girelim.

# GET /
curl http://127.0.0.1:5000/
This is BookAPI

Kütüphanedeki kitapları listelemek için aşağıdaki komutu kullanırız.

# GET /books
curl http://127.0.0.1:5000/api/v1/books
{
  "books": [
    {
      "author": "Test", 
      "isbn": 123123321, 
      "name": "Test Book2", 
      "price": 7.99
    }
  ]
}

API mızın sonucuna baktığımızda JSON olmasını bekliyoruz.API'nin header'ını almak için -i ekleyelim.

# GET /books
curl -i http://127.0.0.1:5000/api/v1/books
{
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 133
Server: Werkzeug/1.0.1 Python/3.8.3
Date: Wed, 24 Jun 2020 15:10:12 GMT

  "books": [
    {
      "author": "Test", 
      "isbn": 123123321, 
      "name": "Test Book2", 
      "price": 7.99
    }
  ]
}

Beklendiği gibi, Content-Type application/json'dur.

Kütüphanedeki kitapları ISBN numarası ile listelemek için aşağıdaki komutu kullanırız.

# GET /books<int:isbn>
curl http://127.0.0.1:5000/api/v1/books/123123321
{
  "books": [
    {
      "author": "Test", 
      "isbn": 123123321, 
      "name": "Test Book2", 
      "price": 7.99
    }
  ]
}

BookAPI'daki diğer endpoint'lere request atabilmemiz için token'a ihtiyacımız var.Bunun için BookAPI'a kayıt oluyoruz.Response kodu 204 olduğu için bize herhangi bir içerik dönmez.

# POST /signup
curl -X POST http://localhost:5000/api/v1/signup \
-d '{
  "username":"testuser", 
  "password":"123456"
  }' \
-H "Content-Type: application/json" 

BookAPI'a login olduğumuzda response olarak bize bir token döner.Bu token'ı diğer requestlerimizde kullacağız.

# GET /login
curl -X GET http://localhost:5000/api/v1/login \
-d '{
  "username":"testuser", 
  "password":"123456"
  }' \
-H "Content-Type: application/json" 

Token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1OTQ5MDIwNjJ9.QQeSUCA6Ox2Ioz73bL1P4NdE2Y8YoZLjanfAN40U6s8  

Kütüphaneye yeni bir kitab eklemek için aşadağıdaki komutu kullanabiliriz.

# POST/books
curl -X POST 'http://127.0.0.1:5000/api/v1/books?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1OTQ5MDgxNzZ9.v9sASWb0WJeDjuad4hWwth8jjpsAe85hh1O09-UyHOs%20%20' \
-H 'Content-Type: application/json' \
-d'{
	"name": "New Add Test Book",
	"price": 9.99,
	"isbn": 987654321,
	"author": "New Test 123"
}'

Result:
{
    "status": "success",
    "data": {
        "author": "New Test 123",
        "name": "New Add Test Book",
        "price": 9.99,
        "isbn": 987654321
    }
}

Kütüphanedeki bir kitabı silmek için aşadağıdaki komutu kullanabiliriz.

# DELETE /books<int:isbn>
curl --location -X DELETE 'http://127.0.0.1:5000/api/v1/books/987654321?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1OTQ5MDgxNzZ9.v9sASWb0WJeDjuad4hWwth8jjpsAe85hh1O09-UyHOs%20%20' \
-H 'Content-Type: application/json' \

Kütüphanedeki bir kitabı replace etmek için aşadağıdaki komutu kullanabiliriz.

# PUT /books<int:isbn>
curl -X PUT 'http://127.0.0.1:5000/books/api/v1/123123321?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1OTQ5MDgxNzZ9.v9sASWb0WJeDjuad4hWwth8jjpsAe85hh1O09-UyHOs%20%20' \ 
-H "Content-Type: application/json" \
-d '{
        "name": "New Test Book",
        "price": 99.99,
        "isbn": 123123321,
        "author": "New Test"
}'

Kütüphanedeki bir kitabın değerlerinin bir kısmını replace etmek için aşadağıdaki komutu kullanabiliriz.

# PATCH /books<int:isbn>
curl -X PATCH 'http://127.0.0.1:5000/api/v1/books/123123321?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1OTQ5MDgxNzZ9.v9sASWb0WJeDjuad4hWwth8jjpsAe85hh1O09-UyHOs%20%20' \
-H 'Content-Type: application/json' \
-d'{
	"name": "New Add Test PATCH"
}'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.