SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
茴香豆的茴有几种写法
 记Python的⼀一个客户端实现
     dreampuf Jul, 2012
本地调用


                main




       printf          scanf
远程调用


                  Client

             Protocol Stream

       ServerA             ServerB
Esty-python
   class MockAPI(API):
       api_url = 'http://host'
       api_version = 'v1'
       def etsy_home(self):
           return Test.scratch_dir
       def get_method_table(self, *args):
           return [{'name': 'testMethod',
                    'uri': '/test/{test_id}',
                    'http_method': 'GET',
                    'params': {
                        'limit': 'int',
                        'test_id': 'user_id_or_name',
                        'offset': 'int',
                        'fizz': 'enum(foo, bar, baz)',
                        'buzz': 'float',
                        'blah': 'unknown type',
                        'kind': 'string',
                        },
                    'type': 'int',
                    'description': 'test method.'}]
       def _get_url(self, url, http_method, content_type, body):
           return '{ "count": 1, "results": [3] }'
Esty-python
   class MockAPI(API):
       api_url = 'http://host'
       api_version = 'v1'
       def etsy_home(self):
           return Test.scratch_dir
       def get_method_table(self, *args):
           return [{'name': 'testMethod',
                    'uri': '/test/{test_id}',
                    'http_method': 'GET',                     重用
      不直观           'params': {
                        'limit': 'int',                       不依赖URL
缺乏语法检查                  'test_id': 'user_id_or_name',
                        'offset': 'int',                      类型验证
    不易调试                'fizz': 'enum(foo, bar, baz)',
                        'buzz': 'float',
                        'blah': 'unknown type',
                                                              URL拓展
                        'kind': 'string',
                        },
                    'type': 'int',
                    'description': 'test method.'}]
       def _get_url(self, url, http_method, content_type, body):
           return '{ "count": 1, "results": [3] }'
GuokrAPI v1


          class GuokrAPI(API):
              HOST = "http://localhost/"
              METHODS = [{
                   "name": "get_tag",
                   "url": "tags",
                   "method": "GET",
                   "description": "Get the tag instance list",
              }, {
                   "name": "get_tags",
                   "url": "tags/%(tagname)s",
                   "method": "GET",
                   "description": "Get a tag instance",
              }]
GuokrAPI v1


          class GuokrAPI(API):
              HOST = "http://localhost/"
              METHODS = [{
    不直观            "name": "get_tag",
                   "url": "tags",
                                                                 重用
                   "method": "GET",
缺乏语法检查             "description": "Get the tag instance list",   不依赖URL
              }, {
   不易调试            "name": "get_tags",                           URLLIB3
                   "url": "tags/%(tagname)s",
   类型验证            "method": "GET",                              URL拓展
                   "description": "Get a tag instance",
              }]
GuokrAPI v2
   tagging = guokr.type(                import guokr
       'tagging'                        import minerva.types as minerva
   ).fields({
       'id': 'int',                     tags = guokr.resources([
       'tag': 'string',                     minerva.tag
       'taggable': 'taggable',          ])
       'tag': 'string',
       'user': 'ukey',                  taggings = guokr.resources([
       'date_create': 'datetime',           minerva.tag,
       'date_deleted': 'datetime',          minerva.taggable,
   }).keys([                            ]).namespace(
     'id'                                   'taggings'
   ]).end()                             )


                 mercury.group.mixin(minerva.taggable)

                 # POST on url /tags
                 call = resources.tags.create().format('jsonp')

                 # POST on url /taggings/tags/科学/group/654321
                 call = resources.taggings.update({
                     'tag': '科学',
                     'taggable': group,
                     'user': 'afsrgx',
                 })
