Code Monkey home page Code Monkey logo

hahow_takehome's Introduction

Hahow Backend Engineer 徵才小專案

這是一個簡單的API server,提供GET /heroesGET /heroes/<hero_id>兩個endpoint,會透過hahow提供的api獲取對應資料,並回傳給client。

Get Started

Install requirements

git clone {repo_url}

pipenv install
pipenv shell

or 

pip install requirements.txt

設定環境變數

export FLASK_APP=flaskr

啟動 local server

flask run

專案架構

程式架構

參考MVC邏輯。
flaskr/:主要程式碼,包含API routing, error handling, controllers等主程式碼

  • views/:api routing邏輯
  • thirdparty/:第三方API 的utility function
  • tests/:相關api測試(目前只有針對api endpoint撰寫,third party尚未實現)
  • error_handler.py:錯誤Response的wrapper
  • thread.py:包含class ThreadRequest,用來執行concurrent requests

Thirdparty Library

這個專案大部分使用python或flask框架提供的library,如 requests , flask-restful、實現retry機制使用的retrying、以及程式碼品質分析(linting)的isort, flake8black


專案特色

Retry 機制

問題:由於第三方API不穩定性,使得api endpoint 頻繁回傳Invalid Response。 解方:針對最不穩定的get_profile_by_id,加入max_retry=2的機制,成功將錯誤次數降到原本的1/3。

Multithreading優化

問題:針對Authenticated request,需要回傳額外的profile data。然而由於該資料需要根據每個id一個一個去打第三方API,導致速度太慢。 解方:針對authenticated的/heros做multithread的優化,成功提升速度兩倍以上。

其他更多詳細內容與測試過程,可參考 這份Google doc


其他

註解原則

原則上,程式碼應該透過函式命名、標註回傳型態等方式,讓其他人一目瞭然,而避免用過多的註解去敘述功能。
然而若是遇到有些狀況,例如為了handle某些特殊情況而加的if-else、因應功能需求產生的額外API call、或是特別複雜的邏輯與演算法,就會需要加上comment,敘述這段程式碼做的事情以及原因。

hahow_takehome's People

Contributors

yijencheng avatar

Watchers

 avatar

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.