5
\$\begingroup\$

This code wraps the user endpoint and media endpoint of Instagram API. Any best practices/ styles or glaring bugs you can see? I wrote some unit tests for each class but did not include for readability

Base class:

import unirest
from oauth2 import OAuth2API


class Client(OAuth2API):
     host = "http://api.instagram.com"
     base_path = "/v1"
     header_default = {"Accept": "application/json"}
     ACCESS_TOKEN_ONLY = ["access_token"]

def __init__(self, **kwargs):
    super(Client, self).__init__(**kwargs)

def build_path(self, endpoint):
    return self.host + self.base_path + endpoint

def build_params(self, params):
    return params

def build_oauth_params(self, params):
    if params == self.ACCESS_TOKEN_ONLY:
        return {"access_token": self.access_token}
    else:
        raise NotImplementedError("ouath params {} not implemented".format(params))

def parse_request(self, endpoint, accepted_oauth_params, accepted_params):
    path = self.build_path(endpoint)
    params = self.build_params(accepted_params)
    params.update(self.build_oauth_params(accepted_oauth_params))

    return path, params

def get_request(self, endpoint, accepted_oauth_params, accepted_params):
    path, params = self.parse_request(endpoint, accepted_oauth_params, accepted_params)
    return unirest.get(path, headers=self.header_default, params=params)

User endpoint class:

from client import Client


class User(Client):
endpoint_base = "/users"

def __init__(self, **kwargs):
    super(User, self).__init__(**kwargs)

def self(self):
    oauth_params = self.ACCESS_TOKEN_ONLY
    params = {}
    endpoint = self.endpoint_base + "/self"
    response = self.get_request(endpoint, oauth_params, params)
    return response

def self_recent_media(self, count=None, min_id=None, max_id=None):
    oauth_params = self.ACCESS_TOKEN_ONLY
    params = {}
    if count:
        params.update({"count": count})
    if min_id:
        params.update({"mind_id": min_id})
    if max_id:
        params.update({"max_id": max_id})
    endpoint = self.endpoint_base + "/self/media/recent"
    response = self.get_request(endpoint, oauth_params, params)
    return response

def user_id(self, user_id):
    oauth_params = self.ACCESS_TOKEN_ONLY
    params = {}
    endpoint = self.endpoint_base + "/" + str(user_id)
    response = self.get_request(endpoint, oauth_params, params)
    return response

def user_recent_media(self, user_id, count=None, min_id=None, max_id=None):
    pass

def self_liked(self, count=None, max_like_id=None):
    oauth_params = self.ACCESS_TOKEN_ONLY
    params = {}
    if count:
        params.update({"count": count})
    if max_like_id:
        params.update({"max_like_id": max_like_id})
    endpoint = self.endpoint_base + "/self/media/liked"
    response = self.get_request(endpoint, oauth_params, params)
    return response

def search(self, query, count=None):
    oauth_params = self.ACCESS_TOKEN_ONLY
    params = {"q": query}
    if count:
        params.update({"count": count})
    endpoint = self.endpoint_base + "/search"
    response = self.get_request(endpoint, oauth_params, params)
    return response

Media endpoint class

from client import Client


class Media(Client):
endpoint_base = "/media"

def __init__(self, **kwargs):
    super(Media, self).__init__(**kwargs)

def media_id(self, media_id):
    oauth_params = self.ACCESS_TOKEN_ONLY
    params = {}
    endpoint = self.endpoint_base + "/" + str(media_id)
    response = self.get_request(endpoint, oauth_params, params)
    return response

def media_shortcode(self, shortcode):
    oauth_params = self.ACCESS_TOKEN_ONLY
    params = {}
    endpoint = self.endpoint_base + "/shortcode/" + str(shortcode)
    response = self.get_request(endpoint, oauth_params, params)
    return response

def media_search(self, latitude=None, longitude=None, distance=1000):
    oauth_params = self.ACCESS_TOKEN_ONLY
    params = {}
    if latitude and longitude:
        params.update({"lat": latitude, "lng": longitude})
    if distance != 1000:
        params.update({"distance": distance})
    endpoint = self.endpoint_base + "/search"
    response = self.get_request(endpoint, oauth_params, params)
    return response
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
    params.update({"count": count})

seems overwrought. What's wrong with just params["count"] = count ?

Also, code like:

endpoint = self.endpoint_base + "/" + str(media_id)

always makes me wonder if I'm getting the number of slashes correct, so instead I do:

import posixpath
endpoint = posixpath.join(self.endpoint_base, str(media_id))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.