GuokrAPI v2
   tagging = guokr.type(                import guokr
       'tagging'                        import minerva.types as minerva
   ).fields({
       'id': 'int',                     tags = guokr.resources([
       'tag': 'string',                     minerva.tag
       'taggable': 'taggable',          ])
       'tag': 'string',
       'user': 'ukey',                  taggings = guokr.resources([
       'date_create': 'datetime',           minerva.tag,
       'date_deleted': 'datetime',          minerva.taggable,
   }).keys([                            ]).namespace(
     'id'                                   'taggings'
   ]).end()                             )


                 mercury.group.mixin(minerva.taggable)
                                                                   重用
                 # POST on url /tags
  URL拓展          call = resources.tags.create().format('jsonp')
                                                                   类型验证
  抽象繁多           # POST on url /taggings/tags/科学/group/654321      链式语法
                 call = resources.taggings.update({
                     'tag': '科学',                                  清晰
                     'taggable': group,
                     'user': 'afsrgx',
                 })
GuokrAPI v2
GuokrAPI v2




              Pyt hon
GuokrAPI v2




              Py   t hon   ic
GuokrAPI v3

  TUPLE_TAGGINGABLE = ("article", "post", "blog", "group", "question")
  class GuokrAPI(API):
      HOST = "http://localhost:5000/"

      @GET("tags/%(tag)s")
      def get_tag(tag): pass
      @GET("tags/%(tag)s/feeds")
      def get_tag_feeds(tag, filter=str): pass
      @POST("tags/")
      def add_tag(tag, synonym=str, logo=str, description=str): pass
      @POST("tag/%(tag)s")
      def post_tag(tag, logo=str, description=str): pass

   class GuokrCMSAPI(GuokrAPI):
      @GET("cms/tags/filter:%(prefix)s")
      def get_tag_prefix(prefix): pass
      #Duplicate
      #@POST("cms/tags")
      #def add_tag(self, tag, synonym=None, logo=None, description=None): pass
      @POST("cms/tags/%(tag)s")
      def post_tag(tag, logon=str, synonyms=str): pass #synonyms VS synonym
      @POST("cms/tags/%(tag)s/lock")
GuokrAPI v3

  TUPLE_TAGGINGABLE = ("article", "post", "blog", "group", "question")
  class GuokrAPI(API):
      HOST = "http://localhost:5000/"

      @GET("tags/%(tag)s")                                               重用
      def get_tag(tag): pass
      @GET("tags/%(tag)s/feeds")                                         类型验证
      def get_tag_feeds(tag, filter=str): pass
      @POST("tags/")                                                     清晰
      def add_tag(tag, synonym=str, logo=str, description=str): pass
      @POST("tag/%(tag)s")                                               调试/直观
      def post_tag(tag, logo=str, description=str): pass

   class GuokrCMSAPI(GuokrAPI):
      @GET("cms/tags/filter:%(prefix)s")
      def get_tag_prefix(prefix): pass
      #Duplicate
      #@POST("cms/tags")
      #def add_tag(self, tag, synonym=None, logo=None, description=None): pass
      @POST("cms/tags/%(tag)s")
      def post_tag(tag, logon=str, synonyms=str): pass #synonyms VS synonym
      @POST("cms/tags/%(tag)s/lock")
结论

•统⼀一   随时注意前后接口⼀一致
•抽象    隐藏不必要的细节
•充要    只接受充分且必须的参数
•直观    对人友好,对机器友好(调试)
•原生    Pythonic
DATA ENTRY
Data Entry v1




                a = (1, 2, 3)
Data Entry v2




                a = {“a”:1,
                     “b”:2,
                     “c”:3)
Data Entry v3




class uDict(dict):
    def __getattr__(self, name):
        return self.__getitem__(name)
Data Entry v4


class uDict(dict):
    def __getattr__(self, name, default=None):
        try:
             return self.__getitem__(name)
        except KeyError:
             return default

a = uDict(a=1, b=2, c=3)
print a.b
print a.g
Data Entry v5




       class Entry(object):
           def __init__(self, a, b, c):
               self.a = a
               self.b = b
               self.c = c
Data Entry v6




     from collections import namedtuple
     Entry = namedtuple("Entry", "a b c")
     a = Entry(a=1, b=2, c=3)
Data Entry v7




