หากคุณกำลังอ่านบทความนี้ คุณอาจคุ้นเคยกับความเป็นไปได้ที่มาพร้อมกับการใช้ API (Application Programming Interface) แล้ว
ด้วยการเพิ่มหนึ่งใน API แบบเปิดจำนวนมากให้กับแอปพลิเคชันของคุณ คุณสามารถขยายฟังก์ชันการทำงานของแอปพลิเคชันหรือเพิ่มคุณค่าด้วยข้อมูลที่จำเป็นได้ แต่จะเกิดอะไรขึ้นหากคุณพัฒนาคุณลักษณะเฉพาะที่คุณต้องการแชร์กับชุมชน
คำตอบนั้นง่าย: คุณต้องการ สร้าง API ของคุณเอง.
ใน REST API ประเภทคำขอและวิธีการร้องขอมีหน้าที่รับผิดชอบในการดำเนินการ เช่น โพสต์ รับ วาง ลบ
ตอนนี้เราเข้าใจคำศัพท์พื้นฐานแล้ว เราก็เริ่มสร้าง API ได้เลย
ออกแบบ
มาสร้างคลังคำพูดเกี่ยวกับปัญญาประดิษฐ์กันดีกว่า AI เป็นหนึ่งในเทคโนโลยีที่เติบโตเร็วที่สุดในปัจจุบัน และ Python เป็นเครื่องมือยอดนิยมสำหรับการทำงานกับ AI
ด้วย API นี้ นักพัฒนา Python สามารถรับข้อมูลเกี่ยวกับ AI และรับแรงบันดาลใจจากความก้าวหน้าใหม่ๆ ได้อย่างรวดเร็ว หากนักพัฒนามีความคิดเห็นอันมีค่าเกี่ยวกับหัวข้อนี้ เขาจะสามารถเพิ่มความคิดเห็นเหล่านั้นลงในพื้นที่เก็บข้อมูลได้
ai_quotes = [
{
"id": 0,
"author": "Kevin Kelly",
"quote": "The business plans of the next 10,000 startups are easy to forecast: " +
"Take X and add AI."
},
{
"id": 1,
"author": "Stephen Hawking",
"quote": "The development of full artificial intelligence could " +
"spell the end of the human race… " +
"It would take off on its own, and re-design " +
"itself at an ever increasing rate. " +
"Humans, who are limited by slow biological evolution, " +
"couldn't compete, and would be superseded."
},
{
"id": 2,
"author": "Claude Shannon",
"quote": "I visualize a time when we will be to robots what " +
"dogs are to humans, " +
"and I’m rooting for the machines."
},
{
"id": 3,
"author": "Elon Musk",
"quote": "The pace of progress in artificial intelligence " +
"(I’m not referring to narrow AI) " +
"is incredibly fast. Unless you have direct " +
"exposure to groups like Deepmind, " +
"you have no idea how fast — it is growing " +
"at a pace close to exponential. " +
"The risk of something seriously dangerous " +
"happening is in the five-year timeframe." +
"10 years at most."
},
{
"id": 4,
"author": "Geoffrey Hinton",
"quote": "I have always been convinced that the only way " +
"to get artificial intelligence to work " +
"is to do the computation in a way similar to the human brain. " +
"That is the goal I have been pursuing. We are making progress, " +
"though we still have lots to learn about " +
"how the brain actually works."
},
{
"id": 5,
"author": "Pedro Domingos",
"quote": "People worry that computers will " +
"get too smart and take over the world, " +
"but the real problem is that they're too stupid " +
"and they've already taken over the world."
},
{
"id": 6,
"author": "Alan Turing",
"quote": "It seems probable that once the machine thinking " +
"method had started, it would not take long " +
"to outstrip our feeble powers… " +
"They would be able to converse " +
"with each other to sharpen their wits. " +
"At some stage therefore, we should " +
"have to expect the machines to take control."
},
{
"id": 7,
"author": "Ray Kurzweil",
"quote": "Artificial intelligence will reach " +
"human levels by around 2029. " +
"Follow that out further to, say, 2045, " +
"we will have multiplied the intelligence, " +
"the human biological machine intelligence " +
"of our civilization a billion-fold."
},
{
"id": 8,
"author": "Sebastian Thrun",
"quote": "Nobody phrases it this way, but I think " +
"that artificial intelligence " +
"is almost a humanities discipline. It's really an attempt " +
"to understand human intelligence and human cognition."
},
{
"id": 9,
"author": "Andrew Ng",
"quote": "We're making this analogy that AI is the new electricity." +
"Electricity transformed industries: agriculture, " +
"transportation, communication, manufacturing."
}
]
ตอนนี้เราจำเป็นต้องสร้างคลาสทรัพยากรใบเสนอราคาที่จะกำหนดการดำเนินงานของจุดสิ้นสุด API ของเรา ภายในชั้นเรียน คุณจะต้องประกาศสี่วิธี: รับ, โพสต์, ใส่, ลบ
เริ่มจากวิธี GET กันก่อน
ทำให้สามารถรับใบเสนอราคาเฉพาะได้โดยการระบุ ID หรือใบเสนอราคาแบบสุ่มหากไม่ได้ระบุ ID
class Quote(Resource):
def get(self, id=0):
if id == 0:
return random.choice(ai_quotes), 200
for quote in ai_quotes:
if(quote["id"] == id):
return quote, 200
return "Quote not found", 404
วิธีการ GET ส่งกลับเครื่องหมายคำพูดแบบสุ่มหาก ID มีค่าเริ่มต้น เช่น ไม่มีการระบุ ID เมื่อเรียกใช้เมธอด
หากมีการระบุ วิธีการจะค้นหาข้อมูลอ้างอิงและค้นหาข้อมูลที่มี ID ที่ระบุ หากไม่พบสิ่งใดเลย ข้อความ “ไม่พบใบเสนอราคา 404” จะปรากฏขึ้น
ตอนนี้เรามาสร้างวิธี POST เพื่อเพิ่มใบเสนอราคาใหม่ลงในพื้นที่เก็บข้อมูล
โดยจะได้รับ ID ของใบเสนอราคาใหม่แต่ละรายการขณะที่คุณพิมพ์ นอกจากนี้ POST จะใช้ reqparse เพื่อแยกวิเคราะห์พารามิเตอร์ที่จะอยู่ในเนื้อหาคำขอ (ข้อความผู้เขียนและเครื่องหมายคำพูด)
def post(self, id):
parser = reqparse.RequestParser()
parser.add_argument("author")
parser.add_argument("quote")
params = parser.parse_args()
for quote in ai_quotes:
if(id == quote["id"]):
return f"Quote with id {id} already exists", 400
quote = {
"id": int(id),
"author": params["author"],
"quote": params["quote"]
}
ai_quotes.append(quote)
return quote, 201
ในโค้ดด้านบน วิธี POST ยอมรับ ID ใบเสนอราคา จากนั้น เมื่อใช้ reqparse ระบบจะดึงข้อมูลผู้เขียนและการอ้างอิงจากแบบสอบถาม และจัดเก็บไว้ในพจนานุกรม Params
หากมีใบเสนอราคาที่มี ID ที่ระบุอยู่แล้ว วิธีการจะแสดงข้อความและรหัสที่เกี่ยวข้อง 400
หากยังไม่ได้สร้างการอ้างอิงด้วย ID ที่ระบุ วิธีการจะสร้างบันทึกใหม่ด้วย ID และผู้แต่งที่ระบุ เช่นเดียวกับพารามิเตอร์อื่น ๆ จากนั้นจะเพิ่มรายการลงในรายการ ai_quotes และส่งคืนรายการพร้อมใบเสนอราคาใหม่พร้อมกับรหัส 201
ตอนนี้เราสร้างวิธี PUT เพื่อเปลี่ยนใบเสนอราคาที่มีอยู่ในที่เก็บ