class Entry(namedtuple("Entry", "a b c")):
    def __new__(cls, a=None, b=None, c=None):
        return super(Entry, cls).__new__(cls, a, b, c)
谢谢!
REFERENCE
•Etsy-python
https://github.com/mcfunley/etsy-python
•Lambda Picture
http://www.flickr.com/photos/rofi/2097239111/

Weitere ähnliche Inhalte

Was ist angesagt?

Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Masahiro Nagano
 
Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - GuilinJackson Tian
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Kris Wallsmith
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHPTaras Kalapun
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)Nikita Popov
 
Modern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlModern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlNova Patch
 
The state of your own hypertext preprocessor
The state of your own hypertext preprocessorThe state of your own hypertext preprocessor
The state of your own hypertext preprocessorAlessandro Nadalin
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP GeneratorsMark Baker
 
Testing Web Applications with GEB
Testing Web Applications with GEBTesting Web Applications with GEB
Testing Web Applications with GEBHoward Lewis Ship
 
Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)MongoSF
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Jeff Carouth
 
Command Bus To Awesome Town
Command Bus To Awesome TownCommand Bus To Awesome Town
Command Bus To Awesome TownRoss Tuck
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersIan Barber
 
Spring data iii
Spring data iiiSpring data iii
Spring data iii명철 강
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helperslicejack
 
deepjs - tools for better programming
deepjs - tools for better programmingdeepjs - tools for better programming
deepjs - tools for better programmingnomocas
 

Was ist angesagt? (18)

Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
 
Mongoskin - Guilin
Mongoskin - GuilinMongoskin - Guilin
Mongoskin - Guilin
 
PHP 5.4
PHP 5.4PHP 5.4
PHP 5.4
 
Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)Doctrine MongoDB ODM (PDXPHP)
Doctrine MongoDB ODM (PDXPHP)
 
Crazy things done on PHP
Crazy things done on PHPCrazy things done on PHP
Crazy things done on PHP
 
PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)PHP 7 – What changed internally? (Forum PHP 2015)
PHP 7 – What changed internally? (Forum PHP 2015)
 
Modern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in PerlModern Getopt for Command Line Processing in Perl
Modern Getopt for Command Line Processing in Perl
 
The state of your own hypertext preprocessor
The state of your own hypertext preprocessorThe state of your own hypertext preprocessor
The state of your own hypertext preprocessor
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP Generators
 
Testing Web Applications with GEB
Testing Web Applications with GEBTesting Web Applications with GEB
Testing Web Applications with GEB
 
Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
Command Bus To Awesome Town
Command Bus To Awesome TownCommand Bus To Awesome Town
Command Bus To Awesome Town
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
 
Spring data iii
Spring data iiiSpring data iii
Spring data iii
 
WordPress Cuztom Helper
WordPress Cuztom HelperWordPress Cuztom Helper
WordPress Cuztom Helper
 
deepjs - tools for better programming
deepjs - tools for better programmingdeepjs - tools for better programming
deepjs - tools for better programming
 
360|iDev
360|iDev360|iDev
360|iDev
 

Andere mochten auch

The introduction of data visualization
The introduction of data visualizationThe introduction of data visualization
The introduction of data visualizationdreampuf
 
Refactoring
RefactoringRefactoring
Refactoringdreampuf
 
Communication with python_http_module
Communication with python_http_moduleCommunication with python_http_module
Communication with python_http_moduledreampuf
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethodsdreampuf
 
Machine learning share No.1
Machine learning share No.1Machine learning share No.1
Machine learning share No.1dreampuf
 
Python profiling
Python profilingPython profiling
Python profilingdreampuf
 
A short introduction of D3js
A short introduction of D3jsA short introduction of D3js
A short introduction of D3jsdreampuf
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome EconomyHelge Tennø
 

Andere mochten auch (8)

The introduction of data visualization
The introduction of data visualizationThe introduction of data visualization
The introduction of data visualization
 
Refactoring
RefactoringRefactoring
Refactoring
 
Communication with python_http_module
Communication with python_http_moduleCommunication with python_http_module
Communication with python_http_module
 
Python magicmethods
Python magicmethodsPython magicmethods
Python magicmethods
 
Machine learning share No.1
Machine learning share No.1Machine learning share No.1
Machine learning share No.1
 
Python profiling
Python profilingPython profiling
Python profiling
 
A short introduction of D3js
A short introduction of D3jsA short introduction of D3js
A short introduction of D3js
 
The Outcome Economy
The Outcome EconomyThe Outcome Economy
The Outcome Economy
 

Ähnlich wie Python client api

Let's read code: the python-requests library
Let's read code: the python-requests libraryLet's read code: the python-requests library
Let's read code: the python-requests librarySusan Tan
 
前端MVC 豆瓣说
前端MVC 豆瓣说前端MVC 豆瓣说
前端MVC 豆瓣说Ting Lv
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Tsuyoshi Yamamoto
 
Recent Changes to jQuery's Internals
Recent Changes to jQuery's InternalsRecent Changes to jQuery's Internals
Recent Changes to jQuery's Internalsjeresig
 
XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話Takehito Tanabe
 
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用Felinx Lee
 
Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Julien Truffaut
 
Тестирование и Django
Тестирование и DjangoТестирование и Django
Тестирование и DjangoMoscowDjango
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsFrancois Zaninotto
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecturepostrational
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Fabien Potencier
 
MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know Norberto Leite
 
Mongo+java (1)
Mongo+java (1)Mongo+java (1)
Mongo+java (1)MongoDB
 
GraphQL Los Angeles Meetup Slides
GraphQL Los Angeles Meetup SlidesGraphQL Los Angeles Meetup Slides
GraphQL Los Angeles Meetup SlidesGrant Miller
 
Simplifying Persistence for Java and MongoDB with Morphia
Simplifying Persistence for Java and MongoDB with MorphiaSimplifying Persistence for Java and MongoDB with Morphia
Simplifying Persistence for Java and MongoDB with MorphiaMongoDB
 
Open Source Search: An Analysis
Open Source Search: An AnalysisOpen Source Search: An Analysis
Open Source Search: An AnalysisJustin Finkelstein
 
Let's read code: python-requests library
Let's read code: python-requests libraryLet's read code: python-requests library
Let's read code: python-requests librarySusan Tan
 

Ähnlich wie Python client api (20)

Let's read code: the python-requests library
Let's read code: the python-requests libraryLet's read code: the python-requests library
Let's read code: the python-requests library
 
前端MVC 豆瓣说
前端MVC 豆瓣说前端MVC 豆瓣说
前端MVC 豆瓣说
 
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
Grails 1.2 探検隊 -新たな聖杯をもとめて・・・-
 
Recent Changes to jQuery's Internals
Recent Changes to jQuery's InternalsRecent Changes to jQuery's Internals
Recent Changes to jQuery's Internals
 
XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話XamarinとAWSをつないでみた話
XamarinとAWSをつないでみた話
 
用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用
 
Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!Implicit parameters, when to use them (or not)!
Implicit parameters, when to use them (or not)!
 
Blog Hacks 2011
Blog Hacks 2011Blog Hacks 2011
Blog Hacks 2011
 
What's new in Django 1.2?
What's new in Django 1.2?What's new in Django 1.2?
What's new in Django 1.2?
 
Тестирование и Django
Тестирование и DjangoТестирование и Django
Тестирование и Django
 
Bonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node jsBonnes pratiques de développement avec Node js
Bonnes pratiques de développement avec Node js
 
Scalable web application architecture
Scalable web application architectureScalable web application architecture
Scalable web application architecture
 
Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)Be RESTful (Symfony Camp 2008)
Be RESTful (Symfony Camp 2008)
 
Objective-c Runtime
Objective-c RuntimeObjective-c Runtime
Objective-c Runtime
 
MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know MongoDB + Java - Everything you need to know
MongoDB + Java - Everything you need to know
 
Mongo+java (1)
Mongo+java (1)Mongo+java (1)
Mongo+java (1)
 
GraphQL Los Angeles Meetup Slides
GraphQL Los Angeles Meetup SlidesGraphQL Los Angeles Meetup Slides
GraphQL Los Angeles Meetup Slides
 
Simplifying Persistence for Java and MongoDB with Morphia
Simplifying Persistence for Java and MongoDB with MorphiaSimplifying Persistence for Java and MongoDB with Morphia
Simplifying Persistence for Java and MongoDB with Morphia
 
Open Source Search: An Analysis
Open Source Search: An AnalysisOpen Source Search: An Analysis
Open Source Search: An Analysis
 
Let's read code: python-requests library
Let's read code: python-requests libraryLet's read code: python-requests library
Let's read code: python-requests library
 

Kürzlich hochgeladen

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 

Kürzlich hochgeladen (20)

DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Python client api

  • 2.
  • 3. 本地调用 main printf scanf
  • 4. 远程调用 Client Protocol Stream ServerA ServerB
  • 5. Esty-python class MockAPI(API): api_url = 'http://host' api_version = 'v1' def etsy_home(self): return Test.scratch_dir def get_method_table(self, *args): return [{'name': 'testMethod', 'uri': '/test/{test_id}', 'http_method': 'GET', 'params': { 'limit': 'int', 'test_id': 'user_id_or_name', 'offset': 'int', 'fizz': 'enum(foo, bar, baz)', 'buzz': 'float', 'blah': 'unknown type', 'kind': 'string', }, 'type': 'int', 'description': 'test method.'}] def _get_url(self, url, http_method, content_type, body): return '{ "count": 1, "results": [3] }'
  • 6. Esty-python class MockAPI(API): api_url = 'http://host' api_version = 'v1' def etsy_home(self): return Test.scratch_dir def get_method_table(self, *args): return [{'name': 'testMethod', 'uri': '/test/{test_id}', 'http_method': 'GET', 重用 不直观 'params': { 'limit': 'int', 不依赖URL 缺乏语法检查 'test_id': 'user_id_or_name', 'offset': 'int', 类型验证 不易调试 'fizz': 'enum(foo, bar, baz)', 'buzz': 'float', 'blah': 'unknown type', URL拓展 'kind': 'string', }, 'type': 'int', 'description': 'test method.'}] def _get_url(self, url, http_method, content_type, body): return '{ "count": 1, "results": [3] }'
  • 7. GuokrAPI v1 class GuokrAPI(API): HOST = "http://localhost/" METHODS = [{ "name": "get_tag", "url": "tags", "method": "GET", "description": "Get the tag instance list", }, { "name": "get_tags", "url": "tags/%(tagname)s", "method": "GET", "description": "Get a tag instance", }]
  • 8. GuokrAPI v1 class GuokrAPI(API): HOST = "http://localhost/" METHODS = [{ 不直观 "name": "get_tag", "url": "tags", 重用 "method": "GET", 缺乏语法检查 "description": "Get the tag instance list", 不依赖URL }, { 不易调试 "name": "get_tags", URLLIB3 "url": "tags/%(tagname)s", 类型验证 "method": "GET", URL拓展 "description": "Get a tag instance", }]
  • 9. GuokrAPI v2 tagging = guokr.type( import guokr     'tagging' import minerva.types as minerva ).fields({     'id': 'int', tags = guokr.resources([     'tag': 'string',     minerva.tag     'taggable': 'taggable', ])     'tag': 'string',     'user': 'ukey', taggings = guokr.resources([     'date_create': 'datetime',     minerva.tag,     'date_deleted': 'datetime',     minerva.taggable, }).keys([ ]).namespace(   'id'     'taggings' ]).end() ) mercury.group.mixin(minerva.taggable) # POST on url /tags call = resources.tags.create().format('jsonp') # POST on url /taggings/tags/科学/group/654321 call = resources.taggings.update({     'tag': '科学',     'taggable': group,     'user': 'afsrgx', })
  • 10. GuokrAPI v2 tagging = guokr.type( import guokr     'tagging' import minerva.types as minerva ).fields({     'id': 'int', tags = guokr.resources([     'tag': 'string',     minerva.tag     'taggable': 'taggable', ])     'tag': 'string',     'user': 'ukey', taggings = guokr.resources([     'date_create': 'datetime',     minerva.tag,     'date_deleted': 'datetime',     minerva.taggable, }).keys([ ]).namespace(   'id'     'taggings' ]).end() ) mercury.group.mixin(minerva.taggable) 重用 # POST on url /tags URL拓展 call = resources.tags.create().format('jsonp') 类型验证 抽象繁多 # POST on url /taggings/tags/科学/group/654321 链式语法 call = resources.taggings.update({     'tag': '科学', 清晰     'taggable': group,     'user': 'afsrgx', })
  • 12. GuokrAPI v2 Pyt hon
  • 13. GuokrAPI v2 Py t hon ic
  • 14. GuokrAPI v3 TUPLE_TAGGINGABLE = ("article", "post", "blog", "group", "question") class GuokrAPI(API): HOST = "http://localhost:5000/" @GET("tags/%(tag)s") def get_tag(tag): pass @GET("tags/%(tag)s/feeds") def get_tag_feeds(tag, filter=str): pass @POST("tags/") def add_tag(tag, synonym=str, logo=str, description=str): pass @POST("tag/%(tag)s") def post_tag(tag, logo=str, description=str): pass class GuokrCMSAPI(GuokrAPI): @GET("cms/tags/filter:%(prefix)s") def get_tag_prefix(prefix): pass #Duplicate #@POST("cms/tags") #def add_tag(self, tag, synonym=None, logo=None, description=None): pass @POST("cms/tags/%(tag)s") def post_tag(tag, logon=str, synonyms=str): pass #synonyms VS synonym @POST("cms/tags/%(tag)s/lock")
  • 15. GuokrAPI v3 TUPLE_TAGGINGABLE = ("article", "post", "blog", "group", "question") class GuokrAPI(API): HOST = "http://localhost:5000/" @GET("tags/%(tag)s") 重用 def get_tag(tag): pass @GET("tags/%(tag)s/feeds") 类型验证 def get_tag_feeds(tag, filter=str): pass @POST("tags/") 清晰 def add_tag(tag, synonym=str, logo=str, description=str): pass @POST("tag/%(tag)s") 调试/直观 def post_tag(tag, logo=str, description=str): pass class GuokrCMSAPI(GuokrAPI): @GET("cms/tags/filter:%(prefix)s") def get_tag_prefix(prefix): pass #Duplicate #@POST("cms/tags") #def add_tag(self, tag, synonym=None, logo=None, description=None): pass @POST("cms/tags/%(tag)s") def post_tag(tag, logon=str, synonyms=str): pass #synonyms VS synonym @POST("cms/tags/%(tag)s/lock")
  • 16. 结论 •统⼀一 随时注意前后接口⼀一致 •抽象 隐藏不必要的细节 •充要 只接受充分且必须的参数 •直观 对人友好,对机器友好(调试) •原生 Pythonic
  • 18. Data Entry v1 a = (1, 2, 3)
  • 19. Data Entry v2 a = {“a”:1, “b”:2, “c”:3)
  • 20. Data Entry v3 class uDict(dict): def __getattr__(self, name): return self.__getitem__(name)
  • 21. Data Entry v4 class uDict(dict): def __getattr__(self, name, default=None): try: return self.__getitem__(name) except KeyError: return default a = uDict(a=1, b=2, c=3) print a.b print a.g
  • 22. Data Entry v5 class Entry(object): def __init__(self, a, b, c): self.a = a self.b = b self.c = c
  • 23. Data Entry v6 from collections import namedtuple Entry = namedtuple("Entry", "a b c") a = Entry(a=1, b=2, c=3)
  • 24. Data Entry v7 class Entry(namedtuple("Entry", "a b c")): def __new__(cls, a=None, b=None, c=None): return super(Entry, cls).__new__(cls, a, b, c)